• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 14726067612

29 Apr 2025 07:47AM UTC coverage: 23.443% (+15.2%) from 8.252%
14726067612

push

github

web-flow
feat(symfony): Autoconfigure classes using `#[ApiResource]` attribute (#6943)

0 of 12 new or added lines in 4 files covered. (0.0%)

3578 existing lines in 159 files now uncovered.

11517 of 49127 relevant lines covered (23.44%)

54.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Serializer/Tests/AbstractItemNormalizerTest.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Serializer\Tests;
15

16
use ApiPlatform\Metadata\ApiProperty;
17
use ApiPlatform\Metadata\ApiResource;
18
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
19
use ApiPlatform\Metadata\Get;
20
use ApiPlatform\Metadata\GetCollection;
21
use ApiPlatform\Metadata\IriConverterInterface;
22
use ApiPlatform\Metadata\Operations;
23
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
24
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
25
use ApiPlatform\Metadata\Property\PropertyNameCollection;
26
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
27
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
28
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
29
use ApiPlatform\Metadata\ResourceClassResolverInterface;
30
use ApiPlatform\Metadata\UrlGeneratorInterface;
31
use ApiPlatform\Serializer\AbstractItemNormalizer;
32
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\DtoWithNullValue;
33
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\Dummy;
34
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\DummyTableInheritance;
35
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\DummyTableInheritanceChild;
36
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\DummyTableInheritanceRelated;
37
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\NonCloneableDummy;
38
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\PropertyCollectionIriOnly;
39
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\PropertyCollectionIriOnlyRelation;
40
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\RelatedDummy;
41
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\SecuredDummy;
42
use Doctrine\Common\Collections\ArrayCollection;
43
use PHPUnit\Framework\TestCase;
44
use Prophecy\Argument;
45
use Prophecy\PhpUnit\ProphecyTrait;
46
use Symfony\Component\PropertyAccess\PropertyAccessor;
47
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
48
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
49
use Symfony\Component\PropertyInfo\Type as LegacyType;
50
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
51
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
52
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
53
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
54
use Symfony\Component\Serializer\Serializer;
55
use Symfony\Component\Serializer\SerializerInterface;
56
use Symfony\Component\TypeInfo\Type;
57

58
/**
59
 * @author Amrouche Hamza <hamza.simperfit@gmail.com>
60
 * @author Kévin Dunglas <dunglas@gmail.com>
61
 */
62
class AbstractItemNormalizerTest extends TestCase
63
{
64
    use ProphecyTrait;
65

66
    #[\PHPUnit\Framework\Attributes\Group('legacy')]
67
    public function testSupportNormalizationAndSupportDenormalization(): void
68
    {
UNCOV
69
        $std = new \stdClass();
×
70
        $dummy = new Dummy();
×
71

72
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
73
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
UNCOV
74
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
75
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
76

77
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
UNCOV
78
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
79
        $resourceClassResolverProphecy->isResourceClass(\stdClass::class)->willReturn(false);
×
80

81
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
82

83
        $this->assertTrue($normalizer->supportsNormalization($dummy));
×
84
        $this->assertFalse($normalizer->supportsNormalization($std));
×
85
        $this->assertTrue($normalizer->supportsDenormalization($dummy, Dummy::class));
×
86
        $this->assertFalse($normalizer->supportsDenormalization($std, \stdClass::class));
×
UNCOV
87
        $this->assertFalse($normalizer->supportsNormalization([]));
×
UNCOV
88
        $this->assertSame(['object' => true], $normalizer->getSupportedTypes('any'));
×
89
    }
90

91
    public function testNormalize(): void
92
    {
93
        $relatedDummy = new RelatedDummy();
×
94

95
        $dummy = new Dummy();
×
96
        $dummy->setName('foo');
×
97
        $dummy->setAlias('ignored');
×
UNCOV
98
        $dummy->setRelatedDummy($relatedDummy);
×
99
        $dummy->relatedDummies->add(new RelatedDummy());
×
100

101
        $relatedDummies = new ArrayCollection([$relatedDummy]);
×
102

UNCOV
103
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
104
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['name', 'alias', 'relatedDummy', 'relatedDummies']));
×
105

106
        // BC layer for api-platform/metadata < 4.1
107
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
108
            $relatedDummyType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
109
            $relatedDummiesType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), $relatedDummyType);
×
110

111
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
UNCOV
112
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
×
113
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'alias', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
×
114
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummyType])->withDescription('')->withReadable(true)->withWritable(false)->withReadableLink(false));
×
115
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(true)->withWritable(false)->withReadableLink(false));
×
116
        } else {
117
            $relatedDummyType = Type::object(RelatedDummy::class);
×
118
            $relatedDummiesType = Type::collection(Type::object(ArrayCollection::class), $relatedDummyType, Type::int());
×
119

120
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
UNCOV
121
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true));
×
122
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'alias', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true));
×
123
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withNativeType($relatedDummyType)->withDescription('')->withReadable(true)->withWritable(false)->withReadableLink(false));
×
124
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withNativeType($relatedDummiesType)->withReadable(true)->withWritable(false)->withReadableLink(false));
×
125
        }
126

127
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
128
        $iriConverterProphecy->getIriFromResource($dummy, Argument::cetera())->willReturn('/dummies/1');
×
UNCOV
129
        $iriConverterProphecy->getIriFromResource($relatedDummy, Argument::cetera())->willReturn('/dummies/2');
×
130

131
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
132
        $propertyAccessorProphecy->getValue($dummy, 'name')->willReturn('foo');
×
133
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummy')->willReturn($relatedDummy);
×
UNCOV
134
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummies')->willReturn($relatedDummies);
×
135

136
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
UNCOV
137
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
138
        $resourceClassResolverProphecy->getResourceClass($dummy, null)->willReturn(Dummy::class);
×
139
        $resourceClassResolverProphecy->getResourceClass($relatedDummy, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
140
        $resourceClassResolverProphecy->getResourceClass($relatedDummies, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
141
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
142
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
143

144
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
145
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
146
        $serializerProphecy->normalize('foo', null, Argument::type('array'))->willReturn('foo');
×
UNCOV
147
        $serializerProphecy->normalize(['/dummies/2'], null, Argument::type('array'))->willReturn(['/dummies/2']);
×
148

UNCOV
149
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
UNCOV
150
        $normalizer->setSerializer($serializerProphecy->reveal());
×
151

152
        $expected = [
×
153
            'name' => 'foo',
×
UNCOV
154
            'relatedDummy' => '/dummies/2',
×
155
            'relatedDummies' => ['/dummies/2'],
×
156
        ];
×
UNCOV
157
        $this->assertSame($expected, $normalizer->normalize($dummy, null, [
×
158
            'resources' => [],
×
159
            'ignored_attributes' => ['alias'],
×
160
        ]));
×
161
    }
162

163
    public function testNormalizeWithSecuredProperty(): void
164
    {
165
        $dummy = new SecuredDummy();
×
166
        $dummy->setTitle('myPublicTitle');
×
167
        $dummy->setAdminOnlyProperty('secret');
×
168

169
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
170
        $propertyNameCollectionFactoryProphecy->create(SecuredDummy::class, [])->willReturn(new PropertyNameCollection(['title', 'adminOnlyProperty']));
×
171

172
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
173

174
        // BC layer for api-platform/metadata < 4.1
175
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
176
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
×
177
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'adminOnlyProperty', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true)->withSecurity('is_granted(\'ROLE_ADMIN\')'));
×
178
        } else {
179
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true));
×
UNCOV
180
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'adminOnlyProperty', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true)->withSecurity('is_granted(\'ROLE_ADMIN\')'));
×
181
        }
182

183
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
UNCOV
184
        $iriConverterProphecy->getIriFromResource($dummy, Argument::cetera())->willReturn('/secured_dummies/1');
×
185

186
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
UNCOV
187
        $propertyAccessorProphecy->getValue($dummy, 'title')->willReturn('myPublicTitle');
×
188
        $propertyAccessorProphecy->getValue($dummy, 'adminOnlyProperty')->willReturn('secret');
×
189

190
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
191
        $resourceClassResolverProphecy->getResourceClass($dummy, null)->willReturn(SecuredDummy::class);
×
192
        $resourceClassResolverProphecy->getResourceClass(null, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
193
        $resourceClassResolverProphecy->isResourceClass(SecuredDummy::class)->willReturn(true);
×
194

UNCOV
195
        $resourceAccessChecker = $this->prophesize(ResourceAccessCheckerInterface::class);
×
UNCOV
196
        $resourceAccessChecker->isGranted(
×
UNCOV
197
            SecuredDummy::class,
×
198
            'is_granted(\'ROLE_ADMIN\')',
×
199
            ['object' => $dummy, 'property' => 'adminOnlyProperty']
×
UNCOV
200
        )->willReturn(false);
×
201

202
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
203
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
204
        $serializerProphecy->normalize('myPublicTitle', null, Argument::type('array'))->willReturn('myPublicTitle');
×
205

206
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, $resourceAccessChecker->reveal()) extends AbstractItemNormalizer {};
×
UNCOV
207
        $normalizer->setSerializer($serializerProphecy->reveal());
×
208

209
        $expected = [
×
210
            'title' => 'myPublicTitle',
×
UNCOV
211
        ];
×
212
        $this->assertSame($expected, $normalizer->normalize($dummy, null, [
×
213
            'resources' => [],
×
UNCOV
214
        ]));
