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

api-platform / core / 15255731762

26 May 2025 01:55PM UTC coverage: 0.0% (-26.5%) from 26.526%
15255731762

Pull #7176

github

web-flow
Merge 66f6cf4d2 into 79edced67
Pull Request #7176: Merge 4.1

0 of 387 new or added lines in 28 files covered. (0.0%)

11394 existing lines in 372 files now uncovered.

0 of 51334 relevant lines covered (0.0%)

0.0 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\Exception\ItemNotFoundException;
20
use ApiPlatform\Metadata\Get;
21
use ApiPlatform\Metadata\GetCollection;
22
use ApiPlatform\Metadata\IriConverterInterface;
23
use ApiPlatform\Metadata\Operations;
24
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
25
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
26
use ApiPlatform\Metadata\Property\PropertyNameCollection;
27
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
28
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
29
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
30
use ApiPlatform\Metadata\ResourceClassResolverInterface;
31
use ApiPlatform\Metadata\UrlGeneratorInterface;
32
use ApiPlatform\Serializer\AbstractItemNormalizer;
33
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\DtoWithNullValue;
34
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\Dummy;
35
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\DummyTableInheritance;
36
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\DummyTableInheritanceChild;
37
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\DummyTableInheritanceRelated;
38
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\NonCloneableDummy;
39
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\PropertyCollectionIriOnly;
40
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\PropertyCollectionIriOnlyRelation;
41
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\RelatedDummy;
42
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\SecuredDummy;
43
use Doctrine\Common\Collections\ArrayCollection;
44
use PHPUnit\Framework\TestCase;
45
use Prophecy\Argument;
46
use Prophecy\PhpUnit\ProphecyTrait;
47
use Symfony\Component\PropertyAccess\PropertyAccessor;
48
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
49
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
50
use Symfony\Component\PropertyInfo\Type as LegacyType;
51
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
52
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
53
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
54
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
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
    {
69
        $std = new \stdClass();
×
70
        $dummy = new Dummy();
×
71

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

77
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
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));
×
87
        $this->assertFalse($normalizer->supportsNormalization([]));
×
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');
×
98
        $dummy->setRelatedDummy($relatedDummy);
×
99
        $dummy->relatedDummies->add(new RelatedDummy());
×
100

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

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);
×
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);
×
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');
×
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);
×
134
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummies')->willReturn($relatedDummies);
×
135

136
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
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');
×
147
        $serializerProphecy->normalize(['/dummies/2'], null, Argument::type('array'))->willReturn(['/dummies/2']);
×
148

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

152
        $expected = [
×
153
            'name' => 'foo',
×
154
            'relatedDummy' => '/dummies/2',
×
155
            'relatedDummies' => ['/dummies/2'],
×
156
        ];
×
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));
×
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);
×
184
        $iriConverterProphecy->getIriFromResource($dummy, Argument::cetera())->willReturn('/secured_dummies/1');
×
185

186
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
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

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

202
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
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 {};
×
207
        $normalizer->setSerializer($serializerProphecy->reveal());
×
208

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

217
    public function testNormalizePropertyAsIriWithUriTemplate(): void
218
    {
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');
×
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(
×
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)),
×
248
                ])
×
249
            );
×
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)),
×
253
                ])
×
254
            );
×
255
            $propertyMetadataFactoryProphecy->create(PropertyCollectionIriOnly::class, 'toOneRelation', ['normalization_groups' => null, 'denormalization_groups' => null, 'operation_name' => null])->willReturn(
×
256
                (new ApiProperty())->withReadable(true)->withUriTemplate('/parent/{parentId}/another-collection-operations/{id}')->withBuiltinTypes([
×
257
                    new LegacyType('object', false, PropertyCollectionIriOnlyRelation::class, false),
×
258
                ])
×
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)))
×
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))
×
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');
×
274
        $iriConverterProphecy->getIriFromResource($propertyCollectionIriOnly, UrlGeneratorInterface::ABS_PATH, Argument::type(GetCollection::class), Argument::any())->willReturn('/property-collection-relations', '/parent/42/another-collection-operations');
×
275
        $iriConverterProphecy->getIriFromResource($propertyCollectionIriOnly, UrlGeneratorInterface::ABS_PATH, Argument::type(Get::class), Argument::any())->willReturn('/parent/42/another-collection-operations/24');
×
276

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

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

284
        $resourceClassResolverProphecy->isResourceClass(PropertyCollectionIriOnly::class)->willReturn(true);
