From 4d6e120c74020114f859f2798827d00d8ac3cf3c Mon Sep 17 00:00:00 2001 From: John P Bloch Date: Thu, 22 Dec 2016 11:24:45 -0600 Subject: [PATCH] Add a test for more complex argument representations in the message --- tests/WP_Mock/DeprecatedListenerTest.php | 45 ++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/WP_Mock/DeprecatedListenerTest.php b/tests/WP_Mock/DeprecatedListenerTest.php index 9eab484f..6276b718 100644 --- a/tests/WP_Mock/DeprecatedListenerTest.php +++ b/tests/WP_Mock/DeprecatedListenerTest.php @@ -70,6 +70,51 @@ public function testCheckCalls_scalar_only() { $this->object->checkCalls(); } + public function testCheckCalls_non_scalars() { + $callback1 = function () { + }; + $object1 = Mockery::mock( 'WP_Query' ); + $range = rand( 5, 10 ); + $resource = fopen( 'php://temp', 'r' ); + $this->object->logDeprecatedCall( 'BazBat::fooBar', array( $callback1 ) ); + $this->object->logDeprecatedCall( 'BazBat::fooBar', array( $object1 ) ); + $this->object->logDeprecatedCall( 'LongerClassName::callback', array( array( $object1, 'shouldReceive' ) ) ); + $this->object->logDeprecatedCall( 'BazBat::fooBar', array( range( 1, $range ), $resource ) ); + $this->object->setTestName( 'OtherTest' ); + $testCase = Mockery::mock( 'PHPUnit_Framework_TestCase' ); + /** @var PHPUnit_Framework_TestCase $testCase */ + $this->object->setTestCase( $testCase ); + $result = Mockery::mock( 'PHPUnit_Framework_TestResult' ); + $testClosure = function ( $case, $exception, $int ) use ( $testCase, $callback1, $object1, $range ) { + $callback1 = get_class( $callback1 ) . ':' . spl_object_hash( $callback1 ); + $object1 = get_class( $object1 ) . ':' . spl_object_hash( $object1 ); + PHPUnit_Framework_Assert::assertSame( $testCase, $case ); + PHPUnit_Framework_Assert::assertTrue( $exception instanceof PHPUnit_Framework_RiskyTest ); + $message = <<"] + ["<$object1>"] + ["Array([$range] ...)","Resource"] + LongerClassName::callback ["[<$object1>,shouldReceive]"] +EOT; + PHPUnit_Framework_Assert::assertEquals( $message, $exception->getMessage() ); + PHPUnit_Framework_Assert::assertTrue( 0 === $int ); + }; + $result->shouldReceive( 'addFailure' ) + ->once() + ->andReturnUsing( $testClosure ); + /** @var \PHPUnit_Framework_TestResult $result */ + $this->object->setTestResult( $result ); + + try { + $this->object->checkCalls(); + } catch ( \Exception $e ) { + fclose( $resource ); + throw $e; + } + fclose( $resource ); + } + protected function getCalls( $listener ) { $prop = new ReflectionProperty( $listener, 'calls' ); $prop->setAccessible( true );