Skip to content

Commit

Permalink
Add extraction constants
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Mar 16, 2014
1 parent 3f374e0 commit c37feaf
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/ZfrRest/Resource/ResourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
*/
interface ResourceInterface
{
/**
* Extraction constants that define how extraction associations are rendered
*/
const ASSOCIATION_EXTRACTION_NONE = 'NONE';
const ASSOCIATION_EXTRACTION_EMBED = 'EMBED';
const ASSOCIATION_EXTRACTION_ID = 'ID';

/**
* Get the resource metadata
*
Expand Down
11 changes: 6 additions & 5 deletions src/ZfrRest/View/Renderer/DefaultResourceRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use Zend\Paginator\Paginator;
use Zend\Stdlib\Hydrator\HydratorPluginManager;
use ZfrRest\Exception\RuntimeException;
use ZfrRest\Resource\Metadata\ResourceMetadataFactory;
use ZfrRest\Resource\Metadata\ResourceMetadataInterface;
use ZfrRest\Resource\Resource;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function __construct(
public function render($nameOrModel, $values = null)
{
if (!$nameOrModel instanceof ResourceModel) {
return;
throw new RuntimeException('Resource renderer expect a ResourceModel instance');
}

$resource = $nameOrModel->getResource();
Expand All @@ -80,7 +81,7 @@ public function render($nameOrModel, $values = null)
$payload = $this->renderItem($data, $resourceMetadata);
}

$payload = array_merge($payload, $this->renderMeta($resource));
$payload = array_merge($this->renderMeta($resource), $payload);

return $payload;
}
Expand Down Expand Up @@ -129,7 +130,7 @@ protected function renderAssociations(array $data, ResourceMetadataInterface $re
$extractionStrategy = $associationMetadata['extraction'];

// If set to NONE, we don't even want the association to be in the payload
if ($extractionStrategy === 'NONE') {
if ($extractionStrategy === ResourceInterface::ASSOCIATION_EXTRACTION_NONE) {
unset($data[$association]);
continue;
}
Expand Down Expand Up @@ -168,7 +169,7 @@ protected function renderAssociation($object, $extractionStrategy, $isCollection
$association = null;

switch($extractionStrategy) {
case 'ID':
case ResourceInterface::ASSOCIATION_EXTRACTION_ID:
$identifiers = [];

foreach ($object as $datum) {
Expand All @@ -179,7 +180,7 @@ protected function renderAssociation($object, $extractionStrategy, $isCollection
$association = $identifiers;
break;

case 'EMBED':
case ResourceInterface::ASSOCIATION_EXTRACTION_EMBED:
$embedded = [];

foreach ($object as $datum) {
Expand Down
6 changes: 3 additions & 3 deletions tests/ZfrRestTest/Asset/Resource/Metadata/Annotation/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class User
* @ORM\OneToMany(targetEntity="Tweet")
* @REST\Association(extraction="ID")
*/
protected $tweets;
//protected $tweets;

public function __construct()
{
Expand Down Expand Up @@ -116,8 +116,8 @@ public function addTweet(Tweet $tweet)
/**
* @return ArrayCollection
*/
public function getTweets()
/*public function getTweets()
{
return $this->tweets;
}
}*/
}
141 changes: 141 additions & 0 deletions tests/ZfrRestTest/View/Renderer/DefaultResourceRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,145 @@ public function testCanRenderSingleResourceWithAssociationAsEmbed()

$this->assertEquals($expectedPayload, $payload);
}

public function testCanRenderCollectionResourceWithoutAssociation()
{
$address1 = new Address();
$address2 = new Address();

$user1 = new User();
$user1->setId(2);
$user1->setUsername('bakura');
$user1->setAddress($address1);

$user2 = new User();
$user2->setId(3);
$user2->setUsername('ocramius');
$user2->setAddress($address2);

$metadata = $this->resourceMetadataFactory->getMetadataForClass(
'ZfrRestTest\Asset\Resource\Metadata\Annotation\User'
);

// In this test, we enforce that association extraction is set to NONE
$metadata->propertyMetadata['associations']['address']['extraction'] = 'NONE';

$resourceModel = new ResourceModel(new Resource([$user1, $user2], $metadata));
$payload = $this->resourceRenderer->render($resourceModel);

$expectedPayload = [
'items' => [
[
'id' => 2,
'username' => 'bakura'
],
[
'id' => 3,
'username' => 'ocramius'
]
]
];

$this->assertEquals($expectedPayload, $payload);
}

public function testCanRenderCollectionResourceWithAssociationAsId()
{
$address1 = new Address();
$address1->setId(45);

$address2 = new Address();
$address2->setId(344);

$user1 = new User();
$user1->setId(2);
$user1->setUsername('bakura');
$user1->setAddress($address1);

$user2 = new User();
$user2->setId(3);
$user2->setUsername('ocramius');
$user2->setAddress($address2);

$metadata = $this->resourceMetadataFactory->getMetadataForClass(
'ZfrRestTest\Asset\Resource\Metadata\Annotation\User'
);

// In this test, we enforce that association extraction is set to ID
$metadata->propertyMetadata['associations']['address']['extraction'] = 'ID';

$resourceModel = new ResourceModel(new Resource([$user1, $user2], $metadata));
$payload = $this->resourceRenderer->render($resourceModel);

$expectedPayload = [
'items' => [
[
'id' => 2,
'username' => 'bakura',
'address' => 45
],
[
'id' => 3,
'username' => 'ocramius',
'address' => 344
]
]
];

$this->assertEquals($expectedPayload, $payload);
}

public function testCanRenderCollectionResourceWithAssociationAsEmbed()
{
$address1 = new Address();
$address1->setId(45);
$address1->setCountry('France');

$address2 = new Address();
$address2->setId(344);
$address2->setCountry('Italia');

$user1 = new User();
$user1->setId(2);
$user1->setUsername('bakura');
$user1->setAddress($address1);

$user2 = new User();
$user2->setId(3);
$user2->setUsername('ocramius');
$user2->setAddress($address2);

$metadata = $this->resourceMetadataFactory->getMetadataForClass(
'ZfrRestTest\Asset\Resource\Metadata\Annotation\User'
);

// In this test, we enforce that association extraction is set to EMBED
$metadata->propertyMetadata['associations']['address']['extraction'] = 'EMBED';

$resourceModel = new ResourceModel(new Resource([$user1, $user2], $metadata));
$payload = $this->resourceRenderer->render($resourceModel);

$expectedPayload = [
'items' => [
[
'id' => 2,
'username' => 'bakura',
'address' => [
'id' => 45,
'country' => 'France'
]
],
[
'id' => 3,
'username' => 'ocramius',
'address' => [
'id' => 344,
'country' => 'Italia'
]
]
]
];

$this->assertEquals($expectedPayload, $payload);
}
}

0 comments on commit c37feaf

Please sign in to comment.