Skip to content

Commit cffda00

Browse files
author
Amrouche Hamza
committed
fix: add itemNormalizerTest
1 parent 21ade42 commit cffda00

File tree

2 files changed

+144
-15
lines changed

2 files changed

+144
-15
lines changed

tests/Hydra/Serializer/CollectionNormalizerTest.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Core\DataProvider\PaginatorInterface;
1717
use ApiPlatform\Core\Hydra\Serializer\CollectionNormalizer;
1818
use ApiPlatform\Core\JsonLd\ContextBuilderInterface;
19+
use ApiPlatform\Core\Serializer\AbstractItemNormalizer;
1920
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2021
use Symfony\Component\Serializer\SerializerInterface;
2122

@@ -30,8 +31,8 @@ public function testSupportsNormalize()
3031
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
3132
$iriConvert = $this->prophesize(IriConverterInterface::class);
3233
$contextBuilder = $this->prophesize(ContextBuilderInterface::class);
33-
$contextBuilder->getResourceContextUri("Foo")->willReturn('/contexts/Foo');
34-
$iriConvert->getIriFromResourceClass("Foo")->willReturn('/foos');
34+
$contextBuilder->getResourceContextUri('Foo')->willReturn('/contexts/Foo');
35+
$iriConvert->getIriFromResourceClass('Foo')->willReturn('/foos');
3536

3637
$normalizer = new CollectionNormalizer($contextBuilder->reveal(), $resourceClassResolverProphecy->reveal(), $iriConvert->reveal());
3738

@@ -44,20 +45,18 @@ public function testSupportsNormalize()
4445
public function testNormalizeApiSubLevel()
4546
{
4647
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
47-
$resourceClassResolverProphecy->getResourceClass(["foo" => "bar"], null, true)->willReturn('Foo')->shouldBeCalled();
48+
$resourceClassResolverProphecy->getResourceClass(['foo' => 'bar'], null, true)->willReturn('Foo')->shouldBeCalled();
4849

49-
$serializer = $this->prophesize(SerializerInterface::class);
50-
$serializer->willImplement(NormalizerInterface::class);
51-
$serializer->normalize('bar', null, ["jsonld_has_context" => true, "jsonld_sub_level" => true, "resource_class" => "Foo"])->willReturn(22);
5250
$iriConvert = $this->prophesize(IriConverterInterface::class);
5351
$contextBuilder = $this->prophesize(ContextBuilderInterface::class);
54-
$contextBuilder->getResourceContextUri("Foo")->willReturn('/contexts/Foo');
55-
$iriConvert->getIriFromResourceClass("Foo")->willReturn('/foo/1');
52+
$contextBuilder->getResourceContextUri('Foo')->willReturn('/contexts/Foo');
53+
$iriConvert->getIriFromResourceClass('Foo')->willReturn('/foo/1');
54+
$itemNormalizer = $this->prophesize(AbstractItemNormalizer::class);
55+
$itemNormalizer->normalize('bar', null, ['jsonld_has_context' => true, 'api_sub_level' => true, 'resource_class' => 'Foo'])->willReturn(22);
5656

5757
$normalizer = new CollectionNormalizer($contextBuilder->reveal(), $resourceClassResolverProphecy->reveal(), $iriConvert->reveal());
5858

59-
$normalizer->setSerializer($serializer->reveal());
60-
59+
$normalizer->setNormalizer($itemNormalizer->reveal());
6160
$this->assertEquals(['@context' => '/contexts/Foo', '@id' => '/foo/1', '@type' => 'hydra:Collection', 'hydra:member' => [0 => '22'], 'hydra:totalItems' => 1], $normalizer->normalize(['foo' => 'bar'], null));
6261
}
6362

@@ -78,15 +77,15 @@ public function testNormalizePaginator()
7877
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
7978
$resourceClassResolverProphecy->getResourceClass($paginator, null, true)->willReturn('Foo')->shouldBeCalled();
8079

81-
$serializer->normalize('foo', null, ["jsonld_has_context" => true, "jsonld_sub_level" => true, "resource_class" => "Foo"])->willReturn(['name' => 'Kévin', 'friend' => 'Smail']);
8280
$iriConvert = $this->prophesize(IriConverterInterface::class);
8381
$contextBuilder = $this->prophesize(ContextBuilderInterface::class);
84-
$contextBuilder->getResourceContextUri("Foo")->willReturn('/contexts/Foo');
85-
$iriConvert->getIriFromResourceClass("Foo")->willReturn('/foo/1');
86-
82+
$contextBuilder->getResourceContextUri('Foo')->willReturn('/contexts/Foo');
83+
$iriConvert->getIriFromResourceClass('Foo')->willReturn('/foo/1');
84+
$itemNormalizer = $this->prophesize(AbstractItemNormalizer::class);
85+
$itemNormalizer->normalize('foo', null, ['jsonld_has_context' => true, 'api_sub_level' => true, 'resource_class' => 'Foo'])->willReturn(['name' => 'Kévin', 'friend' => 'Smail']);
8786
$normalizer = new CollectionNormalizer($contextBuilder->reveal(), $resourceClassResolverProphecy->reveal(), $iriConvert->reveal());
8887

