Skip to content

Commit

Permalink
Removing mocking of reflection classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed May 28, 2013
1 parent 84b8744 commit b6b98fe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace ZfrRestTest\Mvc\View\Http;

use PHPUnit_Framework_TestCase as TestCase;
use ReflectionClass;
use Zend\Http\Request as HttpRequest;
use Zend\Mvc\Router\Http\RouteMatch;
use Zend\Stdlib\Hydrator\HydratorPluginManager;
Expand Down Expand Up @@ -65,15 +66,15 @@ public function testCanCreatePayload()

$classMetadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
$resourceMetadata = new ResourceMetadata('stdClass');
$reflectionClass = new ReflectionClass($data);

$resourceMetadata->hydrator = 'Zend\Stdlib\Hydrator\ObjectProperty';
$resourceMetadata->classMetadata = $classMetadata;

$reflectionClass = $this->getMock('ReflectionClass', array(), array(), '', false);
$reflectionClass->expects($this->any())->method('isInstance')->will($this->returnValue(true));

$classMetadata->expects($this->any())->method('getReflectionClass')->will($this->returnValue($reflectionClass));

$resource = new Resource($data, $resourceMetadata);

$routeMatch = new RouteMatch(array('resource' => $resource));

$this->event->setRouteMatch($routeMatch);
Expand Down
4 changes: 2 additions & 2 deletions tests/ZfrRestTest/Resource/Metadata/ResourceMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace ZfrRestTest\Resource\Metadata;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Server\Reflection\ReflectionClass;
use ZfrRest\Resource\Metadata\ResourceMetadata;

/**
Expand Down Expand Up @@ -79,9 +80,8 @@ public function testCanCreateEmptyResource()
$resourceMetadata = new ResourceMetadata('stdClass');
$metadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
$resourceMetadata->classMetadata = $metadata;
$reflectionClass = new ReflectionClass('stdClass');

$reflectionClass = $this->getMock('ReflectionClass', array(), array(), '', false);
$reflectionClass->expects($this->any())->method('isInstance')->will($this->returnValue(true));
$metadata->expects($this->any())->method('getReflectionClass')->will($this->returnValue($reflectionClass));

$resource = $resourceMetadata->createResource();
Expand Down
41 changes: 16 additions & 25 deletions tests/ZfrRestTest/Resource/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace ZfrRestTest\Resource;

use PHPUnit_Framework_TestCase as TestCase;
use ReflectionClass;
use ZfrRest\Resource\Resource;

/**
Expand All @@ -35,18 +36,14 @@ class ResourceTest extends TestCase
* @covers \ZfrRest\Resource\Resource::isCollection
*
* @dataProvider collectionResourceProvider
*
* @param string $className
* @param mixed $instance
* @param bool $isCollection
*/
public function testResource($instance, $isCollection)
public function testResource($className, $instance, $isCollection)
{
$metadata = $this->createMetadata();

$metadata
->getClassMetadata()
->getReflectionClass()
->expects($this->any())
->method('isInstance')
->will($this->returnValue(! $isCollection));

$metadata = $this->createMetadata(new ReflectionClass($className));
$resource = new Resource($instance, $metadata);

$this->assertSame($instance, $resource->getData());
Expand All @@ -60,14 +57,7 @@ public function testResource($instance, $isCollection)
*/
public function testDisallowsInvalidResource()
{
$metadata = $this->createMetadata();

$metadata
->getClassMetadata()
->getReflectionClass()
->expects($this->any())
->method('isInstance')
->will($this->returnValue(false));
$metadata = $this->createMetadata(new ReflectionClass($this));

$this->setExpectedException('ZfrRest\\Resource\\Exception\\InvalidResourceException');

Expand All @@ -82,22 +72,23 @@ public function testDisallowsInvalidResource()
public function collectionResourceProvider()
{
return array(
array($this->getMock('Iterator'), true),
array($this->getMock('Doctrine\\Common\\Collections\\Selectable'), true),
array($this->getMock('Doctrine\\Common\\Collections\\Collection'), true),
array(array(), true),
array(new \stdClass(), false),
array('stdClass', $this->getMock('Iterator'), true),
array('stdClass', $this->getMock('Doctrine\\Common\\Collections\\Selectable'), true),
array('stdClass', $this->getMock('Doctrine\\Common\\Collections\\Collection'), true),
array('stdClass', array(), true),
array('stdClass', new \stdClass(), false),
);
}

/**
* @param ReflectionClass $reflectionClass
*
* @return \PHPUnit_Framework_MockObject_MockObject|\ZfrRest\Resource\Metadata\ResourceMetadataInterface
*/
private function createMetadata()
private function createMetadata(ReflectionClass $reflectionClass)
{
$resourceMetadata = $this->getMock('ZfrRest\\Resource\\Metadata\\ResourceMetadataInterface');
$metadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
$reflectionClass = $this->getMock('ReflectionClass', array(), array(), '', false);

$resourceMetadata->expects($this->any())->method('getClassMetadata')->will($this->returnValue($metadata));
$metadata->expects($this->any())->method('getReflectionClass')->will($this->returnValue($reflectionClass));
Expand Down

0 comments on commit b6b98fe

Please sign in to comment.