×
215
    }
216

217
    public function testNormalizePropertyAsIriWithUriTemplate(): void
218
    {
UNCOV
219
        $propertyCollectionIriOnlyRelation = new PropertyCollectionIriOnlyRelation();
×
220
        $propertyCollectionIriOnlyRelation->name = 'My Relation';
×
221

222
        $propertyCollectionIriOnly = new PropertyCollectionIriOnly();
×
223
        $propertyCollectionIriOnly->addPropertyCollectionIriOnlyRelation($propertyCollectionIriOnlyRelation);
×
224

225
        $collectionOperation = new GetCollection('/property-collection-relations');
×
UNCOV
226
        $getIterableOperation = new GetCollection('/parent/{parentId}/another-collection-operations');
×
227
        $getToOneOperation = new Get('/parent/{parentId}/another-collection-operations/{id}');
×
228

229
        $resourceRelationMetadataCollection = new ResourceMetadataCollection(PropertyCollectionIriOnlyRelation::class, [
×
230
            (new ApiResource())->withOperations(new Operations([$collectionOperation, $getIterableOperation, $getToOneOperation])),
×
231
        ]);
×
232

233
        $resourceMetadataCollectionFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
234
        $resourceMetadataCollectionFactoryProphecy->create(PropertyCollectionIriOnlyRelation::class)->willReturn($resourceRelationMetadataCollection);
×
235

236
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
237
        $propertyNameCollectionFactoryProphecy->create(PropertyCollectionIriOnly::class, ['normalization_groups' => null, 'denormalization_groups' => null, 'operation_name' => null])->willReturn(
×
UNCOV
238
            new PropertyNameCollection(['propertyCollectionIriOnlyRelation', 'iterableIri', 'toOneRelation'])
×
239
        );
×
240

241
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
242

243
        // BC layer for api-platform/metadata < 4.1
244
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
245
            $propertyMetadataFactoryProphecy->create(PropertyCollectionIriOnly::class, 'propertyCollectionIriOnlyRelation', ['normalization_groups' => null, 'denormalization_groups' => null, 'operation_name' => null])->willReturn(
×
246
                (new ApiProperty())->withReadable(true)->withUriTemplate('/property-collection-relations')->withBuiltinTypes([
×
247
                    new LegacyType('iterable', false, null, true, new LegacyType('int', false, null, false), new LegacyType('object', false, PropertyCollectionIriOnlyRelation::class, false)),
×
UNCOV
248
                ])
×
249
            );
×
UNCOV
250
            $propertyMetadataFactoryProphecy->create(PropertyCollectionIriOnly::class, 'iterableIri', ['normalization_groups' => null, 'denormalization_groups' => null, 'operation_name' => null])->willReturn(
×
251
                (new ApiProperty())->withReadable(true)->withUriTemplate('/parent/{parentId}/another-collection-operations')->withBuiltinTypes([
×
252
                    new LegacyType('iterable', false, null, true, new LegacyType('int', false, null, false), new LegacyType('object', false, PropertyCollectionIriOnlyRelation::class, false)),
×
UNCOV
253
                ])
×
254
            );
×
255
            $propertyMetadataFactoryProphecy->create(PropertyCollectionIriOnly::class, 'toOneRelation', ['normalization_groups' => null, 'denormalization_groups' => null, 'operation_name' => null])->willReturn(
×
UNCOV
256
                (new ApiProperty())->withReadable(true)->withUriTemplate('/parent/{parentId}/another-collection-operations/{id}')->withBuiltinTypes([
×
257
                    new LegacyType('object', false, PropertyCollectionIriOnlyRelation::class, false),
×
258
                ])
×
UNCOV
259
            );
×
260
        } else {
261
            $propertyMetadataFactoryProphecy->create(PropertyCollectionIriOnly::class, 'propertyCollectionIriOnlyRelation', ['normalization_groups' => null, 'denormalization_groups' => null, 'operation_name' => null])->willReturn(
×
262
                (new ApiProperty())->withReadable(true)->withUriTemplate('/property-collection-relations')->withNativeType(Type::list(Type::object(PropertyCollectionIriOnlyRelation::class)))
×
UNCOV
263
            );
×
264
            $propertyMetadataFactoryProphecy->create(PropertyCollectionIriOnly::class, 'iterableIri', ['normalization_groups' => null, 'denormalization_groups' => null, 'operation_name' => null])->willReturn(
×
265
                (new ApiProperty())->withReadable(true)->withUriTemplate('/parent/{parentId}/another-collection-operations')->withNativeType(Type::iterable(Type::object(PropertyCollectionIriOnlyRelation::class)))
×
266
            );
×
267
            $propertyMetadataFactoryProphecy->create(PropertyCollectionIriOnly::class, 'toOneRelation', ['normalization_groups' => null, 'denormalization_groups' => null, 'operation_name' => null])->willReturn(
×
268
                (new ApiProperty())->withReadable(true)->withUriTemplate('/parent/{parentId}/another-collection-operations/{id}')->withNativeType(Type::object(PropertyCollectionIriOnlyRelation::class))
×
UNCOV
269
            );
×
270
        }
271

272
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
273
        $iriConverterProphecy->getIriFromResource($propertyCollectionIriOnly, UrlGeneratorInterface::ABS_URL, null, Argument::any())->willReturn('/property-collection-relations', '/parent/42/another-collection-operations');
×
UNCOV
274
        $iriConverterProphecy->getIriFromResource($propertyCollectionIriOnly, UrlGeneratorInterface::ABS_PATH, Argument::type(GetCollection::class), Argument::any())->willReturn('/property-collection-relations', '/parent/42/another-collection-operations');
×
UNCOV
275
        $iriConverterProphecy->getIriFromResource($propertyCollectionIriOnly, UrlGeneratorInterface::ABS_PATH, Argument::type(Get::class), Argument::any())->willReturn('/parent/42/another-collection-operations/24');
×
276

UNCOV
277
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
278
        $propertyAccessorProphecy->getValue($propertyCollectionIriOnly, 'propertyCollectionIriOnlyRelation')->willReturn([$propertyCollectionIriOnlyRelation]);
×
279
        $propertyAccessorProphecy->getValue($propertyCollectionIriOnly, 'iterableIri')->willReturn($propertyCollectionIriOnlyRelation);
×
280
        $propertyAccessorProphecy->getValue($propertyCollectionIriOnly, 'toOneRelation')->willReturn($propertyCollectionIriOnlyRelation);
×
281

UNCOV
282
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
283

284
        $resourceClassResolverProphecy->isResourceClass(PropertyCollectionIriOnly::class)->willReturn(true);
×
UNCOV
285
        $resourceClassResolverProphecy->getResourceClass(null, PropertyCollectionIriOnly::class)->willReturn(PropertyCollectionIriOnly::class);
×
286

287
        $resourceClassResolverProphecy->isResourceClass(PropertyCollectionIriOnlyRelation::class)->willReturn(true);
×
288
        $resourceClassResolverProphecy->getResourceClass([$propertyCollectionIriOnlyRelation], PropertyCollectionIriOnlyRelation::class)->willReturn(PropertyCollectionIriOnlyRelation::class);
×
289

290
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), new PropertyAccessor(), // $propertyAccessorProphecy->reveal(),
×
UNCOV
291
            null, null, [], $resourceMetadataCollectionFactoryProphecy->reveal(), null, ) extends AbstractItemNormalizer {};
×
292

UNCOV
293
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
294
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
295
        $normalizer->setSerializer($serializerProphecy->reveal());
×
296

UNCOV
297
        $expected = [
×
298
            'propertyCollectionIriOnlyRelation' => '/property-collection-relations',
×
299
            'iterableIri' => '/parent/42/another-collection-operations',
×
UNCOV
300
            'toOneRelation' => '/parent/42/another-collection-operations/24',
×
301
        ];
×
302

303
        $this->assertSame($expected, $normalizer->normalize($propertyCollectionIriOnly, 'jsonld', [
×
304
            'resources' => [],
×
305
            'root_operation' => new GetCollection('/property_collection_iri_onlies'),
×
306
        ]));
×
307
    }
308

309
    public function testDenormalizeWithSecuredProperty(): void
310
    {
311
        $data = [
×
UNCOV
312
            'title' => 'foo',
×
313
            'adminOnlyProperty' => 'secret',
×
UNCOV
314
        ];
×
315

316
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
UNCOV
317
        $propertyNameCollectionFactoryProphecy->create(SecuredDummy::class, [])->willReturn(new PropertyNameCollection(['title', 'adminOnlyProperty']));
×
318

UNCOV
319
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
320

321
        // BC layer for api-platform/metadata < 4.1
322
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
323
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
324
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'adminOnlyProperty', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true)->withSecurity('is_granted(\'ROLE_ADMIN\')'));
×
325
        } else {
326
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(false)->withWritable(true));
×
327
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'adminOnlyProperty', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true)->withSecurity('is_granted(\'ROLE_ADMIN\')'));
×
328
        }
329

330
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
331

UNCOV
332
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
333

UNCOV
334
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
335
        $resourceClassResolverProphecy->getResourceClass(null, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
UNCOV
336
        $resourceClassResolverProphecy->isResourceClass(SecuredDummy::class)->willReturn(true);
×
337

338
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
339
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
340

341
        $resourceAccessChecker = $this->prophesize(ResourceAccessCheckerInterface::class);
×
342
        $resourceAccessChecker->isGranted(
×
UNCOV
343
            SecuredDummy::class,
×
344
            'is_granted(\'ROLE_ADMIN\')',
×
345
            ['object' => null, 'property' => 'adminOnlyProperty']
×
346
        )->willReturn(false);
×
347

348
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, $resourceAccessChecker->reveal()) extends AbstractItemNormalizer {};
×
349
        $normalizer->setSerializer($serializerProphecy->reveal());
