public function testLinkCollectionMayBeInjected()
 {
     $resource = new stdClass();
     $hal = new HalResource($resource, 'id', 'route', array('foo' => 'bar'));
     $links = new LinkCollection();
     $hal->setLinks($links);
     $this->assertSame($links, $hal->getLinks());
 }
 public function setUpChildResource($id, $name)
 {
     $this->child = (object) array('id' => $id, 'name' => $name);
     $resource = new HalResource($this->child, $id);
     $link = new Link('self');
     $link->setRoute('parent/child');
     $link->setRouteParams(array('child' => $id));
     $resource->getLinks()->add($link);
     return $resource;
 }
 public function halObjects()
 {
     $resource = new HalResource(array('foo' => 'bar'), 'identifier', 'route');
     $link = new Link('self');
     $link->setRoute('resource/route')->setRouteParams(array('id' => 'identifier'));
     $resource->getLinks()->add($link);
     $collection = new HalCollection(array($resource));
     $collection->setCollectionRoute('collection/route');
     $collection->setResourceRoute('resource/route');
     return array('resource' => array($resource), 'collection' => array($collection));
 }
예제 #4
0
 /**
  * @group 79
  */
 public function testRenderResourceTriggersEvent()
 {
     $resource = new HalResource((object) array('id' => 'user', 'name' => 'matthew'), 'user');
     $self = new Link('self');
     $self->setRoute('hostname/users', array('id' => 'user'));
     $resource->getLinks()->add($self);
     $this->plugin->getEventManager()->attach('renderResource', function ($e) {
         $resource = $e->getParam('resource');
         $resource->getLinks()->get('self')->setRouteParams(array('id' => 'matthew'));
     });
     $rendered = $this->plugin->renderResource($resource);
     $this->assertContains('/users/matthew', $rendered['_links']['self']['href']);
 }
예제 #5
0
 /**
  * Create a resource and/or collection based on a metadata map
  *
  * @param  object $object
  * @param  Metadata $metadata
  * @return HalResource|HalCollection
  */
 public function createResourceFromMetadata($object, Metadata $metadata)
 {
     if ($metadata->isCollection()) {
         return $this->createCollectionFromMetadata($object, $metadata);
     }
     if ($metadata->hasHydrator()) {
         $hydrator = $metadata->getHydrator();
     } else {
         $hydrator = $this->getHydratorForResource($object);
     }
     if (!$hydrator instanceof HydratorInterface) {
         throw new Exception\RuntimeException(sprintf('Unable to extract %s; no hydrator registered', get_class($object)));
     }
     $data = $hydrator->extract($object);
     $identiferName = $metadata->getIdentifierName();
     if (!isset($data[$identiferName])) {
         throw new Exception\RuntimeException(sprintf('Unable to determine identifier for object of type "%s"; no fields matching "%s"', get_class($object), $identiferName));
     }
     $id = $data[$identiferName];
     $resource = new HalResource($data, $id);
     $links = $resource->getLinks();
     $this->marshalMetadataLinks($metadata, $links);
     if (!$links->has('self')) {
         $link = $this->marshalSelfLinkFromMetadata($metadata, $object, $id, $identiferName);
         $links->add($link);
     }
     return $resource;
 }
 public function testRendersEmbeddedResourcesOfIndividualPaginatedCollectionResources()
 {
     $this->setUpHelpers();
     $this->router->addRoute('user', new Segment('/user[/:id]'));
     $child = new HalResource(array('id' => 'matthew', 'name' => 'matthew', 'github' => 'weierophinney'), 'matthew', 'user');
     $link = new Link('self');
     $link->setRoute('user')->setRouteParams(array('id' => 'matthew'));
     $child->getLinks()->add($link);
     $prototype = array('foo' => 'bar', 'user' => $child);
     $items = array();
     foreach (range(1, 3) as $id) {
         $item = $prototype;
         $item['id'] = $id;
         $items[] = $item;
     }
     $adapter = new ArrayAdapter($items);
     $paginator = new Paginator($adapter);
     $collection = new HalCollection($paginator);
     $collection->setPageSize(5);
     $collection->setPage(1);
     $collection->setCollectionRoute('resource');
     $collection->setResourceRoute('resource');
     $links = $collection->getLinks();
     $self = new Link('self');
     $self->setRoute('resource');
     $links->add($self);
     $model = new RestfulJsonModel(array('payload' => $collection));
     $test = $this->renderer->render($model);
     $test = json_decode($test);
     $this->assertInstanceof('stdClass', $test, var_export($test, 1));
     $collection = $test->_embedded->items;
     foreach ($collection as $item) {
         $this->assertObjectHasAttribute('_embedded', $item, var_export($item, 1));
         $embedded = $item->_embedded;
         $this->assertObjectHasAttribute('user', $embedded);
         $user = $embedded->user;
         $this->assertRelationalLinkContains('/user/matthew', 'self', $user);
         $user = (array) $user;
         foreach ($child->resource as $key => $value) {
             $this->assertArrayHasKey($key, $user);
             $this->assertEquals($value, $user[$key]);
         }
     }
 }