Skip to content

Commit

Permalink
Merge aba6c07 into e4ea55d
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Feb 16, 2014
2 parents e4ea55d + aba6c07 commit a74bb07
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 29 deletions.
3 changes: 2 additions & 1 deletion config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@

'controller_plugins' => [
'invokables' => [
'paginatorWrapper' => 'ZfrRest\Mvc\Controller\Plugin\PaginatorWrapper'
'paginatorWrapper' => 'ZfrRest\Mvc\Controller\Plugin\PaginatorWrapper',
'resourceModel' => 'ZfrRest\Mvc\Controller\Plugin\ResourceModel'
]
],

Expand Down
16 changes: 11 additions & 5 deletions src/ZfrRest/Mvc/Controller/AbstractRestfulController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
use ZfrRest\Http\Exception\Client\NotFoundException;
use ZfrRest\Mvc\Controller\MethodHandler\MethodHandlerPluginManager;
use ZfrRest\Mvc\Exception\RuntimeException;
use ZfrRest\Resource\ResourceInterface;

/**
* @author Michaël Gallego <mic.gallego@gmail.com>
* @licence MIT
*
*
* @method \Zend\Paginator\Paginator paginatorWrapper(\Doctrine\Common\Collections\Collection $data, $criteria = [])
*/
class AbstractRestfulController extends AbstractController
Expand Down Expand Up @@ -64,11 +65,8 @@ public function onDispatch(MvcEvent $event)
$request = $this->getRequest();
$handler = $this->getMethodHandlerManager()->get($request->getMethod());

/* @var \ZfrRest\Resource\ResourceInterface $resource */
$resource = $event->getRouteMatch()->getParam('resource', null);

// We should always have a resource, otherwise throw an 404 exception
if (null === $resource) {
if (!$resource = $this->getMatchedResource()) {
throw new NotFoundException();
}

Expand All @@ -78,6 +76,14 @@ public function onDispatch(MvcEvent $event)
return $result;
}

/**
* @return ResourceInterface
*/
public function getMatchedResource()
{
return $this->getEvent()->getRouteMatch()->getParam('resource', null);
}