×
350

351
        $actual = $normalizer->denormalize($data, SecuredDummy::class);
×
352

UNCOV
353
        $this->assertInstanceOf(SecuredDummy::class, $actual);
×
354

UNCOV
355
        $propertyAccessorProphecy->setValue($actual, 'title', 'foo')->shouldHaveBeenCalled();
×
356
        $propertyAccessorProphecy->setValue($actual, 'adminOnlyProperty', 'secret')->shouldNotHaveBeenCalled();
×
357
    }
358

359
    public function testDenormalizeCreateWithDeniedPostDenormalizeSecuredProperty(): void
360
    {
UNCOV
361
        $data = [
×
UNCOV
362
            'title' => 'foo',
×
UNCOV
363
            'ownerOnlyProperty' => 'should not be set',
×
UNCOV
364
        ];
×
365

UNCOV
366
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
367
        $propertyNameCollectionFactoryProphecy->create(SecuredDummy::class, [])->willReturn(new PropertyNameCollection(['title', 'ownerOnlyProperty']));
×
368

369
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
370

371
        // BC layer for api-platform/metadata < 4.1
372
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
373
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
UNCOV
374
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true)->withWritable(true)->withSecurityPostDenormalize('false')->withDefault(''));
×
375
        } else {
376
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(false)->withWritable(true));
×
377
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true)->withWritable(true)->withSecurityPostDenormalize('false')->withDefault(''));
×
378
        }
379

UNCOV
380
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
381

UNCOV
382
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
383

384
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
385
        $resourceClassResolverProphecy->getResourceClass(null, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
386
        $resourceClassResolverProphecy->isResourceClass(SecuredDummy::class)->willReturn(true);
×
387

388
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
389
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
390

391
        $resourceAccessChecker = $this->prophesize(ResourceAccessCheckerInterface::class);
×
392
        $resourceAccessChecker->isGranted(
×
393
            SecuredDummy::class,
×
394
            'false',
×
395
            Argument::any()
×
396
        )->willReturn(false);
×
397

398
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, $resourceAccessChecker->reveal()) extends AbstractItemNormalizer {};
×
399
        $normalizer->setSerializer($serializerProphecy->reveal());
×
400

401
        $actual = $normalizer->denormalize($data, SecuredDummy::class);
×
402

403
        $this->assertInstanceOf(SecuredDummy::class, $actual);
×
404

UNCOV
405
        $propertyAccessorProphecy->setValue($actual, 'title', 'foo')->shouldHaveBeenCalled();
×
406
        $propertyAccessorProphecy->setValue($actual, 'ownerOnlyProperty', 'should not be set')->shouldHaveBeenCalled();
×
407
        $propertyAccessorProphecy->setValue($actual, 'ownerOnlyProperty', '')->shouldHaveBeenCalled();
×
408
    }
409

410
    public function testDenormalizeUpdateWithSecuredProperty(): void
411
    {
412
        $dummy = new SecuredDummy();
×
413

UNCOV
414
        $data = [
×
UNCOV
415
            'title' => 'foo',
×
UNCOV
416
            'ownerOnlyProperty' => 'secret',
×
417
        ];
×
418

UNCOV
419
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
420
        $propertyNameCollectionFactoryProphecy->create(SecuredDummy::class, [])->willReturn(new PropertyNameCollection(['title', 'ownerOnlyProperty']));
×
421

422
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
423

424
        // BC layer for api-platform/metadata < 4.1
425
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
426
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
UNCOV
427
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true)->withWritable(true)->withSecurity('true'));
×
428
        } else {
429
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(false)->withWritable(true));
×
430
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true)->withWritable(true)->withSecurity('true'));
×
431
        }
432

UNCOV
433
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
434

UNCOV
435
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
436

437
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
438
        $resourceClassResolverProphecy->getResourceClass($dummy, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
439
        $resourceClassResolverProphecy->getResourceClass(null, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
UNCOV
440
        $resourceClassResolverProphecy->isResourceClass(SecuredDummy::class)->willReturn(true);
×
441

442
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
443
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
444

445
        $resourceAccessChecker = $this->prophesize(ResourceAccessCheckerInterface::class);
×
446
        $resourceAccessChecker->isGranted(
×
447
            SecuredDummy::class,
×
448
            'true',
×
449
            ['object' => null, 'property' => 'ownerOnlyProperty']
×
450
        )->willReturn(true);
×
451
        $resourceAccessChecker->isGranted(
×
452
            SecuredDummy::class,
×
453
            'true',
×
454
            ['object' => $dummy, 'property' => 'ownerOnlyProperty']
×
UNCOV
455
        )->willReturn(true);
×
456

457
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, $resourceAccessChecker->reveal()) extends AbstractItemNormalizer {};
×
UNCOV
458
        $normalizer->setSerializer($serializerProphecy->reveal());
×
459

460
        $context = [AbstractItemNormalizer::OBJECT_TO_POPULATE => $dummy];
×
UNCOV
461
        $actual = $normalizer->denormalize($data, SecuredDummy::class, null, $context);
×
462

UNCOV
463
        $this->assertInstanceOf(SecuredDummy::class, $actual);
×
464

465
        $propertyAccessorProphecy->setValue($actual, 'title', 'foo')->shouldHaveBeenCalled();
×
UNCOV
466
        $propertyAccessorProphecy->setValue($actual, 'ownerOnlyProperty', 'secret')->shouldHaveBeenCalled();
×
467
    }
468

469
    public function testDenormalizeUpdateWithDeniedSecuredProperty(): void
470
    {
471
        $dummy = new SecuredDummy();
×
UNCOV
472
        $dummy->setOwnerOnlyProperty('secret');
×
473

474
        $data = [
×
475
            'title' => 'foo',
×
476
            'ownerOnlyProperty' => 'should not be set',
×
UNCOV
477
        ];
×
478

479
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
UNCOV
480
        $propertyNameCollectionFactoryProphecy->create(SecuredDummy::class, [])->willReturn(new PropertyNameCollection(['title', 'ownerOnlyProperty']));
×
481

482
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
483

484
        // BC layer for api-platform/metadata < 4.1
485
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
486
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
487
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true)->withWritable(true)->withSecurity('false'));
×
488
        } else {
UNCOV
489
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(false)->withWritable(true));
×
490
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true)->withWritable(true)->withSecurity('false'));
×
491
        }
492

493
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
494

495
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
496

UNCOV
497
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
498
        $resourceClassResolverProphecy->getResourceClass(null, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
499
        $resourceClassResolverProphecy->getResourceClass($dummy, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
500
        $resourceClassResolverProphecy->isResourceClass(SecuredDummy::class)->willReturn(true);
×
501

502
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
503
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
504

505
        $resourceAccessChecker = $this->prophesize(ResourceAccessCheckerInterface::class);
×
506
        $resourceAccessChecker->isGranted(
×
UNCOV
507
            SecuredDummy::class,
×
508
            'false',
×
509
            ['object' => null, 'property' => 'ownerOnlyProperty']
×
UNCOV
510
        )->willReturn(false);
×
511
        $resourceAccessChecker->isGranted(
×
UNCOV
512
            SecuredDummy::class,
×
513
            'false',
×
514
            ['object' => $dummy, 'property' => 'ownerOnlyProperty']
×
515
        )->willReturn(false);
×
516

UNCOV
517
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, $resourceAccessChecker->reveal()) extends AbstractItemNormalizer {};
×
UNCOV
518
        $normalizer->setSerializer($serializerProphecy->reveal());
×
519

520
        $context = [AbstractItemNormalizer::OBJECT_TO_POPULATE => $dummy];
×
UNCOV
521
        $actual = $normalizer->denormalize($data, SecuredDummy::class, null, $context);
×
522

523
        $this->assertInstanceOf(SecuredDummy::class, $actual);
×
524

UNCOV
525
        $propertyAccessorProphecy->setValue($actual, 'title', 'foo')->shouldHaveBeenCalled();
×
526
        $propertyAccessorProphecy->setValue($actual, 'ownerOnlyProperty', 'should not be set')->shouldNotHaveBeenCalled();
×
527
    }
528

529
    public function testDenormalizeUpdateWithDeniedPostDenormalizeSecuredProperty(): void
530
    {
531
        $dummy = new SecuredDummy();
×
532
        $dummy->setOwnerOnlyProperty('secret');
×
533

534
        $data = [
×
535
            'title' => 'foo',
×
536
            'ownerOnlyProperty' => 'should not be set',
×
UNCOV
537
        ];
×
538

539
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
UNCOV
540
        $propertyNameCollectionFactoryProphecy->create(SecuredDummy::class, [])->willReturn(new PropertyNameCollection(['title', 'ownerOnlyProperty']));
×
541

542
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
543

544
        // BC layer for api-platform/metadata < 4.1
545
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
546
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
547
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true)->withWritable(true)->withSecurityPostDenormalize('false'));
×
548
        } else {
549
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(false)->withWritable(true));
×
550
            $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true)->withWritable(true)->withSecurityPostDenormalize('false'));
×
551
        }
552

553
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
554

