-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding Mockery TestCase and TestCaseTest back (#22)
- Loading branch information
1 parent
a390cac
commit 8483127
Showing
3 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |