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)); }
/** * @group 79 */ public function testRenderCollectionTriggersEvent() { $collection = new HalCollection(array((object) array('id' => 'foo', 'name' => 'foo'), (object) array('id' => 'bar', 'name' => 'bar'), (object) array('id' => 'baz', 'name' => 'baz')), 'hostname/contacts'); $self = new Link('self'); $self->setRoute('hostname/contacts'); $collection->getLinks()->add($self); $collection->setCollectionName('resources'); $this->plugin->getEventManager()->attach('renderCollection', function ($e) { $collection = $e->getParam('collection'); $collection->setAttributes(array('injected' => true)); }); $rendered = $this->plugin->renderCollection($collection); $this->assertArrayHasKey('injected', $rendered); $this->assertTrue($rendered['injected']); }
public function testIsCompleteReturnsTrueWhenRouteIsSet() { $link = new Link('describedby'); $link->setRoute('api/docs'); $this->assertTrue($link->isComplete()); }
public function setUpChildCollection() { $children = array(array('luke', 'Luke Skywalker'), array('leia', 'Leia Organa')); $this->collection = array(); foreach ($children as $info) { $collection[] = call_user_func_array(array($this, 'setUpChildResource'), $info); } $collection = new HalCollection($this->collection); $collection->setCollectionRoute('parent/child'); $collection->setResourceRoute('parent/child'); $collection->setPage(1); $collection->setPageSize(10); $collection->setCollectionName('child'); $link = new Link('self'); $link->setRoute('parent/child'); $collection->getLinks()->add($link); return $collection; }
/** * Creates a link object, given metadata and a resource * * @param Metadata $metadata * @param object $object * @param string $id * @param string $identifierName * @return Link * @throws Exception\RuntimeException */ protected function marshalSelfLinkFromMetadata(Metadata $metadata, $object, $id, $identifierName) { $link = new Link('self'); if ($metadata->hasUrl()) { $link->setUrl($metadata->getUrl()); return $link; } if (!$metadata->hasRoute()) { throw new Exception\RuntimeException(sprintf('Unable to create a self link for resource of type "%s"; metadata does not contain a route or a url', get_class($object))); } $params = array_merge($metadata->getRouteParams(), array($identifierName => $id)); $link->setRoute($metadata->getRoute(), $params, $metadata->getRouteOptions()); return $link; }
public function testAllowsSpecifyingAlternateCallbackForReturningResourceId() { $this->setUpHelpers(); $this->helpers->get('HalLinks')->getEventManager()->attach('getIdFromResource', function ($e) { $resource = $e->getParam('resource'); if (!is_array($resource)) { return false; } if (array_key_exists('name', $resource)) { return $resource['name']; } return false; }, 10); $prototype = array('foo' => 'bar'); $items = array(); foreach (range(1, 100) as $id) { $item = $prototype; $item['name'] = $id; $items[] = $item; } $collection = new HalCollection($items); $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)); $this->assertRelationalLinkEquals('http://localhost.localdomain/resource', 'self', $test); $this->assertObjectHasAttribute('_embedded', $test); $this->assertInstanceof('stdClass', $test->_embedded); $this->assertObjectHasAttribute('items', $test->_embedded); $this->assertInternalType('array', $test->_embedded->items); $this->assertEquals(100, count($test->_embedded->items)); foreach ($test->_embedded->items as $key => $item) { $id = $key + 1; $this->assertRelationalLinkEquals('http://localhost.localdomain/resource/' . $id, 'self', $item); $this->assertObjectHasAttribute('name', $item, var_export($item, 1)); $this->assertEquals($id, $item->name); $this->assertObjectHasAttribute('foo', $item); $this->assertEquals('bar', $item->foo); } }