555
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
556
        $propertyAccessorProphecy->getValue($dummy, 'ownerOnlyProperty')->willReturn('secret');
×
557

558
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
559
        $resourceClassResolverProphecy->getResourceClass($dummy, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
560
        $resourceClassResolverProphecy->getResourceClass(null, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
561
        $resourceClassResolverProphecy->isResourceClass(SecuredDummy::class)->willReturn(true);
×
562

563
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
564
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
565

566
        $resourceAccessChecker = $this->prophesize(ResourceAccessCheckerInterface::class);
×
UNCOV
567
        $resourceAccessChecker->isGranted(
×
568
            SecuredDummy::class,
×
569
            'false',
×
570
            ['object' => $dummy, 'previous_object' => $dummy, 'property' => 'ownerOnlyProperty']
×
571
        )->willReturn(false);
×
572

573
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, $resourceAccessChecker->reveal()) extends AbstractItemNormalizer {};
×
574
        $normalizer->setSerializer($serializerProphecy->reveal());
×
575

UNCOV
576
        $context = [AbstractItemNormalizer::OBJECT_TO_POPULATE => $dummy];
×
UNCOV
577
        $actual = $normalizer->denormalize($data, SecuredDummy::class, null, $context);
×
578

UNCOV
579
        $this->assertInstanceOf(SecuredDummy::class, $actual);
×
580

UNCOV
581
        $propertyAccessorProphecy->setValue($actual, 'title', 'foo')->shouldHaveBeenCalled();
×
582
        $propertyAccessorProphecy->setValue($actual, 'ownerOnlyProperty', 'should not be set')->shouldHaveBeenCalled();
×
583
        $propertyAccessorProphecy->setValue($actual, 'ownerOnlyProperty', 'secret')->shouldHaveBeenCalled();
×
584
    }
585

586
    public function testNormalizeReadableLinks(): void
587
    {
588
        $relatedDummy = new RelatedDummy();
×
589

590
        $dummy = new Dummy();
×
591
        $dummy->setRelatedDummy($relatedDummy);
×
UNCOV
592
        $dummy->relatedDummies->add(new RelatedDummy());
×
593

594
        $relatedDummies = new ArrayCollection([$relatedDummy]);
×
595

596
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
597
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['relatedDummy', 'relatedDummies']));
×
598

599
        // BC layer for api-platform/metadata < 4.1
600
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
601
            $relatedDummyType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
602
            $relatedDummiesType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), $relatedDummyType);
×
603

604
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
605
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummyType])->withReadable(true)->withWritable(false)->withReadableLink(true));
×
606
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(true)->withWritable(false)->withReadableLink(true));
×
607
        } else {
608
            $relatedDummyType = Type::object(RelatedDummy::class);
×
UNCOV
609
            $relatedDummiesType = Type::collection(Type::object(ArrayCollection::class), $relatedDummyType, Type::int());
×
610

611
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
612
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withNativeType($relatedDummyType)->withReadable(true)->withWritable(false)->withReadableLink(true));
×
613
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withNativeType($relatedDummiesType)->withReadable(true)->withWritable(false)->withReadableLink(true));
×
614
        }
615

616
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
617
        $iriConverterProphecy->getIriFromResource($dummy, Argument::cetera())->willReturn('/dummies/1');
×
618

619
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
620
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummy')->willReturn($relatedDummy);
×
UNCOV
621
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummies')->willReturn($relatedDummies);
×
622

623
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
624
        $resourceClassResolverProphecy->getResourceClass($dummy, null)->willReturn(Dummy::class);
×
625
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
626
        $resourceClassResolverProphecy->getResourceClass($relatedDummy, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
627
        $resourceClassResolverProphecy->getResourceClass($relatedDummies, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
UNCOV
628
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
UNCOV
629
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
630

UNCOV
631
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
632
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
633
        $relatedDummyChildContext = Argument::allOf(
×
634
            Argument::type('array'),
×
635
            Argument::withEntry('resource_class', RelatedDummy::class),
×
636
            Argument::not(Argument::withKey('iri')),
×
UNCOV
637
            Argument::not(Argument::withKey('force_resource_class'))
×
638
        );
×
639
        $serializerProphecy->normalize($relatedDummy, null, $relatedDummyChildContext)->willReturn(['foo' => 'hello']);
×
UNCOV
640
        $serializerProphecy->normalize(['foo' => 'hello'], null, Argument::type('array'))->willReturn(['foo' => 'hello']);
×
641
        $serializerProphecy->normalize([['foo' => 'hello']], null, Argument::type('array'))->willReturn([['foo' => 'hello']]);
×
642

UNCOV
643
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
644
        $normalizer->setSerializer($serializerProphecy->reveal());
×
645

UNCOV
646
        $expected = [
×
647
            'relatedDummy' => ['foo' => 'hello'],
×
648
            'relatedDummies' => [['foo' => 'hello']],
×
UNCOV
649
        ];
×
650
        $this->assertSame($expected, $normalizer->normalize($dummy, null, [
×
651
            'resources' => [],
×
UNCOV
652
            'force_resource_class' => Dummy::class,
×
653
        ]));
×
654
    }
655

656
    public function testNormalizePolymorphicRelations(): void
657
    {
UNCOV
658
        $concreteDummy = new DummyTableInheritanceChild();
×
659

660
        $dummy = new DummyTableInheritanceRelated();
×
661
        $dummy->addChild($concreteDummy);
×
662

663
        $abstractDummies = new ArrayCollection([$concreteDummy]);
×
664

665
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
666
        $propertyNameCollectionFactoryProphecy->create(DummyTableInheritanceRelated::class, [])->willReturn(new PropertyNameCollection(['children']));
×
667

668
        // BC layer for api-platform/metadata < 4.1
669
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
670
            $abstractDummyType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, DummyTableInheritance::class);
×
671
            $abstractDummiesType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), $abstractDummyType);
×
672

673
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
UNCOV
674
            $propertyMetadataFactoryProphecy->create(DummyTableInheritanceRelated::class, 'children', [])->willReturn((new ApiProperty())->withBuiltinTypes([$abstractDummiesType])->withReadable(true)->withWritable(false)->withReadableLink(true));
×
675
        } else {
676
            $abstractDummyType = Type::object(DummyTableInheritance::class);
×
677
            $abstractDummiesType = Type::collection(Type::object(ArrayCollection::class), $abstractDummyType, Type::int());
×
678

UNCOV
679
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
UNCOV
680
            $propertyMetadataFactoryProphecy->create(DummyTableInheritanceRelated::class, 'children', [])->willReturn((new ApiProperty())->withNativeType($abstractDummiesType)->withReadable(true)->withWritable(false)->withReadableLink(true));
×
681
        }
682

UNCOV
683
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
UNCOV
684
        $iriConverterProphecy->getIriFromResource($dummy, Argument::cetera())->willReturn('/dummies/1');
×
685

UNCOV
686
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
UNCOV
687
        $propertyAccessorProphecy->getValue($dummy, 'children')->willReturn($abstractDummies);
×
688

UNCOV
689
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
UNCOV
690
        $resourceClassResolverProphecy->getResourceClass($dummy, null)->willReturn(DummyTableInheritanceRelated::class);
×
UNCOV
691
        $resourceClassResolverProphecy->getResourceClass(null, DummyTableInheritanceRelated::class)->willReturn(DummyTableInheritanceRelated::class);
×
UNCOV
692
        $resourceClassResolverProphecy->getResourceClass($concreteDummy, DummyTableInheritance::class)->willReturn(DummyTableInheritanceChild::class);
×
UNCOV
693
        $resourceClassResolverProphecy->getResourceClass($abstractDummies, DummyTableInheritance::class)->willReturn(DummyTableInheritance::class);
×
UNCOV
694
        $resourceClassResolverProphecy->isResourceClass(DummyTableInheritanceRelated::class)->willReturn(true);
×
UNCOV
695
        $resourceClassResolverProphecy->isResourceClass(DummyTableInheritance::class)->willReturn(true);
×
696

UNCOV
697
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
698
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
UNCOV
699
        $concreteDummyChildContext = Argument::allOf(
×
UNCOV
700
            Argument::type('array'),
×
UNCOV
701
            Argument::not(Argument::withKey('iri'))
×
UNCOV
702
        );
×
UNCOV
703
        $serializerProphecy->normalize($concreteDummy, null, $concreteDummyChildContext)->willReturn(['foo' => 'concrete']);
×
UNCOV
704
        $serializerProphecy->normalize([['foo' => 'concrete']], null, Argument::type('array'))->willReturn([['foo' => 'concrete']]);
×
705

UNCOV
706
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
UNCOV
707
        $normalizer->setSerializer($serializerProphecy->reveal());
×
708

UNCOV
709
        $expected = [
×
UNCOV
710
            'children' => [['foo' => 'concrete']],
×
UNCOV
711
        ];
×
UNCOV
712
        $this->assertSame($expected, $normalizer->normalize($dummy, null, [
×
UNCOV
713
            'resources' => [],
×
UNCOV
714
        ]));
×
715
    }
716

717
    public function testDenormalize(): void
718
    {
UNCOV
719
        $data = [
×
UNCOV
720
            'name' => 'foo',
×
UNCOV
721
            'relatedDummy' => '/dummies/1',
×
UNCOV
722
            'relatedDummies' => ['/dummies/2'],
×
UNCOV
723
        ];
×
724

UNCOV
725
        $relatedDummy1 = new RelatedDummy();
×
UNCOV
726
        $relatedDummy2 = new RelatedDummy();
×
727

UNCOV
728
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
UNCOV
729
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['name', 'relatedDummy', 'relatedDummies']));
×
730

