Ejemplo n.º 1
0
    /**
     * Test that the convertEntityToArray() caches serialization results by object.
     *
     * This is done because if you call createEntity() -- say, from a ZF\Rest\RestController,
     * you may end up calling convertEntityToArray() twice -- once to create the HAL
     * entity with the appropriate identifier, and another when creating the serialized
     * representation.
     *
     * This method is testing internals of the plugin; realistically, the behavior is
     * transparent to the end-user.
     *
     * @group 33
     */
    public function testConvertEntityToArrayCachesSerialization()
    {
        $metadata = new MetadataMap(array(
            'ZFTest\Hal\Plugin\TestAsset\Entity' => array(
                'hydrator'   => 'Zend\Stdlib\Hydrator\ObjectProperty',
                'route_name' => 'hostname/resource',
                'route_identifier_name' => 'id',
                'entity_identifier_name' => 'id',
            ),
        ));
        $this->plugin->setMetadataMap($metadata);

        $foo = new TestAsset\Entity('foo', 'Foo Bar');

        $entity1 = $this->plugin->createEntityFromMetadata($foo, $metadata->get($foo));
        $serialized1 = $this->plugin->renderEntity($entity1);

        $entity2 = $this->plugin->createEntityFromMetadata($foo, $metadata->get($foo));
        $serialized2 = $this->plugin->renderEntity($entity2);

        $this->assertSame($serialized1, $serialized2);

        $data = $serialized1;
        unset($data['_links']);

        $r = new ReflectionObject($this->plugin);
        $p = $r->getProperty('serializedEntities');
        $p->setAccessible(true);
        $serializedEntities = $p->getValue($this->plugin);
        $this->assertInstanceOf('SplObjectStorage', $serializedEntities);
        $this->assertTrue($serializedEntities->contains($foo));
        $this->assertSame($data, $serializedEntities[$foo]);
    }
Ejemplo n.º 2
0
 public function testCreateEntityFromMetadataWithoutForcedSelfLinks()
 {
     $object = new TestAsset\Entity('foo', 'Foo');
     $metadata = new MetadataMap(['ZFTest\\Hal\\Plugin\\TestAsset\\Entity' => ['hydrator' => 'Zend\\Hydrator\\ObjectProperty', 'route_name' => 'hostname/resource', 'links' => [], 'force_self_link' => false]]);
     $this->plugin->setMetadataMap($metadata);
     $entity = $this->plugin->createEntityFromMetadata($object, $metadata->get('ZFTest\\Hal\\Plugin\\TestAsset\\Entity'));
     $links = $entity->getLinks();
     $this->assertFalse($links->has('self'));
 }