×
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(),
×
291
            null, null, [], $resourceMetadataCollectionFactoryProphecy->reveal(), null, ) extends AbstractItemNormalizer {};
×
292

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

297
        $expected = [
×
298
            'propertyCollectionIriOnlyRelation' => '/property-collection-relations',
×
299
            'iterableIri' => '/parent/42/another-collection-operations',
×
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 = [
×
312
            'title' => 'foo',
×
313
            'adminOnlyProperty' => 'secret',
×
314
        ];
×
315

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

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

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

334
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
335
        $resourceClassResolverProphecy->getResourceClass(null, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
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(
×
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

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

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

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

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));
×
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

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

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

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

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

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));
×
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

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

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);
×
440
        $resourceClassResolverProphecy->isResourceClass(SecuredDummy::class)->willReturn(true);
×
441

442
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
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']
×
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 {};
×
458
        $normalizer->setSerializer($serializerProphecy->reveal());
×
459

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

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

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

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

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

479
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
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')) {
×
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 {
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

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(
×
507
            SecuredDummy::class,
×
508
            'false',
×
509
            ['object' => null, 'property' => 'ownerOnlyProperty']
×
510
        )->willReturn(false);
×
511
        $resourceAccessChecker->isGranted(
×
512
            SecuredDummy::class,
×
513
            'false',
×
514
            ['object' => $dummy, 'property' => 'ownerOnlyProperty']
×
515
        )->willReturn(false);
×
516

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

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

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

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',
×
537
        ];
×
538

539
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
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);
×
564
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
565

566
        $resourceAccessChecker = $this->prophesize(ResourceAccessCheckerInterface::class);
×
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

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

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

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);
×
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')) {
×
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);
×
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);
×
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);
×
628
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
629
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
630

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')),
×
637
            Argument::not(Argument::withKey('force_resource_class'))
×
638
        );
×
639
        $serializerProphecy->normalize($relatedDummy, null, $relatedDummyChildContext)->willReturn(['foo' => 'hello']);
×
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

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

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

656
    public function testNormalizePolymorphicRelations(): void
657
    {
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')) {
×
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);
×
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

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

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

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

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

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

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

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

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

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

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

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

736
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
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

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);
×
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

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

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 {};
×
768
        $normalizer->setSerializer($serializerProphecy->reveal());
×
769

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

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

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
    {
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 = [
×
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();
×
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
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);
×
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

860
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
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));
×
864
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummiesWithUnionTypes', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesWithUnionTypesIntType, $relatedDummiesWithUnionTypesFloatType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
865
        } else {
866
            $relatedDummyType = Type::object(RelatedDummy::class);
×
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

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));
×
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

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);
×
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);
×
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

914
        $data = [
×
915
            'relatedDummy' => 22,
×
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(
×
926
                (new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false)
×
927
            );
×
928
        } else {
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

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

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

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

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

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

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

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

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

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

964
        // BC layer for api-platform/metadata < 4.1
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)
×
968
            );
×
969
        } else {
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);
×
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

985
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
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 = [
×
999
            'relatedDummies' => ['wrong'],
×
1000
        ];
×
1001

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

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

1007
        // BC layer for api-platform/metadata < 4.1
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)
×
1011
            );
×
1012
        } else {
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)
×
1015
            );
×
1016
        }
1017

1018
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
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);
×
1024
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1025
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1026

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

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

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 = [];
×
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)
×
1041
        $this->assertInstanceOf(NotNormalizableValueException::class, $errors[0]);
×
1042
        $this->assertSame('relatedDummies[0]', $errors[0]->getPath());
×
NEW
1043
        $this->assertSame('Invalid IRI "wrong".', $errors[0]->getMessage());
×
1044
    }
1045

1046
    public function testDeserializationPathForNotDenormalizableResource(): void
1047
    {
1048
        $this->expectException(NotNormalizableValueException::class);
×
NEW
1049
        $this->expectExceptionMessage('Invalid IRI "wrong IRI".');
×
1050

1051
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1052

1053
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1054

1055
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1056
        $iriConverterProphecy->getResourceFromIri(Argument::cetera())->willThrow(new InvalidArgumentException('Invalid IRI'));
×
1057

1058
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1059
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1060
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1061

1062
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1063

1064
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1065
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1066

1067
        $normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
×
1068
            $propertyNameCollectionFactoryProphecy->reveal(),
×
1069
            $propertyMetadataFactoryProphecy->reveal(),
×
1070
            $iriConverterProphecy->reveal(),
×
1071
            $resourceClassResolverProphecy->reveal(),
×
1072
            $propertyAccessorProphecy->reveal(),
×
1073
            null,
×
1074
            null,
×
1075
            [],
×
1076
            null,
×
1077
            null,
×
1078
        ]);