731
        // BC layer for api-platform/metadata < 4.1
UNCOV
732
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
733
            $relatedDummyType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
UNCOV
734
            $relatedDummiesType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), $relatedDummyType);
×
735

UNCOV
736
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
UNCOV
737
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
738

739
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummyType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
740
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
741
        } else {
742
            $relatedDummyType = Type::object(RelatedDummy::class);
×
743
            $relatedDummiesType = Type::collection(Type::object(ArrayCollection::class), $relatedDummyType, Type::int());
×
744

UNCOV
745
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
746
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(false)->withWritable(true));
×
747

748
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withNativeType($relatedDummyType)->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
749
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withNativeType($relatedDummiesType)->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
750
        }
751

752
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
UNCOV
753
        $iriConverterProphecy->getResourceFromIri('/dummies/1', Argument::type('array'))->willReturn($relatedDummy1);
×
754
        $iriConverterProphecy->getResourceFromIri('/dummies/2', Argument::type('array'))->willReturn($relatedDummy2);
×
755

756
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
757

UNCOV
758
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
759
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
760
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
761
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
762
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
763

UNCOV
764
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
765
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
766

767
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
UNCOV
768
        $normalizer->setSerializer($serializerProphecy->reveal());
×
769

770
        $actual = $normalizer->denormalize($data, Dummy::class);
×
771

772
        $this->assertInstanceOf(Dummy::class, $actual);
×
773

UNCOV
774
        $propertyAccessorProphecy->setValue($actual, 'name', 'foo')->shouldHaveBeenCalled();
×
775
        $propertyAccessorProphecy->setValue($actual, 'relatedDummy', $relatedDummy1)->shouldHaveBeenCalled();
×
776
        $propertyAccessorProphecy->setValue($actual, 'relatedDummies', [$relatedDummy2])->shouldHaveBeenCalled();
×
777
    }
778

779
    public function testCanDenormalizeInputClassWithDifferentFieldsThanResourceClass(): void
780
    {
UNCOV
781
        $this->markTestSkipped('TODO: check why this test has been commented');
×
782

783
        // $data = [
784
        //     'dummyName' => 'Dummy Name',
785
        // ];
786
        //
787
        // $context = [
788
        //     'resource_class' => DummyForAdditionalFields::class,
789
        //     'input' => ['class' => DummyForAdditionalFieldsInput::class],
790
        //     'output' => ['class' => DummyForAdditionalFields::class],
791
        // ];
792
        // $augmentedContext = $context + ['api_denormalize' => true];
793
        //
794
        // $preHydratedDummy = new DummyForAdditionalFieldsInput('Name Dummy');
795
        // $cleanedContext = array_diff_key($augmentedContext, [
796
        //     'input' => null,
797
        //     'resource_class' => null,
798
        // ]);
799
        // $cleanedContextWithObjectToPopulate = array_merge($cleanedContext, [
800
        //     AbstractObjectNormalizer::OBJECT_TO_POPULATE => $preHydratedDummy,
801
        //     AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE => true,
802
        // ]);
803
        //
804
        // $dummyInputDto = new DummyForAdditionalFieldsInput('Dummy Name');
805
        // $dummy = new DummyForAdditionalFields('Dummy Name', 'name-dummy');
806
        //
807
        // $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
808
        //
809
        // $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
810
        //
811
        // $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
812
        //
813
        // $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
814
        // $resourceClassResolverProphecy->getResourceClass(null, DummyForAdditionalFields::class)->willReturn(DummyForAdditionalFields::class);
815
        //
816
        // $inputDataTransformerProphecy = $this->prophesize(DataTransformerInitializerInterface::class);
817
        // $inputDataTransformerProphecy->willImplement(DataTransformerInitializerInterface::class);
818
        // $inputDataTransformerProphecy->initialize(DummyForAdditionalFieldsInput::class, $cleanedContext)->willReturn($preHydratedDummy);
819
        // $inputDataTransformerProphecy->supportsTransformation($data, DummyForAdditionalFields::class, $augmentedContext)->willReturn(true);
820
        // $inputDataTransformerProphecy->transform($dummyInputDto, DummyForAdditionalFields::class, $augmentedContext)->willReturn($dummy);
821
        //
822
        // $serializerProphecy = $this->prophesize(SerializerInterface::class);
823
        // $serializerProphecy->willImplement(DenormalizerInterface::class);
824
        // $serializerProphecy->denormalize($data, DummyForAdditionalFieldsInput::class, 'json', $cleanedContextWithObjectToPopulate)->willReturn($dummyInputDto);
825
        //
826
        // $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), null, null, null, [], null, null) extends AbstractItemNormalizer {
827
        // };
828
        // $normalizer->setSerializer($serializerProphecy->reveal());
829
        //
830
        // $actual = $normalizer->denormalize($data, DummyForAdditionalFields::class, 'json', $context);
831
        //
832
        // $this->assertInstanceOf(DummyForAdditionalFields::class, $actual);
833
        // $this->assertSame('Dummy Name', $actual->getName());
834
    }
835

836
    public function testDenormalizeWritableLinks(): void
837
    {
838
        $data = [
×
UNCOV
839
            'name' => 'foo',
×
840
            'relatedDummy' => ['foo' => 'bar'],
×
841
            'relatedDummies' => [['bar' => 'baz']],
×
842
            'relatedDummiesWithUnionTypes' => [0 => ['bar' => 'qux'], 1. => ['bar' => 'quux']],
×
843
        ];
×
844

845
        $relatedDummy1 = new RelatedDummy();
×
UNCOV
846
        $relatedDummy2 = new RelatedDummy();
×
847
        $relatedDummy3 = new RelatedDummy();
×
848
        $relatedDummy4 = new RelatedDummy();
×
849

850
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
851
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['name', 'relatedDummy', 'relatedDummies', 'relatedDummiesWithUnionTypes']));
×
852

853
        // BC layer for api-platform/metadata < 4.1
UNCOV
854
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
855
            $relatedDummyType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
856
            $relatedDummiesType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), $relatedDummyType);
×
UNCOV
857
            $relatedDummiesWithUnionTypesIntType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), $relatedDummyType);
×
858
            $relatedDummiesWithUnionTypesFloatType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT), $relatedDummyType);
×
859

UNCOV
860
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
UNCOV
861
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
862
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummyType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
863
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
UNCOV
864
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummiesWithUnionTypes', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesWithUnionTypesIntType, $relatedDummiesWithUnionTypesFloatType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
865
        } else {
UNCOV
866
            $relatedDummyType = Type::object(RelatedDummy::class);
×
UNCOV
867
            $relatedDummiesType = Type::collection(Type::object(ArrayCollection::class), $relatedDummyType, Type::int());
×
868
            $relatedDummiesWithUnionTypesIntType = Type::collection(Type::object(ArrayCollection::class), $relatedDummyType, Type::int());
×
869
            $relatedDummiesWithUnionTypesFloatType = Type::collection(Type::object(ArrayCollection::class), $relatedDummyType, Type::float());
×
870

UNCOV
871
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
872
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(false)->withWritable(true));
×
873
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withNativeType($relatedDummyType)->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
UNCOV
874
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withNativeType($relatedDummiesType)->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
875
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummiesWithUnionTypes', [])->willReturn((new ApiProperty())->withNativeType(Type::union($relatedDummiesWithUnionTypesIntType, $relatedDummiesWithUnionTypesFloatType))->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
876
        }
877

878
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
879

880
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
881

UNCOV
882
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
883
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
884
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
885
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
886
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
887
        $resourceClassResolverProphecy->isResourceClass(ArrayCollection::class)->willReturn(false);
×
888

889
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
890
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
891
        $serializerProphecy->denormalize(['foo' => 'bar'], RelatedDummy::class, null, Argument::type('array'))->willReturn($relatedDummy1);
×
892
        $serializerProphecy->denormalize(['bar' => 'baz'], RelatedDummy::class, null, Argument::type('array'))->willReturn($relatedDummy2);
×
UNCOV
893
        $serializerProphecy->denormalize(['bar' => 'qux'], RelatedDummy::class, null, Argument::type('array'))->willReturn($relatedDummy3);
×
894
        $serializerProphecy->denormalize(['bar' => 'quux'], RelatedDummy::class, null, Argument::type('array'))->willReturn($relatedDummy4);
×
895

896
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
897
        $normalizer->setSerializer($serializerProphecy->reveal());
×
898

899
        $actual = $normalizer->denormalize($data, Dummy::class);
×
900

901
        $this->assertInstanceOf(Dummy::class, $actual);
×
902

903
        $propertyAccessorProphecy->setValue($actual, 'name', 'foo')->shouldHaveBeenCalled();
×
904
        $propertyAccessorProphecy->setValue($actual, 'relatedDummy', $relatedDummy1)->shouldHaveBeenCalled();
×
905
        $propertyAccessorProphecy->setValue($actual, 'relatedDummies', [$relatedDummy2])->shouldHaveBeenCalled();
×
906
        $propertyAccessorProphecy->setValue($actual, 'relatedDummiesWithUnionTypes', [0 => $relatedDummy3, 1. => $relatedDummy4])->shouldHaveBeenCalled();
×
907
    }
908

909
    public function testBadRelationType(): void
