Skip to content

Commit

Permalink
adding Mockery TestCase and TestCaseTest back (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrewsMorgue authored and jgedarovich committed Mar 23, 2018
1 parent a390cac commit 8483127
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 2 deletions.
71 changes: 71 additions & 0 deletions src/PHPUnit/Extensions/Mockery/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace PHPUnit\Extensions\Mockery;

use Mockery;
use ReflectionClass;

abstract class TestCase extends \PHPUnit\Framework\TestCase {

const REGEX_MOCK = '/@mockery\s+([a-zA-Z0-9._:-\\\\x7f-\xff]+)/';

protected function setUp() {
parent::setUp();
$class = new ReflectionClass($this);
$properties = $class->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<name>[A-Za-z_-]+)(?:[ \t]+(?P<value>.*?))?[ \t]*\r?$/m', $docblock, $matches)) {
$numMatches = count($matches[0]);
for ($i = 0; $i < $numMatches; ++$i) {
$annotations[$matches['name'][$i]][] = $matches['value'][$i];
}
}
return $annotations;
}
}
4 changes: 2 additions & 2 deletions src/PHPUnit/Extensions/MultipleDatabase/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
89 changes: 89 additions & 0 deletions tests/Mockery/Testcase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace PHPUnit\Extensions\Mockery;

use PHPUnit\Extensions\Mockery\TestCase;
use Mockery;

interface Tests_Extensions_Mockery_TestCase_Foo {
function foo();
}
interface Tests_Extensions_Mockery_TestCase_Bar {
function bar();
}
class Tests_Extensions_Mockery_TestCase_Baz {
private $foo;

function __construct($foo) {
$this->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());
}
}

0 comments on commit 8483127

Please sign in to comment.