×
1079
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1080

1081
        $normalizer->denormalize('wrong IRI', Dummy::class, null, ['not_normalizable_value_exceptions' => []]);
×
1082
    }
1083

1084
    public function testDeserializationPathForNotFoundResource(): void
1085
    {
NEW
1086
        $this->expectException(NotNormalizableValueException::class);
×
NEW
1087
        $this->expectExceptionMessage('Some item not found exception.');
×
1088

NEW
1089
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1090

NEW
1091
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1092

NEW
1093
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
NEW
1094
        $iriConverterProphecy->getResourceFromIri(Argument::cetera())->willThrow(new ItemNotFoundException('Some item not found exception.'));
×
1095

NEW
1096
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
NEW
1097
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
NEW
1098
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1099

NEW
1100
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1101

NEW
1102
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
NEW
1103
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1104

NEW
1105
        $normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
×
NEW
1106
            $propertyNameCollectionFactoryProphecy->reveal(),
×
NEW
1107
            $propertyMetadataFactoryProphecy->reveal(),
×
NEW
1108
            $iriConverterProphecy->reveal(),
×
NEW
1109
            $resourceClassResolverProphecy->reveal(),
×
NEW
1110
            $propertyAccessorProphecy->reveal(),
×
NEW
1111
            null,
×
NEW
1112
            null,
×
NEW
1113
            [],
×
NEW
1114
            null,
×
NEW
1115
            null,
×
NEW
1116
        ]);
×
NEW
1117
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1118

NEW
1119
        $normalizer->denormalize('/some-iri', Dummy::class, null, ['not_normalizable_value_exceptions' => []]);
×
1120
    }
1121

1122
    public function testInnerDocumentNotAllowed(): void
1123
    {
1124
        $this->expectException(UnexpectedValueException::class);
×
1125
        $this->expectExceptionMessage('Nested documents for attribute "relatedDummy" are not allowed. Use IRIs instead.');
×
1126

1127
        $data = [
×
1128
            'relatedDummy' => [
×
1129
                'foo' => 'bar',
×
1130
            ],
×
1131
        ];
×
1132

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

1136
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1137

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

1149
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1150

1151
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1152
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1153
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
1154
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1155
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1156

1157
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1158

1159
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1160
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1161

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

1165
        $normalizer->denormalize($data, Dummy::class);
×
1166
    }
1167

1168
    public function testBadType(): void
1169
    {
1170
        $this->expectException(UnexpectedValueException::class);
×
1171
        $this->expectExceptionMessage('The type of the "foo" attribute must be "float", "integer" given.');
×
1172

1173
        $data = [
×
1174
            'foo' => 42,
×
1175
        ];
×
1176

1177
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1178
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['foo']));
×
1179

1180
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1181

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

1189
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1190

1191
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1192
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1193
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1194

1195
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1196

1197
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1198
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1199

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

1203
        $normalizer->denormalize($data, Dummy::class);
×
1204
    }
1205

1206
    public function testTypeChecksCanBeDisabled(): void
1207
    {
1208
        $data = [
×
1209
            'foo' => 42,
×
1210
        ];
×
1211

1212
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1213
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['foo']));
×
1214

1215
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1216

1217
        // BC layer for api-platform/metadata < 4.1
1218
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1219
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1220
        } else {
1221
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1222
        }
1223

1224
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1225

1226
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1227
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1228
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1229

1230
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1231

1232
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1233
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1234

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

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

1240
        $this->assertInstanceOf(Dummy::class, $actual);
×
1241

1242
        $propertyAccessorProphecy->setValue($actual, 'foo', 42)->shouldHaveBeenCalled();
×
1243
    }
1244

1245
    public function testJsonAllowIntAsFloat(): void
1246
    {
1247
        $data = [
×
1248
            'foo' => 42,
×
1249
        ];
×
1250

1251
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1252
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['foo']));
×
1253

1254
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1255

1256
        // BC layer for api-platform/metadata < 4.1
1257
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1258
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1259
        } else {
1260
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1261
        }