910
    {
911
        $this->expectException(NotNormalizableValueException::class);
×
912
        $this->expectExceptionMessage('The type of the "relatedDummy" attribute must be "array" (nested document) or "string" (IRI), "integer" given.');
×
913

UNCOV
914
        $data = [
×
UNCOV
915
            'relatedDummy' => 22,
×
UNCOV
916
        ];
×
917

918
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
919
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['relatedDummy']));
×
920

921
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
922

923
        // BC layer for api-platform/metadata < 4.1
924
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
925
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
×
UNCOV
926
                (new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false)
×
927
            );
×
928
        } else {
UNCOV
929
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
×
930
                (new ApiProperty())->withNativeType(Type::object(RelatedDummy::class))->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false)
×
931
            );
×
932
        }
933

UNCOV
934
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
935

UNCOV
936
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
937
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
938
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
939
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
940
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
941

UNCOV
942
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
943

UNCOV
944
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
945
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
946

UNCOV
947
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
948
        $normalizer->setSerializer($serializerProphecy->reveal());
×
949

UNCOV
950
        $normalizer->denormalize($data, Dummy::class);
×
951
    }
952

953
    public function testBadRelationTypeWithExceptionToValidationErrors(): void
954
    {
UNCOV
955
        $data = [
×
956
            'relatedDummy' => 22,
×
957
        ];
×
958

959
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
960
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['relatedDummy']));
×
961

UNCOV
962
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
963

964
        // BC layer for api-platform/metadata < 4.1
UNCOV
965
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
966
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
×
967
                (new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false)
×
UNCOV
968
            );
×
969
        } else {
UNCOV
970
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
×
971
                (new ApiProperty())->withNativeType(Type::object(RelatedDummy::class))->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false)
×
972
            );
×
973
        }
974

975
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
976

977
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
978
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
UNCOV
979
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
980
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
981
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
982

983
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
984

UNCOV
985
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
986
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
987

988
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
989
        $normalizer->setSerializer($serializerProphecy->reveal());
×
990

991
        // 'not_normalizable_value_exceptions' is set by Serializer thanks to DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS
992
        $actual = $normalizer->denormalize($data, Dummy::class, null, ['not_normalizable_value_exceptions' => []]);
×
993
        $this->assertNull($actual->relatedDummy);
×
994
    }
995

996
    public function testDeserializationPathForNotDenormalizableRelations(): void
997
    {
998
        $data = [
×
UNCOV
999
            'relatedDummies' => ['wrong'],
×
1000
        ];
×
1001

1002
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
UNCOV
1003
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['relatedDummies']));
×
1004

UNCOV
1005
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1006

1007
        // BC layer for api-platform/metadata < 4.1
UNCOV
1008
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1009
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn(
×
1010
                (new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, null, new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class))])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true)
×
UNCOV
1011
            );
×
1012
        } else {
UNCOV
1013
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn(
×
1014
                (new ApiProperty())->withNativeType(Type::collection(Type::object(ArrayCollection::class), Type::object(RelatedDummy::class)))->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true)
×
UNCOV
1015
            );
×
1016
        }
1017

UNCOV
1018
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
UNCOV
1019
        $iriConverterProphecy->getResourceFromIri(Argument::cetera())->willThrow(new InvalidArgumentException('Invalid IRI'));
×
1020

1021
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1022
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1023
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
UNCOV
1024
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1025
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1026

UNCOV
1027
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1028

1029
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
1030
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1031

UNCOV
1032
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {
×
1033
        };
×
1034

1035
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1036

1037
        $errors = [];
×
UNCOV
1038
        $actual = $normalizer->denormalize($data, Dummy::class, null, ['not_normalizable_value_exceptions' => &$errors]);
×
1039
        $this->assertEmpty($actual->relatedDummies);
×
1040
        $this->assertCount(1, $errors); // @phpstan-ignore-line method.impossibleType (false positive)
×
UNCOV
1041
        $this->assertInstanceOf(NotNormalizableValueException::class, $errors[0]);
×
1042
        $this->assertSame('relatedDummies[0]', $errors[0]->getPath());
×
1043
    }
1044

1045
    public function testInnerDocumentNotAllowed(): void
1046
    {
1047
        $this->expectException(UnexpectedValueException::class);
×
UNCOV
1048
        $this->expectExceptionMessage('Nested documents for attribute "relatedDummy" are not allowed. Use IRIs instead.');
×
1049

UNCOV
1050
        $data = [
×
UNCOV
1051
            'relatedDummy' => [
×
UNCOV
1052
                'foo' => 'bar',
×
UNCOV
1053
            ],
×
1054
        ];
×
1055

UNCOV
1056
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1057
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['relatedDummy']));
×
1058

1059
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1060

1061
        // BC layer for api-platform/metadata < 4.1
1062
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1063
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
×
1064
                (new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false)
×
1065
            );
×
1066
        } else {
1067
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
×
1068
                (new ApiProperty())->withNativeType(Type::object(RelatedDummy::class))->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false)
×
1069
            );
×
1070
        }
1071

1072
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1073

1074
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1075
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
UNCOV
1076
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
1077
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1078
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1079

UNCOV
1080
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1081

1082
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1083
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1084

1085
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
1086
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1087

1088
        $normalizer->denormalize($data, Dummy::class);
×
1089
    }
1090

1091
    public function testBadType(): void
1092
    {
1093
        $this->expectException(UnexpectedValueException::class);
×
1094
        $this->expectExceptionMessage('The type of the "foo" attribute must be "float", "integer" given.');
×
1095

1096
        $data = [
×
1097
            'foo' => 42,
×
UNCOV
1098
        ];
×
1099

UNCOV
1100
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1101
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['foo']));
×
1102

UNCOV
1103
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1104

1105
        // BC layer for api-platform/metadata < 4.1
UNCOV
1106
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1107
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1108
        } else {
UNCOV
1109
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1110
        }
1111

1112
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1113

1114
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
UNCOV
1115
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1116
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1117

UNCOV
1118
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1119

1120
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
1121
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1122

UNCOV
1123
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
1124
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1125

1126
        $normalizer->denormalize($data, Dummy::class);
×
1127
    }
1128

1129
    public function testTypeChecksCanBeDisabled(): void
1130
    {
1131
        $data = [
×
UNCOV
1132
            'foo' => 42,
×
1133
        ];
×
1134

UNCOV
1135
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1136
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['foo']));
×
1137

1138
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1139

1140
        // BC layer for api-platform/metadata < 4.1
UNCOV
1141
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
1142
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1143
        } else {
UNCOV
1144
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1145
        }
1146

1147
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1148

1149
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1150
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1151
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1152

1153
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1154

1155
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1156
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1157

1158
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
1159
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1160

1161
        $actual = $normalizer->denormalize($data, Dummy::class, null, ['disable_type_enforcement' => true]);
×
1162

1163
        $this->assertInstanceOf(Dummy::class, $actual);
×
1164

1165
        $propertyAccessorProphecy->setValue($actual, 'foo', 42)->shouldHaveBeenCalled();
×
1166
    }
1167

1168
    public function testJsonAllowIntAsFloat(): void
1169
    {
1170
        $data = [
×
1171
            'foo' => 42,
×
1172
        ];
×
1173

UNCOV
1174
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1175
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['foo']));
×
1176

1177
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1178

1179
        // BC layer for api-platform/metadata < 4.1
1180
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1181
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1182
        } else {
1183
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1184
        }
1185

1186
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1187

1188
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1189
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
UNCOV
1190
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1191

1192
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1193

UNCOV
1194
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1195
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1196

UNCOV
1197
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
1198
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1199

UNCOV
1200
        $actual = $normalizer->denormalize($data, Dummy::class, 'jsonfoo');
×
1201

1202
        $this->assertInstanceOf(Dummy::class, $actual);
×
1203

1204
        $propertyAccessorProphecy->setValue($actual, 'foo', 42)->shouldHaveBeenCalled();
×
1205
    }
1206

1207
    public function testDenormalizeBadKeyType(): void
1208
    {
1209
        $this->expectException(NotNormalizableValueException::class);
×
1210
        $this->expectExceptionMessage('The type of the key "a" must be "int", "string" given.');
×
1211

1212
        $data = [
×
1213
            'name' => 'foo',
×
1214
            'relatedDummy' => [
×
1215
                'foo' => 'bar',
×
1216
            ],
×
1217
            'relatedDummies' => [
×
1218
                'a' => [
×
UNCOV
1219
                    'bar' => 'baz',
×
1220
                ],
×
UNCOV
1221
            ],
×
UNCOV
1222
            'relatedDummiesWithUnionTypes' => [
×
UNCOV
1223
                'a' => [
×
UNCOV
1224
                    'bar' => 'baz',
×
1225
                ],
×
1226
            ],
×
1227
        ];
×
1228

1229
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
UNCOV
1230
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['relatedDummies']));
×
1231

1232
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1233

1234
        // BC layer for api-platform/metadata < 4.1
1235
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
1236
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
1237
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)])->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1238

UNCOV
1239
            $type = new LegacyType(
×
1240
                LegacyType::BUILTIN_TYPE_OBJECT,
×
1241
                false,
×
UNCOV
1242
                ArrayCollection::class,
×
1243
                true,
×
UNCOV
1244
                new LegacyType(LegacyType::BUILTIN_TYPE_INT),
×
1245
                new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)
×
1246
            );
