Skip to content

Commit

Permalink
Merge a0d973c into 067ec09
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Feb 17, 2014
2 parents 067ec09 + a0d973c commit b93bc8e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

namespace ZfrRest\Mvc\Controller\Plugin;

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use ZfrRest\Mvc\Controller\AbstractRestfulController;
use ZfrRest\Mvc\Exception\RuntimeException;
Expand Down Expand Up @@ -55,6 +58,12 @@ public function __invoke($data, ResourceMetadataInterface $resourceMetadata = nu

$resourceMetadata = $resourceMetadata ?: $this->controller->getMatchedResource()->getMetadata();

// When an URI like "/users" is matched, we may receive an ObjectRepository, that is Selectable
// BUT NOT iterable. Therefore we force a match with an empty Criteria for those cases
if ($data instanceof Selectable && !$data instanceof Collection) {
$data = $data->matching(new Criteria());
}

return new ResourceViewModel(new Resource($data, $resourceMetadata));
}
}
21 changes: 21 additions & 0 deletions tests/ZfrRestTest/Mvc/Controller/Plugin/ResourceModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,25 @@ public function testCanCreateResourceModelWithMetadata()
$this->assertSame($data, $resourceModel->getResource()->getData());
$this->assertSame($metadata, $resourceModel->getResource()->getMetadata());
}

public function testForceSelectableToBeCollectionIfNotAlready()
{
$controller = new AbstractRestfulController();
$plugin = new ResourceModel();
$plugin->setController($controller);

$metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface');

$data = $this->getMock('Doctrine\Common\Collections\Selectable');
$data->expects($this->once())
->method('matching')
->with($this->isInstanceOf('Doctrine\Common\Collections\Criteria'))
->will($this->returnValue($this->getMock('Doctrine\Common\Collections\Collection')));

$resourceModel = $plugin($data, $metadata);

$this->assertInstanceOf('ZfrRest\View\Model\ResourceModel', $resourceModel);
//$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $resourceModel->getResource()->getData());
$this->assertSame($metadata, $resourceModel->getResource()->getMetadata());
}
}

0 comments on commit b93bc8e

Please sign in to comment.