1262

1263
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1264

1265
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1266
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1267
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1268

1269
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1270

1271
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1272
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1273

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

1277
        $actual = $normalizer->denormalize($data, Dummy::class, 'jsonfoo');
×
1278

1279
        $this->assertInstanceOf(Dummy::class, $actual);
×
1280

1281
        $propertyAccessorProphecy->setValue($actual, 'foo', 42)->shouldHaveBeenCalled();
×
1282
    }
1283

1284
    public function testDenormalizeBadKeyType(): void
1285
    {
1286
        $this->expectException(NotNormalizableValueException::class);
×
1287
        $this->expectExceptionMessage('The type of the key "a" must be "int", "string" given.');
×
1288

1289
        $data = [
×
1290
            'name' => 'foo',
×
1291
            'relatedDummy' => [
×
1292
                'foo' => 'bar',
×
1293
            ],
×
1294
            'relatedDummies' => [
×
1295
                'a' => [
×
1296
                    'bar' => 'baz',
×
1297
                ],
×
1298
            ],
×
1299
            'relatedDummiesWithUnionTypes' => [
×
1300
                'a' => [
×
1301
                    'bar' => 'baz',
×
1302
                ],
×
1303
            ],
×
1304
        ];
×
1305

1306
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1307
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['relatedDummies']));
×
1308

1309
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1310

1311
        // BC layer for api-platform/metadata < 4.1
1312
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1313
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
1314
            $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));
×
1315

1316
            $type = new LegacyType(
×
1317
                LegacyType::BUILTIN_TYPE_OBJECT,
×
1318
                false,
×
1319
                ArrayCollection::class,
×
1320
                true,
×
1321
                new LegacyType(LegacyType::BUILTIN_TYPE_INT),
×
1322
                new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)
×
1323
            );
×
1324
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$type])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
1325
        } else {
1326
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(false)->withWritable(true));
×
1327
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withNativeType(Type::object(RelatedDummy::class))->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1328

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

1333
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1334

1335
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1336
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1337
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
1338
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1339
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1340

1341
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1342

1343
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1344
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1345

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

1349
        $normalizer->denormalize($data, Dummy::class);
×
1350
    }
1351

1352
    public function testNullable(): void
1353
    {
1354
        $data = [
×
1355
            'name' => null,
×
1356
        ];
×
1357

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

1361
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1362

1363
        // BC layer for api-platform/metadata < 4.1
1364
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1365
            $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));
×
1366
        } else {
1367
            $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
×
1368
        }
1369

1370
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1371

1372
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1373
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1374
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1375

1376
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1377

1378
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1379
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1380

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

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

1386
        $this->assertInstanceOf(Dummy::class, $actual);
×
1387

1388
        $propertyAccessorProphecy->setValue($actual, 'name', null)->shouldHaveBeenCalled();
×
1389
    }
1390

1391
    public function testDenormalizeBasicTypePropertiesFromXml(): void
1392
    {
1393
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1394
        $propertyNameCollectionFactoryProphecy->create(ObjectWithBasicProperties::class, [])->willReturn(new PropertyNameCollection([
×
1395
            'boolTrue1',
×
1396
            'boolFalse1',
×
1397
            'boolTrue2',
×
1398
            'boolFalse2',
×
1399
            'int1',
×
1400
            'int2',
×
1401
            'float1',
×
1402
            'float2',
×
1403
            'float3',
×
1404
            'floatNaN',
×
1405
            'floatInf',
×
1406
            'floatNegInf',
×
1407
        ]));
×
1408

1409
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1410

1411
        // BC layer for api-platform/metadata < 4.1
1412
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1413
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolTrue1', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_BOOL)])->withDescription('')->withReadable(false)->withWritable(true));
×
1414
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolFalse1', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_BOOL)])->withDescription('')->withReadable(false)->withWritable(true));
×
1415
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolTrue2', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_BOOL)])->withDescription('')->withReadable(false)->withWritable(true));
×
1416
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolFalse2', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_BOOL)])->withDescription('')->withReadable(false)->withWritable(true));
×
1417
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'int1', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_INT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1418
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'int2', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_INT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1419
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float1', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1420
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float2', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1421
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float3', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1422
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatNaN', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1423
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatInf', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1424
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatNegInf', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1425
        } else {
1426
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolTrue1', [])->willReturn((new ApiProperty())->withNativeType(Type::bool())->withDescription('')->withReadable(false)->withWritable(true));
×
1427
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolFalse1', [])->willReturn((new ApiProperty())->withNativeType(Type::bool())->withDescription('')->withReadable(false)->withWritable(true));
×
1428
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolTrue2', [])->willReturn((new ApiProperty())->withNativeType(Type::bool())->withDescription('')->withReadable(false)->withWritable(true));
×
1429
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolFalse2', [])->willReturn((new ApiProperty())->withNativeType(Type::bool())->withDescription('')->withReadable(false)->withWritable(true));
×
1430
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'int1', [])->willReturn((new ApiProperty())->withNativeType(Type::int())->withDescription('')->withReadable(false)->withWritable(true));
×
1431
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'int2', [])->willReturn((new ApiProperty())->withNativeType(Type::int())->withDescription('')->withReadable(false)->withWritable(true));
×
1432
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float1', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true));
×
1433
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float2', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true));
×
1434
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float3', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true));
×
1435
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatNaN', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true));
×
1436
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatInf', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true));
×
1437
            $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatNegInf', [])->willReturn((new ApiProperty())->withNativeType(Type::float())->withDescription('')->withReadable(false)->withWritable(true));