×
UNCOV
1247
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$type])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
1248
        } else {
1249
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(false)->withWritable(true));
×
1250
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withNativeType(Type::object(RelatedDummy::class))->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1251

1252
            $type = Type::collection(Type::object(ArrayCollection::class), Type::object(RelatedDummy::class), Type::int());
×
UNCOV
1253
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withNativeType($type)->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
1254
        }
1255

1256
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1257

1258
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1259
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
UNCOV
1260
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
1261
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
UNCOV
1262
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1263

UNCOV
1264
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1265

1266
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1267
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1268

1269
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
1270
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1271

UNCOV
1272
        $normalizer->denormalize($data, Dummy::class);
×
1273
    }
1274

1275
    public function testNullable(): void
1276
    {
1277
        $data = [
×
UNCOV
1278
            'name' => null,
×
1279
        ];
×
1280

1281
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1282
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['name']));
×
1283

1284
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1285

1286
        // BC layer for api-platform/metadata < 4.1
1287
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
1288
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING, true)])->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1289
        } else {
1290
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withNativeType(Type::nullable(Type::string()))->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false)); // @phpstan-ignore-line
×
1291
        }
1292

1293
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1294

UNCOV
1295
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1296
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1297
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1298

UNCOV
1299
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1300

UNCOV
1301
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
1302
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1303

1304
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
1305
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1306

1307
        $actual = $normalizer->denormalize($data, Dummy::class);
×
1308

UNCOV
1309
        $this->assertInstanceOf(Dummy::class, $actual);
×
1310

1311
        $propertyAccessorProphecy->setValue($actual, 'name', null)->shouldHaveBeenCalled();
×
1312
    }
1313

1314
    public function testDenormalizeBasicTypePropertiesFromXml(): void
1315
    {
1316
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1317
        $propertyNameCollectionFactoryProphecy->create(ObjectWithBasicProperties::class, [])->willReturn(new PropertyNameCollection([
×
UNCOV
1318
            'boolTrue1',
×
1319
            'boolFalse1',
×
1320
            'boolTrue2',
×
UNCOV
1321
            'boolFalse2',
×
1322
            'int1',
×
1323
            'int2',
×
UNCOV
1324
            'float1',
×
1325
            'float2',
×
1326
            'float3',
×
UNCOV
1327
            'floatNaN',
×
1328
            'floatInf',
×
1329
            'floatNegInf',
×
UNCOV
1330
        ]));
×
1331

UNCOV
1332
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1333

1334
        // BC layer for api-platform/metadata < 4.1
UNCOV
1335
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1336
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolTrue1', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_BOOL)])->withDescription('')->withReadable(false)->withWritable(true));
×
1337
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolFalse1', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_BOOL)])->withDescription('')->withReadable(false)->withWritable(true));
×
1338
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolTrue2', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_BOOL)])->withDescription('')->withReadable(false)->withWritable(true));
×
1339
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolFalse2', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_BOOL)])->withDescription('')->withReadable(false)->withWritable(true));
×
1340
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'int1', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_INT)])->withDescription('')->withReadable(false)->withWritable(true));
×
UNCOV
1341
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'int2', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_INT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1342
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float1', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
UNCOV
1343
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float2', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1344
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float3', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1345
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatNaN', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
UNCOV
1346
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatInf', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1347
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatNegInf', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1348
        } else {
UNCOV
1349
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolTrue1', [])->willReturn((new ApiProperty())->withNativeType(Type::bool())->withDescription('')->withReadable(false)->withWritable(true));
×
1350
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolFalse1', [])->willReturn((new ApiProperty())->withNativeType(Type::bool())->withDescription('')->withReadable(false)->withWritable(true));
×
1351
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolTrue2', [])->willReturn((new ApiProperty())->withNativeType(Type::bool())->withDescription('')->withReadable(false)->withWritable(true));
×
1352
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolFalse2', [])->willReturn((new ApiProperty())->withNativeType(Type::bool())->withDescription('')->withReadable(false)->withWritable(true));
×
1353
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'int1', [])->willReturn((new ApiProperty())->withNativeType(Type::int())->withDescription('')->withReadable(false)->withWritable(true));
×
1354
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'int2', [])->willReturn((new ApiProperty())->withNativeType(Type::int())->withDescription('')->withReadable(false)->withWritable(true));
×
UNCOV
1355
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float1', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true));
×
1356
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float2', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true));
×
1357
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float3', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true));
×
1358
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatNaN', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true));
×
UNCOV
1359
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatInf', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true));
×
1360
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatNegInf', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true));
×
1361
        }
1362

1363
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1364

1365
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1366
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'boolTrue1', true)->shouldBeCalled();
×
1367
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'boolFalse1', false)->shouldBeCalled();
×
1368
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'boolTrue2', true)->shouldBeCalled();
×
1369
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'boolFalse2', false)->shouldBeCalled();
×
1370
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'int1', 4711)->shouldBeCalled();
×
1371
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'int2', -4711)->shouldBeCalled();
×
UNCOV
1372
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'float1', Argument::approximate(123.456, 0))->shouldBeCalled();
×
1373
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'float2', Argument::approximate(-1.2344e56, 1))->shouldBeCalled();
×
1374
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'float3', Argument::approximate(45E-6, 1))->shouldBeCalled();
×
1375
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'floatNaN', Argument::that(static fn (float $arg) => is_nan($arg)))->shouldBeCalled();
×
1376
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'floatInf', \INF)->shouldBeCalled();
×
UNCOV
1377
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'floatNegInf', -\INF)->shouldBeCalled();
×
1378

1379
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
UNCOV
1380
        $resourceClassResolverProphecy->getResourceClass(null, ObjectWithBasicProperties::class)->willReturn(ObjectWithBasicProperties::class);
×
1381
        $resourceClassResolverProphecy->isResourceClass(ObjectWithBasicProperties::class)->willReturn(true);
×
1382

1383
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1384
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
1385

1386
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
1387
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1388

1389
        $objectWithBasicProperties = $normalizer->denormalize(
×
1390
            [
×
1391
                'boolTrue1' => 'true',
×
1392
                'boolFalse1' => 'false',
×
UNCOV
1393
                'boolTrue2' => '1',
×
1394
                'boolFalse2' => '0',
×
1395
                'int1' => '4711',
×
1396
                'int2' => '-4711',
×
UNCOV
1397
                'float1' => '123.456',
×
UNCOV
1398
                'float2' => '-1.2344e56',
×
UNCOV
1399
                'float3' => '45E-6',
×
UNCOV
1400
                'floatNaN' => 'NaN',
×
UNCOV
1401
                'floatInf' => 'INF',
×
UNCOV
1402
                'floatNegInf' => '-INF',
×
UNCOV
1403
            ],
×
UNCOV
1404
            ObjectWithBasicProperties::class,
×
UNCOV
1405
            'xml'
×
UNCOV
1406
        );
×
1407

UNCOV
1408
        $this->assertInstanceOf(ObjectWithBasicProperties::class, $objectWithBasicProperties);
×
1409
    }
1410

1411
    public function testDenormalizeCollectionDecodedFromXmlWithOneChild(): void
1412
    {
UNCOV
1413
        $data = [
×
UNCOV
1414
            'relatedDummies' => [
×
UNCOV
1415
                'name' => 'foo',
×
UNCOV
1416
            ],
×
UNCOV
1417
        ];
×
1418

UNCOV
1419
        $relatedDummy = new RelatedDummy();
×
UNCOV
1420
        $relatedDummy->setName('foo');
×
1421

UNCOV
1422
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
UNCOV
1423
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['relatedDummies']));
×
1424

1425
        // BC layer for api-platform/metadata < 4.1
UNCOV
1426
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
1427
            $relatedDummyType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
UNCOV
1428
            $relatedDummiesType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), $relatedDummyType);
×
1429

UNCOV
1430
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
UNCOV
1431
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
1432
        } else {
UNCOV
1433
            $relatedDummyType = Type::object(RelatedDummy::class);
×
UNCOV
1434
            $relatedDummiesType = Type::collection(Type::object(ArrayCollection::class), $relatedDummyType, Type::int());
×
1435

UNCOV
1436
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
UNCOV
1437
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withNativeType($relatedDummiesType)->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
1438
        }
1439

UNCOV
1440
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1441

UNCOV
1442
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
UNCOV
1443
        $propertyAccessorProphecy->setValue(Argument::type(Dummy::class), 'relatedDummies', Argument::type('array'))->shouldBeCalled();
×
1444

UNCOV
1445
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
UNCOV
1446
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
UNCOV
1447
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
UNCOV
1448
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
UNCOV
1449
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1450

UNCOV
1451
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
1452
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
UNCOV
1453
        $serializerProphecy->denormalize(['name' => 'foo'], RelatedDummy::class, 'xml', Argument::type('array'))->willReturn($relatedDummy);
×
1454

UNCOV
1455
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
UNCOV
1456
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1457

UNCOV
1458
        $normalizer->denormalize($data, Dummy::class, 'xml');
×
1459
    }
1460

1461
    public function testDenormalizePopulatingNonCloneableObject(): void
