Skip to content

Commit

Permalink
Further cleanups in the graph route implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Jun 12, 2013
1 parent f318d81 commit 3a26207
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 29 deletions.
10 changes: 6 additions & 4 deletions src/ZfrRest/Mvc/Router/Http/ResourceGraphRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,18 @@ protected function buildRouteMatch(ResourceInterface $resource, $path)
$classMetadata = $metadata->getClassMetadata();
$data = $resource->getData();

if ($resource->isCollection() && $data instanceof Collection) {
$resource = new Resource(new ResourcePaginator($metadata, new CollectionAdapter($data)), $metadata);
}

// If returned $data is a collection, then we use the controller specified in Collection mapping
if ($resource->isCollection()) {
if (null === $collectionMetadata) {
throw Exception\RuntimeException::missingCollectionMetadata($classMetadata);
}

if ($data instanceof Collection) {
$resource = new Resource(new ResourcePaginator($metadata, new CollectionAdapter($data)), $metadata);
} elseif ($data instanceof Selectable) {
$resource = new Resource(new ResourcePaginator($metadata, new SelectableAdapter($data)), $metadata);
}

$controllerName = $collectionMetadata->getControllerName();
} else {
$controllerName = $metadata->getControllerName();
Expand Down
109 changes: 84 additions & 25 deletions tests/ZfrRestTest/Mvc/Router/Http/ResourceGraphRouteFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Http\Request;
use ZfrRest\Factory\ResourceGraphRouteFactory;
use ZfrRest\Http\Exception;
use ZfrRest\Mvc\Router\Http\ResourceGraphRoute;
use ZfrRestTest\Util\ServiceManagerFactory;
Expand All @@ -35,46 +36,104 @@
class ResourceGraphRouteFunctionalTest extends TestCase
{
/**
* Verifies that the resource graph route retrieves the correct metadata
* for an inheritance of classes
* @var \Doctrine\Common\Persistence\ObjectManager
*/
public function testRetrievesChildClassMetadata()
protected $objectManager;

/**
* @var \ZfrRest\Mvc\Router\Http\ResourceGraphRoute
*/
protected $router;

/**
* {@inheritDoc}
*/
protected function setUp()
{
$serviceManager = ServiceManagerFactory::getServiceManager();
$config = $serviceManager->get('Config');
/* @var $entityManager \Doctrine\ORM\EntityManager */
$entityManager = $serviceManager->get('Doctrine\\ORM\\EntityManager');
$request = new Request();

$config['router']['routes']['foo_route'] = array(
'type' => 'ResourceGraphRoute',
'options' => array(
'route' => '/foo/bar/',
'resource' => 'Foo\\Repository',
),
);
$serviceManager = ServiceManagerFactory::getServiceManager();
$this->objectManager = $serviceManager->get('Doctrine\\ORM\\EntityManager');
$routeFactory = new ResourceGraphRouteFactory();

$request->setUri('/foo/bar/');
$serviceManager->setAllowOverride(true);
$serviceManager->setService('Config', $config);
$serviceManager->setService(
'Foo\\Repository',
$entityManager->getRepository('ZfrRestTest\Asset\Annotation\Page')
'ZfrRestTest\Asset\Repository\PageRepository',
$this->objectManager->getRepository('ZfrRestTest\Asset\Annotation\Page')
);

/* @var $router \Zend\Mvc\Router\Http\TreeRouteStack */
$router = $serviceManager->get('HttpRouter');
$routeFactory->setCreationOptions(
array(
'route' => '/foo/bar/',
'resource' => 'ZfrRestTest\Asset\Repository\PageRepository',
)
);

$this->router = $routeFactory->createService($serviceManager->get('RoutePluginManager'));
}

$match = $router->match($request);
/**
* Verifies that the resource graph route retrieves the correct metadata
* for an inheritance of classes
*/
public function testRetrievesChildClassMetadata()
{
$match = $this->router->match($this->createRequest('/foo/bar/'));

$this->assertInstanceOf('Zend\\Mvc\\Router\\RouteMatch', $match);

/* @var $resource \ZfrRest\Resource\ResourceInterface */
$resource = $match->getParam('resource');

$this->assertInstanceOf('ZfrRest\\Resource\\ResourceInterface', $resource);

$this->assertSame('ZfrRestTest\Asset\Annotation\Page', $resource->getMetadata()->getClassName());
}

/**
* Verifies that the resource graph route strips slashes before applying comparisons
*
* @dataProvider checkSlashesProvider
*
* @param string $path
* @param bool $shouldMatch
*/
public function testMatchesSlashes($path, $shouldMatch)
{
$match = $this->router->match($this->createRequest($path));

if ($shouldMatch) {
$this->assertInstanceOf('Zend\\Mvc\\Router\\RouteMatch', $match);
} else {
$this->assertNull($match);
}
}

/**
* @param string $uri
* @param array $query
*
* @return Request
*/
private function createRequest($uri, array $query = array())
{
$request = new Request();

$request->setUri($uri);

foreach ($query as $key => $value) {
$request->getQuery()->set($key, $value);
}

return $request;
}

/**
* @return array
*/
public function checkSlashesProvider()
{
return array(
array('foo/bar', false),
array('/foo/bar', false),
array('foo/bar/', false),
array('/foo/bar/', true),
);
}
}

0 comments on commit 3a26207

Please sign in to comment.