×
1438
        }
1439

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

1442
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1443
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'boolTrue1', true)->shouldBeCalled();
×
1444
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'boolFalse1', false)->shouldBeCalled();
×
1445
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'boolTrue2', true)->shouldBeCalled();
×
1446
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'boolFalse2', false)->shouldBeCalled();
×
1447
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'int1', 4711)->shouldBeCalled();
×
1448
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'int2', -4711)->shouldBeCalled();
×
1449
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'float1', Argument::approximate(123.456, 0))->shouldBeCalled();
×
1450
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'float2', Argument::approximate(-1.2344e56, 1))->shouldBeCalled();
×
1451
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'float3', Argument::approximate(45E-6, 1))->shouldBeCalled();
×
1452
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'floatNaN', Argument::that(static fn (float $arg) => is_nan($arg)))->shouldBeCalled();
×
1453
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'floatInf', \INF)->shouldBeCalled();
×
1454
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'floatNegInf', -\INF)->shouldBeCalled();
×
1455

1456
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1457
        $resourceClassResolverProphecy->getResourceClass(null, ObjectWithBasicProperties::class)->willReturn(ObjectWithBasicProperties::class);
×
1458
        $resourceClassResolverProphecy->isResourceClass(ObjectWithBasicProperties::class)->willReturn(true);
×
1459

1460
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1461
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
1462

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

1466
        $objectWithBasicProperties = $normalizer->denormalize(
×
1467
            [
×
1468
                'boolTrue1' => 'true',
×
1469
                'boolFalse1' => 'false',
×
1470
                'boolTrue2' => '1',
×
1471
                'boolFalse2' => '0',
×
1472
                'int1' => '4711',
×
1473
                'int2' => '-4711',
×
1474
                'float1' => '123.456',
×
1475
                'float2' => '-1.2344e56',
×
1476
                'float3' => '45E-6',
×
1477
                'floatNaN' => 'NaN',
×
1478
                'floatInf' => 'INF',
×
1479
                'floatNegInf' => '-INF',
×
1480
            ],
×
1481
            ObjectWithBasicProperties::class,
×
1482
            'xml'
×
1483
        );
×
1484

1485
        $this->assertInstanceOf(ObjectWithBasicProperties::class, $objectWithBasicProperties);
×
1486
    }
1487

1488
    public function testDenormalizeCollectionDecodedFromXmlWithOneChild(): void
1489
    {
1490
        $data = [
×
1491
            'relatedDummies' => [
×
1492
                'name' => 'foo',
×
1493
            ],
×
1494
        ];
×
1495

1496
        $relatedDummy = new RelatedDummy();
×
1497
        $relatedDummy->setName('foo');
×
1498

1499
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1500
        $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['relatedDummies']));
×
1501

1502
        // BC layer for api-platform/metadata < 4.1
1503
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1504
            $relatedDummyType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
1505
            $relatedDummiesType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), $relatedDummyType);
×
1506

1507
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1508
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
1509
        } else {
1510
            $relatedDummyType = Type::object(RelatedDummy::class);
×
1511
            $relatedDummiesType = Type::collection(Type::object(ArrayCollection::class), $relatedDummyType, Type::int());
×
1512

1513
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1514
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withNativeType($relatedDummiesType)->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
1515
        }
