public function testSerialize()
 {
     $attributeMetadata = new AttributeMetadata('attribute');
     $attributeMetadata->addGroup('a');
     $attributeMetadata->addGroup('b');
     $serialized = serialize($attributeMetadata);
     $this->assertEquals($attributeMetadata, unserialize($serialized));
 }
 public function testMerge()
 {
     $attributeMetadata1 = new AttributeMetadata('a1');
     $attributeMetadata1->addGroup('a');
     $attributeMetadata1->addGroup('b');
     $attributeMetadata2 = new AttributeMetadata('a2');
     $attributeMetadata2->addGroup('a');
     $attributeMetadata2->addGroup('c');
     $attributeMetadata1->merge($attributeMetadata2);
     $this->assertEquals(array('a', 'b', 'c'), $attributeMetadata1->getGroups());
 }
 public function testCreate()
 {
     $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
     $dummyResourceMetadata = (new ResourceMetadata())->withAttributes(['normalization_context' => ['groups' => ['dummy_read']], 'denormalization_context' => ['groups' => ['dummy_write']]]);
     $resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn($dummyResourceMetadata)->shouldBeCalled();
     $resourceMetadataFactoryProphecy->create(RelatedDummy::class)->willReturn(new ResourceMetadata())->shouldBeCalled();
     $resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();
     $serializerClassMetadataFactoryProphecy = $this->prophesize(SerializerClassMetadataFactoryInterface::class);
     $dummySerializerClassMetadata = new SerializerClassMetadata(Dummy::class);
     $fooSerializerAttributeMetadata = new SerializerAttributeMetadata('foo');
     $fooSerializerAttributeMetadata->addGroup('dummy_read');
     $fooSerializerAttributeMetadata->addGroup('dummy_write');
     $dummySerializerClassMetadata->addAttributeMetadata($fooSerializerAttributeMetadata);
     $relatedDummySerializerAttributeMetadata = new SerializerAttributeMetadata('relatedDummy');
     $relatedDummySerializerAttributeMetadata->addGroup('dummy_read');
     $relatedDummySerializerAttributeMetadata->addGroup('dummy_write');
     $dummySerializerClassMetadata->addAttributeMetadata($relatedDummySerializerAttributeMetadata);
     $nameConvertedSerializerAttributeMetadata = new SerializerAttributeMetadata('nameConverted');
     $dummySerializerClassMetadata->addAttributeMetadata($nameConvertedSerializerAttributeMetadata);
     $serializerClassMetadataFactoryProphecy->getMetadataFor(Dummy::class)->willReturn($dummySerializerClassMetadata)->shouldBeCalled();
     $relatedDummySerializerClassMetadata = new SerializerClassMetadata(RelatedDummy::class);
     $idSerializerAttributeMetadata = new SerializerAttributeMetadata('id');
     $idSerializerAttributeMetadata->addGroup('dummy_read');
     $relatedDummySerializerClassMetadata->addAttributeMetadata($idSerializerAttributeMetadata);
     $nameSerializerAttributeMetadata = new SerializerAttributeMetadata('name');
     $nameSerializerAttributeMetadata->addGroup('dummy_read');
     $relatedDummySerializerClassMetadata->addAttributeMetadata($nameSerializerAttributeMetadata);
     $serializerClassMetadataFactoryProphecy->getMetadataFor(RelatedDummy::class)->willReturn($relatedDummySerializerClassMetadata)->shouldBeCalled();
     $serializerClassMetadataFactory = $serializerClassMetadataFactoryProphecy->reveal();
     $decoratedProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
     $fooPropertyMetadata = (new PropertyMetadata())->withType(new Type(Type::BUILTIN_TYPE_ARRAY, true))->withReadable(false)->withWritable(true);
     $decoratedProphecy->create(Dummy::class, 'foo', [])->willReturn($fooPropertyMetadata)->shouldBeCalled();
     $relatedDummyPropertyMetadata = (new PropertyMetadata())->withType(new Type(Type::BUILTIN_TYPE_OBJECT, true, RelatedDummy::class));
     $decoratedProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn($relatedDummyPropertyMetadata)->shouldBeCalled();
     $nameConvertedPropertyMetadata = (new PropertyMetadata())->withType(new Type(Type::BUILTIN_TYPE_STRING, true));
     $decoratedProphecy->create(Dummy::class, 'nameConverted', [])->willReturn($nameConvertedPropertyMetadata)->shouldBeCalled();
     $decorated = $decoratedProphecy->reveal();
     $serializerPropertyMetadataFactory = new SerializerPropertyMetadataFactory($resourceMetadataFactory, $serializerClassMetadataFactory, $decorated);
     $actual = [];
     $actual[] = $serializerPropertyMetadataFactory->create(Dummy::class, 'foo');
     $actual[] = $serializerPropertyMetadataFactory->create(Dummy::class, 'relatedDummy');
     $actual[] = $serializerPropertyMetadataFactory->create(Dummy::class, 'nameConverted');
     $this->assertInstanceOf(PropertyMetadata::class, $actual[0]);
     $this->assertFalse($actual[0]->isReadable());
     $this->assertTrue($actual[0]->isWritable());
     $this->assertInstanceOf(PropertyMetadata::class, $actual[1]);
     $this->assertTrue($actual[1]->isReadable());
     $this->assertTrue($actual[1]->isWritable());
     $this->assertTrue($actual[1]->isReadableLink());
     $this->assertFalse($actual[1]->isWritableLink());
     $this->assertInstanceOf(PropertyMetadata::class, $actual[2]);
     $this->assertFalse($actual[2]->isReadable());
     $this->assertFalse($actual[2]->isWritable());
 }
 public function testGetAllowedAttributesAsObjects()
 {
     $classMetadata = new ClassMetadata('c');
     $a1 = new AttributeMetadata('a1');
     $classMetadata->addAttributeMetadata($a1);
     $a2 = new AttributeMetadata('a2');
     $a2->addGroup('test');
     $classMetadata->addAttributeMetadata($a2);
     $a3 = new AttributeMetadata('a3');
     $a3->addGroup('other');
     $classMetadata->addAttributeMetadata($a3);
     $a4 = new AttributeMetadata('a4');
     $a4->addGroup('test');
     $a4->addGroup('other');
     $classMetadata->addAttributeMetadata($a4);
     $this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
     $result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('test')), false);
     $this->assertEquals(array($a2, $a4), $result);
     $result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('other')), false);
     $this->assertEquals(array($a3, $a4), $result);
 }
Exemplo n.º 5
0
 public static function createXmlCLassMetadata()
 {
     $expected = new ClassMetadata('Symfony\\Component\\Serializer\\Tests\\Fixtures\\GroupDummy');
     $foo = new AttributeMetadata('foo');
     $foo->addGroup('group1');
     $foo->addGroup('group2');
     $expected->addAttributeMetadata($foo);
     $bar = new AttributeMetadata('bar');
     $bar->addGroup('group2');
     $expected->addAttributeMetadata($bar);
     return $expected;
 }