/**
* Get the method handler plugin manager
*
Expand Down
2 changes: 1 addition & 1 deletion src/ZfrRest/Mvc/Controller/MethodHandler/DeleteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function handleMethod(AbstractController $controller, ResourceInterface $
throw new MethodNotAllowedException();
}

$result = $controller->delete($resource->getData(), $resource->getMetadata());
$result = $controller->delete($resource->getData());

// According to http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7, status code should
// be 204 if nothing is returned
Expand Down
2 changes: 1 addition & 1 deletion src/ZfrRest/Mvc/Controller/MethodHandler/GetHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public function handleMethod(AbstractController $controller, ResourceInterface $
throw new MethodNotAllowedException();
}

return $controller->get($resource->getData(), $resource->getMetadata());
return $controller->get($resource->getData());
}
}
2 changes: 1 addition & 1 deletion src/ZfrRest/Mvc/Controller/MethodHandler/PostHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function handleMethod(AbstractController $controller, ResourceInterface $
$data = $this->validateData($singleResource, $data);
$data = $this->hydrateData($singleResource, $data);

$result = $controller->post($data, $singleResource->getMetadata());
$result = $controller->post($data);

// Set the Location header with the URL of the newly created resource
if ($result instanceof ResourceModel) {
Expand Down
2 changes: 1 addition & 1 deletion src/ZfrRest/Mvc/Controller/MethodHandler/PutHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function handleMethod(AbstractController $controller, ResourceInterface $
$data = $this->validateData($resource, $data);
$data = $this->hydrateData($resource, $data);

return $controller->put($data, $resource->getMetadata());
return $controller->put($data);
}

/**
Expand Down
60 changes: 60 additions & 0 deletions src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace ZfrRest\Mvc\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use ZfrRest\Mvc\Controller\AbstractRestfulController;
use ZfrRest\Mvc\Exception\RuntimeException;
use ZfrRest\Resource\Metadata\ResourceMetadataInterface;
use ZfrRest\Resource\Resource;
use ZfrRest\View\Model\ResourceModel as ViewResourceModel;

/**
* Controller plugin that allows to create a resource model quickly.
*
* @author Michaël Gallego <mic.gallego@gmail.com>
* @licence MIT
*/
class ResourceModel extends AbstractPlugin
{
/**
* Create a resource model from the data
*
* If no resource metadata interface is passed, it will fetched it from matched resource of
* the controller (which is what you want 99% of the time).
*
* @param mixed $data
* @param ResourceMetadataInterface|null $resourceMetadata
* @return ViewResourceModel
* @throws RuntimeException
*/
public function __invoke($data, ResourceMetadataInterface $resourceMetadata = null)
{
if (!$this->controller instanceof AbstractRestfulController) {
throw new RuntimeException(
'You tried to use the ResourceModel controller plugin on a controller instance that does
not extend "ZfrRest\Mvc\Controller\AbstractRestfulController"'
);
}

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

return new ViewResourceModel(new Resource($data, $resourceMetadata));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function testThrowNotFoundExceptionIfNoResourceIsMatched()
$event->setRouteMatch($routeMatch);

$controller = new AbstractRestfulController();
$controller->setEvent($event);

$serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
$pluginManager = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
Expand Down Expand Up @@ -76,6 +77,7 @@ public function testCanSetResultIfResource()
$pluginManager = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');

$controller->setServiceLocator($serviceLocator);
$controller->setEvent($event);

$serviceLocator->expects($this->once())
->method('get')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,9 @@ public function testCanReturnData()
->method('getData')
->will($this->returnValue($data));

$metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface');
$resource->expects($this->once())
->method('getMetadata')
->will($this->returnValue($metadata));

$controller->expects($this->once())
->method('delete')
->with($data, $metadata)
->with($data)
->will($this->returnValue(['foo' => 'bar']));

$controller->expects($this->never())
Expand All @@ -84,14 +79,9 @@ public function testSetProperStatusCodeIfNothingIsReturnedFromDeleteMethod()
->method('getData')
->will($this->returnValue($data));

$metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface');
$resource->expects($this->once())
->method('getMetadata')
->will($this->returnValue($metadata));

$controller->expects($this->once())
->method('delete')
->with($data, $metadata)
->with($data)
->will($this->returnValue(null));

$response = new HttpResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,9 @@ public function testCanReturnData()
->method('getData')
->will($this->returnValue($data));

$metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface');
$resource->expects($this->once())
->method('getMetadata')
->will($this->returnValue($metadata));

$controller->expects($this->once())
->method('get')
->with($data, $metadata)
->with($data)
->will($this->returnValue(['foo' => 'bar']));

$controller->expects($this->never())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @group Coverage
* @covers \ZfrRest\Mvc\Controller\Plugin\PaginatorWrapper
*/
class AbstractRestfulControllerTest extends PHPUnit_Framework_TestCase
class PaginatorWrapperTest extends PHPUnit_Framework_TestCase
{
public function testCanCreatePaginatorFromCollection()
{
Expand Down
61 changes: 61 additions & 0 deletions tests/ZfrRestTest/Mvc/Controller/Plugin/ResourceModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace ZfrRestTest\Mvc\Controller\Plugin;

use PHPUnit_Framework_TestCase;
use ZfrRest\Mvc\Controller\AbstractRestfulController;
use ZfrRest\Mvc\Controller\Plugin\ResourceModel;

/**
* @licence MIT
* @author Michaël Gallego <mic.gallego@gmail.com>
*
* @group Coverage
* @covers \ZfrRest\Mvc\Controller\Plugin\ResourceModel
*/
class ResourceModelTest extends PHPUnit_Framework_TestCase
{
public function testThrowExceptionIfNotAbstractRestfulController()
{
$this->setExpectedException('ZfrRest\Mvc\Exception\RuntimeException');

$plugin = new ResourceModel();
$plugin(new \stdClass());
}

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

$reflClass = $this->getMock('ReflectionClass', [], [], '', false);
$reflClass->expects($this->once())->method('isInstance')->will($this->returnValue(true));

$metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface');
$metadata->expects($this->once())->method('getReflectionClass')->will($this->returnValue($reflClass));

$data = new \stdClass();
$resourceModel = $plugin($data, $metadata);

$this->assertInstanceOf('ZfrRest\View\Model\ResourceModel', $resourceModel);
$this->assertSame($data, $resourceModel->getResource()->getData());
$this->assertSame($metadata, $resourceModel->getResource()->getMetadata());
}
}

0 comments on commit a74bb07

Please sign in to comment.