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 testSerialize() { $classMetadata = new ClassMetadata('a'); $a1 = $this->getMock('Symfony\\Component\\Serializer\\Mapping\\AttributeMetadataInterface'); $a1->method('getName')->willReturn('b1'); $a2 = $this->getMock('Symfony\\Component\\Serializer\\Mapping\\AttributeMetadataInterface'); $a2->method('getName')->willReturn('b2'); $classMetadata->addAttributeMetadata($a1); $classMetadata->addAttributeMetadata($a2); $serialized = serialize($classMetadata); $this->assertEquals($classMetadata, unserialize($serialized)); }
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; }
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); }
public function testMerge() { $classMetadata1 = new ClassMetadata('c1'); $classMetadata2 = new ClassMetadata('c2'); $ac1 = $this->getMock('Symfony\\Component\\Serializer\\Mapping\\AttributeMetadataInterface'); $ac1->method('getName')->willReturn('a1'); $ac1->method('getGroups')->willReturn(array('a', 'b')); $ac2 = $this->getMock('Symfony\\Component\\Serializer\\Mapping\\AttributeMetadataInterface'); $ac2->method('getName')->willReturn('a1'); $ac2->method('getGroups')->willReturn(array('b', 'c')); $classMetadata1->addAttributeMetadata($ac1); $classMetadata2->addAttributeMetadata($ac2); $classMetadata1->merge($classMetadata2); $ac1->method('getGroups')->willReturn('a', 'b', 'c'); $this->assertEquals(array('a1' => $ac1), $classMetadata2->getAttributesMetadata()); }