Skip to content

Commit c4fa41d

Browse files
committed
One more test
1 parent b949294 commit c4fa41d

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Graphql\Serializer;
15+
16+
use ApiPlatform\Core\Api\IriConverterInterface;
17+
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
18+
use ApiPlatform\Core\Exception\InvalidArgumentException;
19+
use ApiPlatform\Core\Graphql\Serializer\ItemNormalizer;
20+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
22+
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
23+
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
24+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
25+
use PHPUnit\Framework\TestCase;
26+
use Prophecy\Argument;
27+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
28+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
29+
use Symfony\Component\Serializer\SerializerInterface;
30+
31+
/**
32+
* @author Kévin Dunglas <[email protected]>
33+
*/
34+
class ItemNormalizerTest extends TestCase
35+
{
36+
public function testSupportNormalization()
37+
{
38+
$std = new \stdClass();
39+
$dummy = new Dummy();
40+
$dummy->setDescription('hello');
41+
42+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
43+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
44+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
45+
46+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
47+
$resourceClassResolverProphecy->getResourceClass($dummy)->willReturn(Dummy::class)->shouldBeCalled();
48+
$resourceClassResolverProphecy->getResourceClass($std)->willThrow(new InvalidArgumentException())->shouldBeCalled();
49+
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true)->shouldBeCalled();
50+
$resourceClassResolverProphecy->isResourceClass(\stdClass::class)->willReturn(false)->shouldBeCalled();
51+
52+
$normalizer = new ItemNormalizer(
53+
$propertyNameCollectionFactoryProphecy->reveal(),
54+
$propertyMetadataFactoryProphecy->reveal(),
55+
$iriConverterProphecy->reveal(),
56+
$resourceClassResolverProphecy->reveal()
57+
);
58+
59+
$this->assertTrue($normalizer->supportsNormalization($dummy, ItemNormalizer::FORMAT));
60+
$this->assertTrue($normalizer->supportsNormalization($dummy, ItemNormalizer::FORMAT));
61+
$this->assertFalse($normalizer->supportsNormalization($std, ItemNormalizer::FORMAT));
62+
63+
$this->assertTrue($normalizer->supportsDenormalization($dummy, Dummy::class, ItemNormalizer::FORMAT));
64+
$this->assertTrue($normalizer->supportsDenormalization($dummy, Dummy::class, ItemNormalizer::FORMAT));
65+
$this->assertFalse($normalizer->supportsDenormalization($std, \stdClass::class, ItemNormalizer::FORMAT));
66+
}
67+
68+
public function testNormalize()
69+
{
70+
$dummy = new Dummy();
71+
$dummy->setName('hello');
72+
73+
$propertyNameCollection = new PropertyNameCollection(['name']);
74+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
75+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection)->shouldBeCalled();
76+
77+
$propertyMetadataFactory = new PropertyMetadata(null, null, true);
78+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
79+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadataFactory)->shouldBeCalled();
80+
81+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
82+
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummies/1')->shouldBeCalled();
83+
84+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
85+
$resourceClassResolverProphecy->getResourceClass($dummy, null, true)->willReturn(Dummy::class)->shouldBeCalled();
86+
87+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
88+
$serializerProphecy->willImplement(NormalizerInterface::class);
89+
$serializerProphecy->normalize('hello', ItemNormalizer::FORMAT, Argument::type('array'))->willReturn('hello')->shouldBeCalled();
90+
91+
$normalizer = new ItemNormalizer(
92+
$propertyNameCollectionFactoryProphecy->reveal(),
93+
$propertyMetadataFactoryProphecy->reveal(),
94+
$iriConverterProphecy->reveal(),
95+
$resourceClassResolverProphecy->reveal()
96+
);
97+
$normalizer->setSerializer($serializerProphecy->reveal());
98+
99+
$this->assertEquals(['name' => 'hello', '#item' => serialize($dummy)], $normalizer->normalize($dummy, ItemNormalizer::FORMAT, ['resources' => []]));
100+
}
101+
102+
public function testDenormalize()
103+
{
104+
$context = ['resource_class' => Dummy::class, 'api_allow_update' => true];
105+
106+
$propertyNameCollection = new PropertyNameCollection(['name']);
107+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
108+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection)->shouldBeCalled();
109+
110+
$propertyMetadataFactory = new PropertyMetadata(null, null, true, true);
111+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
112+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadataFactory)->shouldBeCalled();
113+
114+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
115+
116+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
117+
118+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
119+
$serializerProphecy->willImplement(DenormalizerInterface::class);
120+
121+
$normalizer = new ItemNormalizer(
122+
$propertyNameCollectionFactoryProphecy->reveal(),
123+
$propertyMetadataFactoryProphecy->reveal(),
124+
$iriConverterProphecy->reveal(),
125+
$resourceClassResolverProphecy->reveal()
126+
);
127+
$normalizer->setSerializer($serializerProphecy->reveal());
128+
129+
$this->assertInstanceOf(Dummy::class, $normalizer->denormalize(['name' => 'hello'], Dummy::class, ItemNormalizer::FORMAT, $context));
130+
}
131+
}

0 commit comments

Comments
 (0)