1516

1517
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1518

1519
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1520
        $propertyAccessorProphecy->setValue(Argument::type(Dummy::class), 'relatedDummies', Argument::type('array'))->shouldBeCalled();
×
1521

1522
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1523
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1524
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
1525
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1526
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1527

1528
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1529
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1530
        $serializerProphecy->denormalize(['name' => 'foo'], RelatedDummy::class, 'xml', Argument::type('array'))->willReturn($relatedDummy);
×
1531

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

1535
        $normalizer->denormalize($data, Dummy::class, 'xml');
×
1536
    }
1537

1538
    public function testDenormalizePopulatingNonCloneableObject(): void
1539
    {
1540
        $dummy = new NonCloneableDummy();
×
1541
        $dummy->setName('foo');
×
1542

1543
        $data = [
×
1544
            'name' => 'bar',
×
1545
        ];
×
1546

1547
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1548
        $propertyNameCollectionFactoryProphecy->create(NonCloneableDummy::class, [])->willReturn(new PropertyNameCollection(['name']));
×
1549

1550
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1551

1552
        // BC layer for api-platform/metadata < 4.1
1553
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1554
            $propertyMetadataFactoryProphecy->create(NonCloneableDummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
1555
        } else {
1556
            $propertyMetadataFactoryProphecy->create(NonCloneableDummy::class, 'name', [])->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(false)->withWritable(true));
×
1557
        }
1558

1559
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1560
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1561
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1562
        $resourceClassResolverProphecy->getResourceClass(null, NonCloneableDummy::class)->willReturn(NonCloneableDummy::class);
×
1563
        $resourceClassResolverProphecy->getResourceClass($dummy, NonCloneableDummy::class)->willReturn(NonCloneableDummy::class);
×
1564
        $resourceClassResolverProphecy->isResourceClass(NonCloneableDummy::class)->willReturn(true);
×
1565

1566
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1567
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
1568

1569
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
1570
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1571
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1572

1573
        $context = [AbstractItemNormalizer::OBJECT_TO_POPULATE => $dummy];
×
1574
        $actual = $normalizer->denormalize($data, NonCloneableDummy::class, null, $context);
×
1575

1576
        $this->assertInstanceOf(NonCloneableDummy::class, $actual);
×
1577
        $this->assertSame($dummy, $actual);
×
1578
        $propertyAccessorProphecy->setValue($actual, 'name', 'bar')->shouldHaveBeenCalled();
×
1579
    }
1580

1581
    public function testDenormalizeObjectWithNullDisabledTypeEnforcement(): void
1582
    {
1583
        $data = [
×
1584
            'dummy' => null,
×
1585
        ];
×
1586

1587
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1588
        $propertyNameCollectionFactoryProphecy->create(DtoWithNullValue::class, [])->willReturn(new PropertyNameCollection(['dummy']));
×
1589

1590
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1591

1592
        // BC layer for api-platform/metadata < 4.1
1593
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1594
            $propertyMetadataFactoryProphecy->create(DtoWithNullValue::class, 'dummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, nullable: true)])->withDescription('')->withReadable(true)->withWritable(true));
×
1595
        } else {
1596
            $propertyMetadataFactoryProphecy->create(DtoWithNullValue::class, 'dummy', [])->willReturn((new ApiProperty())->withNativeType(Type::nullable(Type::object()))->withDescription('')->withReadable(true)->withWritable(true)); // @phpstan-ignore-line
×
1597
        }
1598

1599
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1600
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1601
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1602
        $resourceClassResolverProphecy->getResourceClass(null, DtoWithNullValue::class)->willReturn(DtoWithNullValue::class);
×
1603
        $resourceClassResolverProphecy->isResourceClass(DtoWithNullValue::class)->willReturn(true);
×
1604

1605
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1606
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
1607

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

1611
        $context = [AbstractItemNormalizer::DISABLE_TYPE_ENFORCEMENT => true];
×
1612
        $actual = $normalizer->denormalize($data, DtoWithNullValue::class, null, $context);
×
1613

1614
        $this->assertInstanceOf(DtoWithNullValue::class, $actual);
×
1615
        $this->assertEquals(new DtoWithNullValue(), $actual);
×
1616
    }
1617

1618
    public function testCacheKey(): void