89-
$normalizer->setSerializer($serializer->reveal());
88+
$normalizer->setNormalizer($itemNormalizer->reveal());
9089

9190
$this->assertEquals(['@context' => '/contexts/Foo', '@id' => '/foo/1', '@type' => 'hydra:Collection', 'hydra:member' => [0 => ['name' => 'Kévin', 'friend' => 'Smail']], 'hydra:totalItems' => 1312.], $normalizer->normalize($paginator));
9291
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
namespace ApiPlatform\Core\tests\Hydra;
13+
14+
use ApiPlatform\Core\Api\IriConverterInterface;
15+
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
16+
use ApiPlatform\Core\Exception\InvalidArgumentException;
17+
use ApiPlatform\Core\JsonLd\ContextBuilderInterface;
18+
use ApiPlatform\Core\JsonLd\Serializer\ItemNormalizer;
19+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
20+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
21+
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
22+
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
23+
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
24+
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
25+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
26+
use Prophecy\Argument;
27+
use Symfony\Component\PropertyInfo\Type;
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 \PHPUnit_Framework_TestCase
35+
{
36+
public function testDontSupportDenormalization()
37+
{
38+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
39+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
40+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
41+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
42+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
43+
$contextBuilderProphecy = $this->prophesize(ContextBuilderInterface::class);
44+
$resourceClassResolverProphecy->getResourceClass(['dummy'], 'Dummy')->willReturn(Dummy::class);
45+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['name' => 'name']));
46+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn(new PropertyMetadata())->shouldBeCalled(1);
47+
48+
$normalizer = new ItemNormalizer($resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $contextBuilderProphecy->reveal());
49+
50+
$this->assertFalse($normalizer->supportsDenormalization('foo', ItemNormalizer::FORMAT));
51+
$normalizer->denormalize(['foo'], Dummy::class, 'jsonld', ['jsonld_has_context' => true, 'jsonld_sub_level' => true, 'resource_class' => Dummy::class]);
52+
}
53+
54+
public function testSupportNormalization()
55+
{
56+
$std = new \stdClass();
57+
$dummy = new Dummy();
58+
$dummy->setDescription('hello');
59+
60+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
61+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
62+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
63+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
64+
$contextBuilderProphecy = $this->prophesize(ContextBuilderInterface::class);
65+
66+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
67+
$resourceClassResolverProphecy->getResourceClass($dummy)->willReturn(Dummy::class)->shouldBeCalled();
68+
$resourceClassResolverProphecy->getResourceClass($std)->willThrow(new InvalidArgumentException())->shouldBeCalled();
69+
70+
$normalizer = new ItemNormalizer(
71+
$resourceMetadataFactoryProphecy->reveal(),
72+
$propertyNameCollectionFactoryProphecy->reveal(),
73+
$propertyMetadataFactoryProphecy->reveal(),
74+
$iriConverterProphecy->reveal(),
75+
$resourceClassResolverProphecy->reveal(),
76+
$contextBuilderProphecy->reveal()
77+
);
78+
79+
$this->assertTrue($normalizer->supportsNormalization($dummy, 'jsonld'));
80+
$this->assertFalse($normalizer->supportsNormalization($dummy, 'xml'));
81+
$this->assertFalse($normalizer->supportsNormalization($std, 'jsonld'));
82+
}
83+
84+
public function testNormalize()
85+
{
86+
$dummy = new Dummy();
87+
$dummy->setName('hello');
88+
89+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
90+
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata('Dummy'));
91+
$propertyNameCollection = new PropertyNameCollection(['name']);
92+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
93+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection)->shouldBeCalled();
94+
95+
$propertyMetadataFactory = new PropertyMetadata(null, null, true);
96+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
97+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadataFactory)->shouldBeCalled();
98+
99+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
100+
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummies/1988')->shouldBeCalled();
101+
102+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
103+
$resourceClassResolverProphecy->getResourceClass($dummy, null, true)->willReturn(Dummy::class)->shouldBeCalled();
104+
105+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
106+
$serializerProphecy->willImplement(NormalizerInterface::class);
107+
$serializerProphecy->normalize('hello', null, Argument::type('array'))->willReturn('hello')->shouldBeCalled();
108+
$contextBuilderProphecy = $this->prophesize(ContextBuilderInterface::class);
109+
$contextBuilderProphecy->getResourceContextUri(Dummy::class)->willReturn('/contexts/Dummy');
110+
111+
$normalizer = new ItemNormalizer(
112+
$resourceMetadataFactoryProphecy->reveal(),
113+
$propertyNameCollectionFactoryProphecy->reveal(),
114+
$propertyMetadataFactoryProphecy->reveal(),
115+
$iriConverterProphecy->reveal(),
116+
$resourceClassResolverProphecy->reveal(),
117+
$contextBuilderProphecy->reveal()
118+
);
119+
$normalizer->setSerializer($serializerProphecy->reveal());
120+
121+
$expected = ['@context' => '/contexts/Dummy',
122+
'@id' => '/dummies/1988',
123+
'@type' => 'Dummy',
124+
'name' => 'hello',
125+
126+
127+
];
128+
$this->assertEquals($expected, $normalizer->normalize($dummy));
129+
}
130+
}

0 commit comments

Comments
 (0)