1462
    {
UNCOV
1463
        $dummy = new NonCloneableDummy();
×
UNCOV
1464
        $dummy->setName('foo');
×
1465

UNCOV
1466
        $data = [
×
UNCOV
1467
            'name' => 'bar',
×
UNCOV
1468
        ];
×
1469

UNCOV
1470
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
UNCOV
1471
        $propertyNameCollectionFactoryProphecy->create(NonCloneableDummy::class, [])->willReturn(new PropertyNameCollection(['name']));
×
1472

UNCOV
1473
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1474

1475
        // BC layer for api-platform/metadata < 4.1
UNCOV
1476
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
1477
            $propertyMetadataFactoryProphecy->create(NonCloneableDummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
1478
        } else {
UNCOV
1479
            $propertyMetadataFactoryProphecy->create(NonCloneableDummy::class, 'name', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(false)->withWritable(true));
×
1480
        }
1481

UNCOV
1482
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
UNCOV
1483
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
UNCOV
1484
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
UNCOV
1485
        $resourceClassResolverProphecy->getResourceClass(null, NonCloneableDummy::class)->willReturn(NonCloneableDummy::class);
×
UNCOV
1486
        $resourceClassResolverProphecy->getResourceClass($dummy, NonCloneableDummy::class)->willReturn(NonCloneableDummy::class);
×
UNCOV
1487
        $resourceClassResolverProphecy->isResourceClass(NonCloneableDummy::class)->willReturn(true);
×
1488

UNCOV
1489
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
1490
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
1491

UNCOV
1492
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
UNCOV
1493
        $normalizer->setSerializer($serializerProphecy->reveal());
×
UNCOV
1494
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1495

UNCOV
1496
        $context = [AbstractItemNormalizer::OBJECT_TO_POPULATE => $dummy];
×
UNCOV
1497
        $actual = $normalizer->denormalize($data, NonCloneableDummy::class, null, $context);
×
1498

UNCOV
1499
        $this->assertInstanceOf(NonCloneableDummy::class, $actual);
×
UNCOV
1500
        $this->assertSame($dummy, $actual);
×
UNCOV
1501
        $propertyAccessorProphecy->setValue($actual, 'name', 'bar')->shouldHaveBeenCalled();
×
1502
    }
1503

1504
    public function testDenormalizeObjectWithNullDisabledTypeEnforcement(): void
1505
    {
UNCOV
1506
        $data = [
×
UNCOV
1507
            'dummy' => null,
×
UNCOV
1508
        ];
×
1509

UNCOV
1510
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
UNCOV
1511
        $propertyNameCollectionFactoryProphecy->create(DtoWithNullValue::class, [])->willReturn(new PropertyNameCollection(['dummy']));
×
1512

UNCOV
1513
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1514

1515
        // BC layer for api-platform/metadata < 4.1
UNCOV
1516
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
1517
            $propertyMetadataFactoryProphecy->create(DtoWithNullValue::class, 'dummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, nullable: true)])->withDescription('')->withReadable(true)->withWritable(true));
×
1518
        } else {
UNCOV
1519
            $propertyMetadataFactoryProphecy->create(DtoWithNullValue::class, 'dummy', [])->willReturn((new ApiProperty())->withNativeType(Type::nullable(Type::object()))->withDescription('')->withReadable(true)->withWritable(true)); // @phpstan-ignore-line
×
1520
        }
1521

UNCOV
1522
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
UNCOV
1523
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
UNCOV
1524
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
UNCOV
1525
        $resourceClassResolverProphecy->getResourceClass(null, DtoWithNullValue::class)->willReturn(DtoWithNullValue::class);
×
UNCOV
1526
        $resourceClassResolverProphecy->isResourceClass(DtoWithNullValue::class)->willReturn(true);
×
1527

UNCOV
1528
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
1529
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
1530

UNCOV
1531
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
UNCOV
1532
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1533

UNCOV
1534
        $context = [AbstractItemNormalizer::DISABLE_TYPE_ENFORCEMENT => true];
×
UNCOV
1535
        $actual = $normalizer->denormalize($data, DtoWithNullValue::class, null, $context);
×
1536

UNCOV
1537
        $this->assertInstanceOf(DtoWithNullValue::class, $actual);
×
UNCOV
1538
        $this->assertEquals(new DtoWithNullValue(), $actual);
×
1539
    }
1540

1541
    public function testCacheKey(): void
1542
    {
UNCOV
1543
        $relatedDummy = new RelatedDummy();
×
1544

UNCOV
1545
        $dummy = new Dummy();
×
UNCOV
1546
        $dummy->setName('foo');
×
UNCOV
1547
        $dummy->setAlias('ignored');
×
UNCOV
1548
        $dummy->setRelatedDummy($relatedDummy);
×
UNCOV
1549
        $dummy->relatedDummies->add(new RelatedDummy());
×
1550

UNCOV
1551
        $relatedDummies = new ArrayCollection([$relatedDummy]);
×
1552

UNCOV
1553
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
UNCOV
1554
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['name', 'alias', 'relatedDummy', 'relatedDummies']));
×
1555

1556
        // BC layer for api-platform/metadata < 4.1
UNCOV
1557
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
1558
            $relatedDummyType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
UNCOV
1559
            $relatedDummiesType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), $relatedDummyType);
×
1560

UNCOV
1561
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
UNCOV
1562
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
×
UNCOV
1563
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'alias', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
×
UNCOV
1564
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummyType])->withDescription('')->withReadable(true)->withWritable(false)->withReadableLink(false));
×
UNCOV
1565
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(true)->withWritable(false)->withReadableLink(false));
×
1566
        } else {
UNCOV
1567
            $relatedDummyType = Type::object(RelatedDummy::class);
×
UNCOV
1568
            $relatedDummiesType = Type::collection(Type::object(ArrayCollection::class), $relatedDummyType, Type::int());
×
1569

UNCOV
1570
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
UNCOV
1571
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true));
×
UNCOV
1572
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'alias', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true));
×
UNCOV
1573
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType($relatedDummyType)->withDescription('')->withReadable(true)->withWritable(false)->withReadableLink(false));
×
UNCOV
1574
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType($relatedDummiesType)->withReadable(true)->withWritable(false)->withReadableLink(false));
×
1575
        }
1576

UNCOV
1577
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
UNCOV
1578
        $iriConverterProphecy->getIriFromResource($dummy, Argument::cetera())->willReturn('/dummies/1');
×
UNCOV
1579
        $iriConverterProphecy->getIriFromResource($relatedDummy, Argument::cetera())->willReturn('/dummies/2');
×
1580

UNCOV
1581
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
UNCOV
1582
        $propertyAccessorProphecy->getValue($dummy, 'name')->willReturn('foo');
×
UNCOV
1583
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummy')->willReturn($relatedDummy);
×
UNCOV
1584
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummies')->willReturn($relatedDummies);
×
1585

UNCOV
1586
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
UNCOV
1587
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
UNCOV
1588
        $resourceClassResolverProphecy->getResourceClass($dummy, null)->willReturn(Dummy::class);
×
UNCOV
1589
        $resourceClassResolverProphecy->getResourceClass($relatedDummy, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
UNCOV
1590
        $resourceClassResolverProphecy->getResourceClass($relatedDummies, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
UNCOV
1591
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
UNCOV
1592
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1593

UNCOV
1594
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
UNCOV
1595
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
UNCOV
1596
        $serializerProphecy->normalize('foo', null, Argument::type('array'))->willReturn('foo');
×
UNCOV
1597
        $serializerProphecy->normalize(['/dummies/2'], null, Argument::type('array'))->willReturn(['/dummies/2']);
×
1598

UNCOV
1599
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
UNCOV
1600
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1601

UNCOV
1602
        $expected = [
×
UNCOV
1603
            'name' => 'foo',
×
UNCOV
1604
            'relatedDummy' => '/dummies/2',
×
UNCOV
1605
            'relatedDummies' => ['/dummies/2'],
×
UNCOV
1606
        ];
×
UNCOV
1607
        $this->assertSame($expected, $normalizer->normalize($dummy, null, [
×
UNCOV
1608
            'resources' => [],
×
UNCOV
1609
            'groups' => ['group'],
×
UNCOV
1610
            'ignored_attributes' => ['alias'],
×
UNCOV
1611
            'operation_name' => 'operation_name',
×
UNCOV
1612
            'root_operation_name' => 'root_operation_name',
×
UNCOV
1613
        ]));
×
1614

UNCOV
1615
        $operationCacheKey = (new \ReflectionClass($normalizer))->getProperty('localFactoryOptionsCache')->getValue($normalizer);
×
UNCOV
1616
        $this->assertEquals(array_keys($operationCacheKey), [\sprintf('%s%s%s%s', Dummy::class, 'operation_name', 'root_operation_name', 'n')]);
×
UNCOV
1617
        $this->assertEquals(current($operationCacheKey), ['serializer_groups' => ['group']]);
×
1618
    }
1619
}
1620

1621
class ObjectWithBasicProperties
1622
{
1623
    /** @var bool */
1624
    public $boolTrue1;
1625

1626
    /** @var bool */
1627
    public $boolFalse1;
1628

1629
    /** @var bool */
1630
    public $boolTrue2;
1631

1632
    /** @var bool */
1633
    public $boolFalse2;
1634

1635
    /** @var int */
1636
    public $int1;
1637

1638
    /** @var int */
1639
    public $int2;
1640

1641
    /** @var float */
1642
    public $float1;
1643

1644
    /** @var float */
1645
    public $float2;
1646

1647
    /** @var float */
1648
    public $float3;
1649

1650
    /** @var float */
1651
    public $floatNaN;
1652

1653
    /** @var float */
1654
    public $floatInf;
1655

1656
    /** @var float */
1657
    public $floatNegInf;
1658
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc