Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow generate link for itself #117

Merged
merged 7 commits into from
Jan 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/ZfrRest/Mvc/Controller/MethodHandler/PostHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use ZfrRest\Http\Exception\Client\MethodNotAllowedException;
use ZfrRest\Options\ControllerBehavioursOptions;
use ZfrRest\Resource\ResourceInterface;
use ZfrRest\View\Model\ResourceModel;

/**
* Handler for the POST method verb
Expand Down Expand Up @@ -99,10 +100,11 @@ public function handleMethod(AbstractController $controller, ResourceInterface $
$result = $controller->post($data, $singleResource->getMetadata());

// Set the Location header with the URL of the newly created resource
if (is_object($result)) {
// @TODO: when we have a functional router, we should be able to automatically set the Location
// header with the URI of the newly created resource
$controller->getResponse()->setStatusCode(201);
if ($result instanceof ResourceModel) {
$location = $controller->url()->fromRoute(null, ['resource' => $result->getResource()]);

$controller->getResponse()->setStatusCode(201)
->getHeaders()->addHeaderLine('Location', $location);
}

return $result;
Expand Down
36 changes: 34 additions & 2 deletions src/ZfrRest/Router/Http/ResourceGraphRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,41 @@ public function __construct(MetadataFactory $metadataFactory, BaseSubPathMatcher
*/
public function assemble(array $params = [], array $options = [])
{
// @TODO: not sure about how to do this correctly...
$trimmedRoute = trim($this->route, '/');

throw new RuntimeException('ResourceGraphRoute does not support yet assembling route');
// If no resource it's equal to "self"
if (!isset($params['resource'])) {
return '/' . $trimmedRoute;
}

/* @var \ZfrRest\Resource\ResourceInterface $resource */
$resource = $params['resource'];
$resourceMetadata = $resource->getMetadata();

$classMetadata = $resourceMetadata->getClassMetadata();
$identifiers = $classMetadata->getIdentifierValues($resource->getData());

if (count($identifiers) > 1) {
throw new RuntimeException('ZfrRest assembling does not support composite identifiers');
}

$route = '/' . $trimmedRoute . '/' . current($identifiers);

if (!isset($params['association'])) {
return $route;
}

if (!$resourceMetadata->hasAssociationMetadata($params['association'])) {
throw new RuntimeException(sprintf(
'You are trying to generate a URL for the association "%s", which does not exist or is not exposed',
$params['association']
));
}

$associationMetadata = $resourceMetadata->getAssociationMetadata($params['association']);
$route .= '/' . $associationMetadata['path'];

return $route;
}

/**
Expand Down
73 changes: 72 additions & 1 deletion tests/ZfrRestTest/Router/Http/ResourceGraphRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,80 @@ public function testReturnNullIfNotAHttpRequest()
$this->metadataFactory,
$this->baseSubPathMatcher,
new \stdClass(),
'route'
'/route'
);

$this->assertNull($resourceGraphRoute->match($this->getMock('Zend\Stdlib\RequestInterface')));
}

public function testCanAssembleWithoutResource()
{
$resourceGraphRoute = new ResourceGraphRoute(
$this->metadataFactory,
$this->baseSubPathMatcher,
new \stdClass(),
'/route'
);

$this->assertEquals('/route', $resourceGraphRoute->assemble());
}

public function testCanAssembleWithResource()
{
$resourceGraphRoute = new ResourceGraphRoute(
$this->metadataFactory,
$this->baseSubPathMatcher,
new \stdClass(),
'/route'
);

$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
$classMetadata->expects($this->once())
->method('getIdentifierValues')
->will($this->returnValue(['id' => 2]));

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

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

$this->assertEquals('/route/2', $resourceGraphRoute->assemble(['resource' => $resource]));
}

public function testCanAssembleWithResourceAndAssociation()
{
$resourceGraphRoute = new ResourceGraphRoute(
$this->metadataFactory,
$this->baseSubPathMatcher,
new \stdClass(),
'/route'
);

$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
$classMetadata->expects($this->once())
->method('getIdentifierValues')
->will($this->returnValue(['id' => 2]));

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

$metadata->expects($this->once())
->method('hasAssociationMetadata')
->with('tweets')
->will($this->returnValue(true));

$metadata->expects($this->once())
->method('getAssociationMetadata')
->with('tweets')
->will($this->returnValue(['path' => 'tweets']));

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

$this->assertEquals('/route/2/tweets', $resourceGraphRoute->assemble([
'resource' => $resource,
'association' => 'tweets'
]));
}
}