diff --git a/src/PHPUnit/Extensions/Mockery/TestCase.php b/src/PHPUnit/Extensions/Mockery/TestCase.php new file mode 100644 index 0000000..f397b02 --- /dev/null +++ b/src/PHPUnit/Extensions/Mockery/TestCase.php @@ -0,0 +1,71 @@ +getProperties(); + foreach ($properties as $property) { + $doc_comment = $property->getDocComment(); + if (preg_match(self::REGEX_MOCK, $doc_comment, $matches)) { + $annotations = $this->parseAnnotations($doc_comment); + if (isset($annotations['mockery'])) { + $property_name = $property->getName(); + $this->{$property_name} = + $this->getMockery($annotations['mockery'][0]); + } + } + } + } + + protected function verifyMockObjects() { + $container = Mockery::getContainer(); + if (isset($container)) { + $reflected_container = new ReflectionClass($container); + $reflected_mocks = $reflected_container->getProperty('_mocks'); + $reflected_mocks->setAccessible(true); + $mocks = $reflected_mocks->getValue($container); + foreach ($mocks as $mock) { + $reflected_mock = new ReflectionClass($mock); + $reflected_expectations = + $reflected_mock->getProperty('_mockery_expectations'); + $reflected_expectations->setAccessible(true); + $expectations = $reflected_expectations->getValue($mock); + foreach ($expectations as $director) { + $this->addToAssertionCount(count($director->getExpectations())); + } + } + Mockery::close(); + } + + + parent::verifyMockObjects(); + } + + protected function getMockery() { + $args = func_get_args(); + return call_user_func_array(array('Mockery', 'mock'), $args); + } + + // TODO: Use PHPUnit_Util_Test::parseAnnotations() instead + private function parseAnnotations($docblock) { + $annotations = array(); + // Strip away the docblock header and footer to ease parsing of one line annotations + $docblock = substr($docblock, 3, -2); + if (preg_match_all('/@(?P[A-Za-z_-]+)(?:[ \t]+(?P.*?))?[ \t]*\r?$/m', $docblock, $matches)) { + $numMatches = count($matches[0]); + for ($i = 0; $i < $numMatches; ++$i) { + $annotations[$matches['name'][$i]][] = $matches['value'][$i]; + } + } + return $annotations; + } +} diff --git a/src/PHPUnit/Extensions/MultipleDatabase/TestCase.php b/src/PHPUnit/Extensions/MultipleDatabase/TestCase.php index acb0370..6854431 100644 --- a/src/PHPUnit/Extensions/MultipleDatabase/TestCase.php +++ b/src/PHPUnit/Extensions/MultipleDatabase/TestCase.php @@ -9,7 +9,7 @@ * populate multiple databases for tests dependent upon databases. */ abstract class TestCase -extends \PHPUnit\Framework\TestCase { +extends \PHPUnit\Extensions\Mockery\TestCase { private $testers; @@ -42,7 +42,7 @@ public static function assertTablesEqual($expected, $actual) { \PHPUnit\DbUnit\TestCase::assertTablesEqual($expected, $actual); } -/** + /** * Asserts that two given datasets are equal. * * @param IDataSet $expected diff --git a/tests/Mockery/Testcase.php b/tests/Mockery/Testcase.php new file mode 100644 index 0000000..c646ec7 --- /dev/null +++ b/tests/Mockery/Testcase.php @@ -0,0 +1,89 @@ +foo = $foo; + } + + function baz() { + $this->foo->foo(); + } +} +class Tests_Extensions_Mockery_TestCaseTest extends TestCase { + /** @mockery Tests_Extensions_Mockery_TestCase_Foo */ + protected $foo; + + /** @mockery Tests_Extensions_Mockery_TestCase_Bar */ + protected $bar; + + protected $baz; + + protected $old; + + protected function setUp() { + parent::setUp(); + $this->baz = $this->getMockery( + new Tests_Extensions_Mockery_TestCase_Baz($this->foo) + ); + $this->old = $this->getMockBuilder(Tests_Extensions_Mockery_TestCase_Baz::class) + ->disableOriginalConstructor() + ->getMock(); + } + + public function testFoo_notNull() { + $this->assertNotNull($this->foo); + } + + public function testFoo_canMockFunction() { + $this->foo->shouldReceive('foo')->andReturn(2)->atLeast(1); + $this->assertEquals(2, $this->foo->foo()); + } + + public function testBar_notNull() { + $this->assertNotNull($this->bar); + } + + public function testBar_canMockFunction() { + $this->bar->shouldReceive('bar')->never(); + } + + public function testBaz_notNull() { + $this->assertNotNull($this->baz); + } + + public function testBaz_partialCallsThrough() { + $this->foo->shouldReceive('foo')->once(); + $this->baz->baz(); + } + + public function testBaz_bypassFoo() { + $this->foo->shouldReceive('foo')->never(); + $this->baz->shouldReceive('baz')->once(); + $this->baz->baz(); + } + + public function testOld_notNull() { + $this->assertNotNull($this->old); + } + + public function testOld_canMockFunction() { + $this->old + ->expects($this->atLeastOnce()) + ->method('baz') + ->will($this->returnSelf()); + $this->assertEquals($this->old, $this->old->baz()); + } +}