1619
    {
1620
        $relatedDummy = new RelatedDummy();
×
1621

1622
        $dummy = new Dummy();
×
1623
        $dummy->setName('foo');
×
1624
        $dummy->setAlias('ignored');
×
1625
        $dummy->setRelatedDummy($relatedDummy);
×
1626
        $dummy->relatedDummies->add(new RelatedDummy());
×
1627

1628
        $relatedDummies = new ArrayCollection([$relatedDummy]);
×
1629

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

1633
        // BC layer for api-platform/metadata < 4.1
1634
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
1635
            $relatedDummyType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
1636
            $relatedDummiesType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), $relatedDummyType);
×
1637

1638
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1639
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
×
1640
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'alias', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
×
1641
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummyType])->withDescription('')->withReadable(true)->withWritable(false)->withReadableLink(false));
×
1642
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(true)->withWritable(false)->withReadableLink(false));
×
1643
        } else {
1644
            $relatedDummyType = Type::object(RelatedDummy::class);
×
1645
            $relatedDummiesType = Type::collection(Type::object(ArrayCollection::class), $relatedDummyType, Type::int());
×
1646

1647
            $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1648
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true));
×
1649
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'alias', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withDescription('')->withReadable(true));
×
1650
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType($relatedDummyType)->withDescription('')->withReadable(true)->withWritable(false)->withReadableLink(false));
×
1651
            $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType($relatedDummiesType)->withReadable(true)->withWritable(false)->withReadableLink(false));
×
1652
        }
1653

1654
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1655
        $iriConverterProphecy->getIriFromResource($dummy, Argument::cetera())->willReturn('/dummies/1');
×
1656
        $iriConverterProphecy->getIriFromResource($relatedDummy, Argument::cetera())->willReturn('/dummies/2');
×
1657

1658
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1659
        $propertyAccessorProphecy->getValue($dummy, 'name')->willReturn('foo');
×
1660
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummy')->willReturn($relatedDummy);
×
1661
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummies')->willReturn($relatedDummies);
×
1662

1663
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1664
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1665
        $resourceClassResolverProphecy->getResourceClass($dummy, null)->willReturn(Dummy::class);
×
1666
        $resourceClassResolverProphecy->getResourceClass($relatedDummy, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
1667
        $resourceClassResolverProphecy->getResourceClass($relatedDummies, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
1668
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1669
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1670

1671
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1672
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
1673
        $serializerProphecy->normalize('foo', null, Argument::type('array'))->willReturn('foo');
×
1674
        $serializerProphecy->normalize(['/dummies/2'], null, Argument::type('array'))->willReturn(['/dummies/2']);
×
1675

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

1679
        $expected = [
×
1680
            'name' => 'foo',
×
1681
            'relatedDummy' => '/dummies/2',
×
1682
            'relatedDummies' => ['/dummies/2'],
×
1683
        ];
×
1684
        $this->assertSame($expected, $normalizer->normalize($dummy, null, [
×
1685
            'resources' => [],
×
1686
            'groups' => ['group'],
×
1687
            'ignored_attributes' => ['alias'],
×
1688
            'operation_name' => 'operation_name',
×
1689
            'root_operation_name' => 'root_operation_name',
×
1690
        ]));
×
1691

1692
        $operationCacheKey = (new \ReflectionClass($normalizer))->getProperty('localFactoryOptionsCache')->getValue($normalizer);
×
1693
        $this->assertEquals(array_keys($operationCacheKey), [\sprintf('%s%s%s%s', Dummy::class, 'operation_name', 'root_operation_name', 'n')]);
×
1694
        $this->assertEquals(current($operationCacheKey), ['serializer_groups' => ['group']]);
×
1695
    }
1696
}
1697

1698
class ObjectWithBasicProperties
1699
{
1700
    /** @var bool */
1701
    public $boolTrue1;
1702

1703
    /** @var bool */
1704
    public $boolFalse1;
1705

1706
    /** @var bool */
1707
    public $boolTrue2;
1708

1709
    /** @var bool */
1710
    public $boolFalse2;
1711

1712
    /** @var int */
1713
    public $int1;
1714

1715
    /** @var int */
1716
    public $int2;
1717

1718
    /** @var float */
1719
    public $float1;
1720

1721
    /** @var float */
1722
    public $float2;
1723

1724
    /** @var float */
1725
    public $float3;
1726

1727
    /** @var float */
1728
    public $floatNaN;
1729

1730
    /** @var float */
1731
    public $floatInf;
1732

1733
    /** @var float */
1734
    public $floatNegInf;
1735
}
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