Exemplo n.º 1
0
 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');
     $properties = $collection->getProperties();
     $idProperty = new Property('id');
     $idProperty->setRoute('resource');
     $properties->add($idProperty);
     $result = $this->plugin->renderCollection($collection);
     $this->assertInternalType('array', $result, var_export($result, 1));
     $this->assertPropertyEquals('http://localhost.localdomain/resource', 'id', $result);
     $this->assertArrayHasKey('member', $result);
     $this->assertInternalType('array', $result['member']);
     $this->assertEquals(100, count($result['member']));
     foreach ($result['member'] as $key => $item) {
         $id = $key + 1;
         $this->assertPropertyEquals('http://localhost.localdomain/resource/' . $id, 'id', $item);
         $this->assertArrayHasKey('name', $item, var_export($item, 1));
         $this->assertEquals($id, $item['name']);
         $this->assertArrayHasKey('foo', $item);
         $this->assertEquals('bar', $item['foo']);
     }
 }