/** * @param int $pages * @param int $currentPage * @return Collection */ private function getHalCollection($pages, $currentPage) { $items = []; for ($i = 0; $i < $pages; $i++) { $items[] = []; } $adapter = new ArrayAdapter($items); $collection = new Paginator($adapter); $halCollection = new Collection($collection); $halCollection->setCollectionRoute('foo'); $halCollection->setPage($currentPage); $halCollection->setPageSize(1); return $halCollection; }
public function testRendersEmbeddedEntitiesOfIndividualPaginatedCollections() { $this->setUpHelpers(); $this->router->addRoute('user', new Segment('/user[/:id]')); $child = new Entity(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 Collection($paginator); $collection->setPageSize(5); $collection->setPage(1); $collection->setCollectionRoute('resource'); $collection->setEntityRoute('resource'); $links = $collection->getLinks(); $self = new Link('self'); $self->setRoute('resource'); $links->add($self); $model = new HalJsonModel(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->entity as $key => $value) { $this->assertArrayHasKey($key, $user); $this->assertEquals($value, $user[$key]); } } }
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->setEntityRoute('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; }
public function testRendersEmbeddedEntitiesOfIndividualPaginatedCollections() { $routeClass = class_exists(V2Segment::class) ? V2Segment::class : Segment::class; $this->router->addRoute('user', new $routeClass('/user[/:id]')); $child = new Entity(['id' => 'matthew', 'name' => 'matthew', 'github' => 'weierophinney'], 'matthew'); $link = new Link('self'); $link->setRoute('user')->setRouteParams(['id' => 'matthew']); $child->getLinks()->add($link); $prototype = ['foo' => 'bar', 'user' => $child]; $items = []; foreach (range(1, 3) as $id) { $item = $prototype; $item['id'] = $id; $items[] = $item; } $adapter = new ArrayPaginator($items); $paginator = new Paginator($adapter); $collection = new Collection($paginator); $collection->setPageSize(5); $collection->setPage(1); $collection->setCollectionRoute('resource'); $collection->setEntityRoute('resource'); $links = $collection->getLinks(); $self = new Link('self'); $self->setRoute('resource'); $links->add($self); $result = $this->plugin->renderCollection($collection); $this->assertInternalType('array', $result, var_export($result, 1)); $collection = $result['_embedded']['items']; foreach ($collection as $item) { $this->assertArrayHasKey('_embedded', $item, var_export($item, 1)); $embedded = $item['_embedded']; $this->assertArrayHasKey('user', $embedded); $user = $embedded['user']; $this->assertRelationalLinkContains('/user/matthew', 'self', $user); foreach ($child->getEntity() as $key => $value) { $this->assertArrayHasKey($key, $user); $this->assertEquals($value, $user[$key]); } } }
public function testPageIsMutable() { $hal = new Collection(array(), 'item/route'); $hal->setPage(5); $this->assertEquals(5, $hal->getPage()); }
/** * @group 14 */ public function testRenderingPaginatorCollectionRendersPaginationAttributes() { $set = array(); for ($id = 1; $id <= 100; $id += 1) { $entity = new Entity((object) array('id' => $id, 'name' => 'foo'), 'foo'); $links = $entity->getLinks(); $self = new Link('self'); $self->setRoute('hostname/users', array('id' => $id)); $links->add($self); $set[] = $entity; } $paginator = new Paginator(new ArrayPaginator($set)); $collection = new Collection($paginator); $collection->setCollectionName('users'); $collection->setCollectionRoute('hostname/users'); $collection->setPage(3); $collection->setPageSize(10); $rendered = $this->plugin->renderCollection($collection); $expected = array( '_links', '_embedded', 'page_count', 'page_size', 'total_items', 'page', ); $this->assertEquals($expected, array_keys($rendered)); $this->assertEquals(100, $rendered['total_items']); $this->assertEquals(3, $rendered['page']); $this->assertEquals(10, $rendered['page_count']); $this->assertEquals(10, $rendered['page_size']); return $rendered; }