/** * @param $collection * @param $metadataMap * @param $expectedResult * @param $exception * * @dataProvider renderCollectionWithMaxDepthProvider */ public function testRenderCollectionWithMaxDepth($collection, $metadataMap, $expectedResult, $exception = null) { $this->plugin->setMetadataMap($metadataMap); if ($exception) { $this->setExpectedException($exception['class'], $exception['message']); } if (is_callable($collection)) { $collection = $collection(); } $halCollection = $this->plugin->createCollection($collection); $result = $this->plugin->renderCollection($halCollection); $this->assertEquals($expectedResult, $result); }
public function testAllowsSpecifyingAlternateCallbackForReturningEntityId() { $this->plugin->getEventManager()->attach('getIdFromEntity', function ($e) { $entity = $e->getParam('entity'); if (!is_array($entity)) { return false; } if (array_key_exists('name', $entity)) { return $entity['name']; } return false; }, 10); $prototype = ['foo' => 'bar']; $items = []; foreach (range(1, 100) as $id) { $item = $prototype; $item['name'] = $id; $items[] = $item; } $collection = new Collection($items); $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)); $this->assertRelationalLinkEquals('http://localhost.localdomain/resource', 'self', $result); $this->assertArrayHasKey('_embedded', $result); $this->assertInternalType('array', $result['_embedded']); $this->assertArrayHasKey('items', $result['_embedded']); $this->assertInternalType('array', $result['_embedded']['items']); $this->assertEquals(100, count($result['_embedded']['items'])); foreach ($result['_embedded']['items'] as $key => $item) { $id = $key + 1; $this->assertRelationalLinkEquals('http://localhost.localdomain/resource/' . $id, 'self', $item); $this->assertArrayHasKey('name', $item, var_export($item, 1)); $this->assertEquals($id, $item['name']); $this->assertArrayHasKey('foo', $item); $this->assertEquals('bar', $item['foo']); } }