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

api-platform / core / 15183722556

22 May 2025 10:03AM UTC coverage: 27.348% (-0.03%) from 27.379%
15183722556

push

github

web-flow
fix(serializer): exception message to not expose resource FQCN (#7156)

0 of 31 new or added lines in 2 files covered. (0.0%)

80 existing lines in 4 files now uncovered.

13482 of 49298 relevant lines covered (27.35%)

74.57 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\Type;
50
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
51
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
52
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
53
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
54
use Symfony\Component\Serializer\SerializerInterface;
55

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

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

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

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

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

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

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

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

99
        $relatedDummies = new ArrayCollection([$relatedDummy]);
×
100

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

104
        $relatedDummyType = new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
105
        $relatedDummiesType = new Type(Type::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new Type(Type::BUILTIN_TYPE_INT), $relatedDummyType);
×
106

107
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
108
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
×
109
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'alias', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
×
110
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummyType])->withDescription('')->withReadable(true)->withWritable(false)->withReadableLink(false));
×
111
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(true)->withWritable(false)->withReadableLink(false));
×
112

113
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
114
        $iriConverterProphecy->getIriFromResource($dummy, Argument::cetera())->willReturn('/dummies/1');
×
115
        $iriConverterProphecy->getIriFromResource($relatedDummy, Argument::cetera())->willReturn('/dummies/2');
×
116

117
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
118
        $propertyAccessorProphecy->getValue($dummy, 'name')->willReturn('foo');
×
119
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummy')->willReturn($relatedDummy);
×
120
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummies')->willReturn($relatedDummies);
×
121

122
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
123
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
124
        $resourceClassResolverProphecy->getResourceClass($dummy, null)->willReturn(Dummy::class);
×
125
        $resourceClassResolverProphecy->getResourceClass($relatedDummy, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
126
        $resourceClassResolverProphecy->getResourceClass($relatedDummies, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
127
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
128
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
129

130
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
131
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
132
        $serializerProphecy->normalize('foo', null, Argument::type('array'))->willReturn('foo');
×
133
        $serializerProphecy->normalize(['/dummies/2'], null, Argument::type('array'))->willReturn(['/dummies/2']);
×
134

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

138
        $expected = [
×
139
            'name' => 'foo',
×
140
            'relatedDummy' => '/dummies/2',
×
141
            'relatedDummies' => ['/dummies/2'],
×
142
        ];
×
143
        $this->assertSame($expected, $normalizer->normalize($dummy, null, [
×
144
            'resources' => [],
×
145
            'ignored_attributes' => ['alias'],
×
146
        ]));
×
147
    }
148

149
    public function testNormalizeWithSecuredProperty(): void
150
    {
151
        $dummy = new SecuredDummy();
×
152
        $dummy->setTitle('myPublicTitle');
×
153
        $dummy->setAdminOnlyProperty('secret');
×
154

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

158
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
159
        $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
×
160
        $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'adminOnlyProperty', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true)->withSecurity('is_granted(\'ROLE_ADMIN\')'));
×
161

162
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
163
        $iriConverterProphecy->getIriFromResource($dummy, Argument::cetera())->willReturn('/secured_dummies/1');
×
164

165
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
166
        $propertyAccessorProphecy->getValue($dummy, 'title')->willReturn('myPublicTitle');
×
167
        $propertyAccessorProphecy->getValue($dummy, 'adminOnlyProperty')->willReturn('secret');
×
168

169
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
170
        $resourceClassResolverProphecy->getResourceClass($dummy, null)->willReturn(SecuredDummy::class);
×
171
        $resourceClassResolverProphecy->getResourceClass(null, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
172
        $resourceClassResolverProphecy->isResourceClass(SecuredDummy::class)->willReturn(true);
×
173

174
        $resourceAccessChecker = $this->prophesize(ResourceAccessCheckerInterface::class);
×
175
        $resourceAccessChecker->isGranted(
×
176
            SecuredDummy::class,
×
177
            'is_granted(\'ROLE_ADMIN\')',
×
178
            ['object' => $dummy, 'property' => 'adminOnlyProperty']
×
179
        )->willReturn(false);
×
180

181
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
182
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
183
        $serializerProphecy->normalize('myPublicTitle', null, Argument::type('array'))->willReturn('myPublicTitle');
×
184

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

188
        $expected = [
×
189
            'title' => 'myPublicTitle',
×
190
        ];
×
191
        $this->assertSame($expected, $normalizer->normalize($dummy, null, [
×
192
            'resources' => [],
×
193
        ]));
×
194
    }
195

196
    public function testNormalizePropertyAsIriWithUriTemplate(): void
197
    {
198
        $propertyCollectionIriOnlyRelation = new PropertyCollectionIriOnlyRelation();
×
199
        $propertyCollectionIriOnlyRelation->name = 'My Relation';
×
200

201
        $propertyCollectionIriOnly = new PropertyCollectionIriOnly();
×
202
        $propertyCollectionIriOnly->addPropertyCollectionIriOnlyRelation($propertyCollectionIriOnlyRelation);
×
203

204
        $collectionOperation = new GetCollection('/property-collection-relations');
×
205
        $getIterableOperation = new GetCollection('/parent/{parentId}/another-collection-operations');
×
206
        $getToOneOperation = new Get('/parent/{parentId}/another-collection-operations/{id}');
×
207

208
        $resourceRelationMetadataCollection = new ResourceMetadataCollection(PropertyCollectionIriOnlyRelation::class, [
×
209
            (new ApiResource())->withOperations(new Operations([$collectionOperation, $getIterableOperation, $getToOneOperation])),
×
210
        ]);
×
211

212
        $resourceMetadataCollectionFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
213
        $resourceMetadataCollectionFactoryProphecy->create(PropertyCollectionIriOnlyRelation::class)->willReturn($resourceRelationMetadataCollection);
×
214

215
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
216
        $propertyNameCollectionFactoryProphecy->create(PropertyCollectionIriOnly::class, ['normalization_groups' => null, 'denormalization_groups' => null, 'operation_name' => null])->willReturn(
×
217
            new PropertyNameCollection(['propertyCollectionIriOnlyRelation', 'iterableIri', 'toOneRelation'])
×
218
        );
×
219

220
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
221
        $propertyMetadataFactoryProphecy->create(PropertyCollectionIriOnly::class, 'propertyCollectionIriOnlyRelation', ['normalization_groups' => null, 'denormalization_groups' => null, 'operation_name' => null])->willReturn(
×
222
            (new ApiProperty())->withReadable(true)->withUriTemplate('/property-collection-relations')->withBuiltinTypes([
×
223
                new Type('iterable', false, null, true, new Type('int', false, null, false), new Type('object', false, PropertyCollectionIriOnlyRelation::class, false)),
×
224
            ])
×
225
        );
×
226

227
        $propertyMetadataFactoryProphecy->create(PropertyCollectionIriOnly::class, 'iterableIri', ['normalization_groups' => null, 'denormalization_groups' => null, 'operation_name' => null])->willReturn(
×
228
            (new ApiProperty())->withReadable(true)->withUriTemplate('/parent/{parentId}/another-collection-operations')->withBuiltinTypes([
×
229
                new Type('iterable', false, null, true, new Type('int', false, null, false), new Type('object', false, PropertyCollectionIriOnlyRelation::class, false)),
×
230
            ])
×
231
        );
×
232

233
        $propertyMetadataFactoryProphecy->create(PropertyCollectionIriOnly::class, 'toOneRelation', ['normalization_groups' => null, 'denormalization_groups' => null, 'operation_name' => null])->willReturn(
×
234
            (new ApiProperty())->withReadable(true)->withUriTemplate('/parent/{parentId}/another-collection-operations/{id}')->withBuiltinTypes([
×
235
                new Type('object', false, PropertyCollectionIriOnlyRelation::class, false),
×
236
            ])
×
237
        );
×
238

239
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
240
        $iriConverterProphecy->getIriFromResource($propertyCollectionIriOnly, UrlGeneratorInterface::ABS_URL, null, Argument::any())->willReturn('/property-collection-relations', '/parent/42/another-collection-operations');
×
241
        $iriConverterProphecy->getIriFromResource($propertyCollectionIriOnly, UrlGeneratorInterface::ABS_PATH, Argument::type(GetCollection::class), Argument::any())->willReturn('/property-collection-relations', '/parent/42/another-collection-operations');
×
242
        $iriConverterProphecy->getIriFromResource($propertyCollectionIriOnly, UrlGeneratorInterface::ABS_PATH, Argument::type(Get::class), Argument::any())->willReturn('/parent/42/another-collection-operations/24');
×
243

244
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
245
        $propertyAccessorProphecy->getValue($propertyCollectionIriOnly, 'propertyCollectionIriOnlyRelation')->willReturn([$propertyCollectionIriOnlyRelation]);
×
246
        $propertyAccessorProphecy->getValue($propertyCollectionIriOnly, 'iterableIri')->willReturn($propertyCollectionIriOnlyRelation);
×
247
        $propertyAccessorProphecy->getValue($propertyCollectionIriOnly, 'toOneRelation')->willReturn($propertyCollectionIriOnlyRelation);
×
248

249
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
250

251
        $resourceClassResolverProphecy->isResourceClass(PropertyCollectionIriOnly::class)->willReturn(true);
×
252
        $resourceClassResolverProphecy->getResourceClass(null, PropertyCollectionIriOnly::class)->willReturn(PropertyCollectionIriOnly::class);
×
253

254
        $resourceClassResolverProphecy->isResourceClass(PropertyCollectionIriOnlyRelation::class)->willReturn(true);
×
255
        $resourceClassResolverProphecy->getResourceClass([$propertyCollectionIriOnlyRelation], PropertyCollectionIriOnlyRelation::class)->willReturn(PropertyCollectionIriOnlyRelation::class);
×
256

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

260
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
261
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
262
        $normalizer->setSerializer($serializerProphecy->reveal());
×
263

264
        $expected = [
×
265
            'propertyCollectionIriOnlyRelation' => '/property-collection-relations',
×
266
            'iterableIri' => '/parent/42/another-collection-operations',
×
267
            'toOneRelation' => '/parent/42/another-collection-operations/24',
×
268
        ];
×
269

270
        $this->assertSame($expected, $normalizer->normalize($propertyCollectionIriOnly, 'jsonld', [
×
271
            'resources' => [],
×
272
            'root_operation' => new GetCollection('/property_collection_iri_onlies'),
×
273
        ]));
×
274
    }
275

276
    public function testDenormalizeWithSecuredProperty(): void
277
    {
278
        $data = [
×
279
            'title' => 'foo',
×
280
            'adminOnlyProperty' => 'secret',
×
281
        ];
×
282

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

286
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
287
        $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
288
        $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'adminOnlyProperty', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true)->withSecurity('is_granted(\'ROLE_ADMIN\')'));
×
289

290
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
291

292
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
293

294
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
295
        $resourceClassResolverProphecy->getResourceClass(null, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
296
        $resourceClassResolverProphecy->isResourceClass(SecuredDummy::class)->willReturn(true);
×
297

298
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
299
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
300

301
        $resourceAccessChecker = $this->prophesize(ResourceAccessCheckerInterface::class);
×
302
        $resourceAccessChecker->isGranted(
×
303
            SecuredDummy::class,
×
304
            'is_granted(\'ROLE_ADMIN\')',
×
305
            ['object' => null, 'property' => 'adminOnlyProperty']
×
306
        )->willReturn(false);
×
307

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

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

313
        $this->assertInstanceOf(SecuredDummy::class, $actual);
×
314

315
        $propertyAccessorProphecy->setValue($actual, 'title', 'foo')->shouldHaveBeenCalled();
×
316
        $propertyAccessorProphecy->setValue($actual, 'adminOnlyProperty', 'secret')->shouldNotHaveBeenCalled();
×
317
    }
318

319
    public function testDenormalizeCreateWithDeniedPostDenormalizeSecuredProperty(): void
320
    {
321
        $data = [
×
322
            'title' => 'foo',
×
323
            'ownerOnlyProperty' => 'should not be set',
×
324
        ];
×
325

326
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
327
        $propertyNameCollectionFactoryProphecy->create(SecuredDummy::class, [])->willReturn(new PropertyNameCollection(['title', 'ownerOnlyProperty']));
×
328

329
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
330
        $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
331
        $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true)->withWritable(true)->withSecurityPostDenormalize('false')->withDefault(''));
×
332

333
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
334

335
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
336

337
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
338
        $resourceClassResolverProphecy->getResourceClass(null, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
339
        $resourceClassResolverProphecy->isResourceClass(SecuredDummy::class)->willReturn(true);
×
340

341
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
342
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
343

344
        $resourceAccessChecker = $this->prophesize(ResourceAccessCheckerInterface::class);
×
345
        $resourceAccessChecker->isGranted(
×
346
            SecuredDummy::class,
×
347
            'false',
×
348
            Argument::any()
×
349
        )->willReturn(false);
×
350

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

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

356
        $this->assertInstanceOf(SecuredDummy::class, $actual);
×
357

358
        $propertyAccessorProphecy->setValue($actual, 'title', 'foo')->shouldHaveBeenCalled();
×
359
        $propertyAccessorProphecy->setValue($actual, 'ownerOnlyProperty', 'should not be set')->shouldHaveBeenCalled();
×
360
        $propertyAccessorProphecy->setValue($actual, 'ownerOnlyProperty', '')->shouldHaveBeenCalled();
×
361
    }
362

363
    public function testDenormalizeUpdateWithSecuredProperty(): void
364
    {
365
        $dummy = new SecuredDummy();
×
366

367
        $data = [
×
368
            'title' => 'foo',
×
369
            'ownerOnlyProperty' => 'secret',
×
370
        ];
×
371

372
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
373
        $propertyNameCollectionFactoryProphecy->create(SecuredDummy::class, [])->willReturn(new PropertyNameCollection(['title', 'ownerOnlyProperty']));
×
374

375
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
376
        $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
377
        $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true)->withWritable(true)->withSecurity('true'));
×
378

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

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

383
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
384
        $resourceClassResolverProphecy->getResourceClass($dummy, SecuredDummy::class)->willReturn(SecuredDummy::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
            'true',
×
395
            ['object' => null, 'property' => 'ownerOnlyProperty']
×
396
        )->willReturn(true);
×
397
        $resourceAccessChecker->isGranted(
×
398
            SecuredDummy::class,
×
399
            'true',
×
400
            ['object' => $dummy, 'property' => 'ownerOnlyProperty']
×
401
        )->willReturn(true);
×
402

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

406
        $context = [AbstractItemNormalizer::OBJECT_TO_POPULATE => $dummy];
×
407
        $actual = $normalizer->denormalize($data, SecuredDummy::class, null, $context);
×
408

409
        $this->assertInstanceOf(SecuredDummy::class, $actual);
×
410

411
        $propertyAccessorProphecy->setValue($actual, 'title', 'foo')->shouldHaveBeenCalled();
×
412
        $propertyAccessorProphecy->setValue($actual, 'ownerOnlyProperty', 'secret')->shouldHaveBeenCalled();
×
413
    }
414

415
    public function testDenormalizeUpdateWithDeniedSecuredProperty(): void
416
    {
417
        $dummy = new SecuredDummy();
×
418
        $dummy->setOwnerOnlyProperty('secret');
×
419

420
        $data = [
×
421
            'title' => 'foo',
×
422
            'ownerOnlyProperty' => 'should not be set',
×
423
        ];
×
424

425
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
426
        $propertyNameCollectionFactoryProphecy->create(SecuredDummy::class, [])->willReturn(new PropertyNameCollection(['title', 'ownerOnlyProperty']));
×
427

428
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
429
        $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
430
        $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true)->withWritable(true)->withSecurity('false'));
×
431

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

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

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

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

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

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

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

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

464
        $propertyAccessorProphecy->setValue($actual, 'title', 'foo')->shouldHaveBeenCalled();
×
465
        $propertyAccessorProphecy->setValue($actual, 'ownerOnlyProperty', 'should not be set')->shouldNotHaveBeenCalled();
×
466
    }
467

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

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

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

481
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
482
        $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
483
        $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true)->withWritable(true)->withSecurityPostDenormalize('false'));
×
484

485
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
486

487
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
488
        $propertyAccessorProphecy->getValue($dummy, 'ownerOnlyProperty')->willReturn('secret');
×
489

490
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
491
        $resourceClassResolverProphecy->getResourceClass($dummy, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
492
        $resourceClassResolverProphecy->getResourceClass(null, SecuredDummy::class)->willReturn(SecuredDummy::class);
×
493
        $resourceClassResolverProphecy->isResourceClass(SecuredDummy::class)->willReturn(true);
×
494

495
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
496
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
497

498
        $resourceAccessChecker = $this->prophesize(ResourceAccessCheckerInterface::class);
×
499
        $resourceAccessChecker->isGranted(
×
500
            SecuredDummy::class,
×
501
            'false',
×
502
            ['object' => $dummy, 'previous_object' => $dummy, 'property' => 'ownerOnlyProperty']
×
503
        )->willReturn(false);
×
504

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

508
        $context = [AbstractItemNormalizer::OBJECT_TO_POPULATE => $dummy];
×
509
        $actual = $normalizer->denormalize($data, SecuredDummy::class, null, $context);
×
510

511
        $this->assertInstanceOf(SecuredDummy::class, $actual);
×
512

513
        $propertyAccessorProphecy->setValue($actual, 'title', 'foo')->shouldHaveBeenCalled();
×
514
        $propertyAccessorProphecy->setValue($actual, 'ownerOnlyProperty', 'should not be set')->shouldHaveBeenCalled();
×
515
        $propertyAccessorProphecy->setValue($actual, 'ownerOnlyProperty', 'secret')->shouldHaveBeenCalled();
×
516
    }
517

518
    public function testNormalizeReadableLinks(): void
519
    {
520
        $relatedDummy = new RelatedDummy();
×
521

522
        $dummy = new Dummy();
×
523
        $dummy->setRelatedDummy($relatedDummy);
×
524
        $dummy->relatedDummies->add(new RelatedDummy());
×
525

526
        $relatedDummies = new ArrayCollection([$relatedDummy]);
×
527

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

531
        $relatedDummyType = new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
532
        $relatedDummiesType = new Type(Type::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new Type(Type::BUILTIN_TYPE_INT), $relatedDummyType);
×
533

534
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
535
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummyType])->withReadable(true)->withWritable(false)->withReadableLink(true));
×
536
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(true)->withWritable(false)->withReadableLink(true));
×
537

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

541
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
542
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummy')->willReturn($relatedDummy);
×
543
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummies')->willReturn($relatedDummies);
×
544

545
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
546
        $resourceClassResolverProphecy->getResourceClass($dummy, null)->willReturn(Dummy::class);
×
547
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
548
        $resourceClassResolverProphecy->getResourceClass($relatedDummy, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
549
        $resourceClassResolverProphecy->getResourceClass($relatedDummies, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
550
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
551
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
552

553
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
554
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
555
        $relatedDummyChildContext = Argument::allOf(
×
556
            Argument::type('array'),
×
557
            Argument::withEntry('resource_class', RelatedDummy::class),
×
558
            Argument::not(Argument::withKey('iri')),
×
559
            Argument::not(Argument::withKey('force_resource_class'))
×
560
        );
×
561
        $serializerProphecy->normalize($relatedDummy, null, $relatedDummyChildContext)->willReturn(['foo' => 'hello']);
×
562
        $serializerProphecy->normalize(['foo' => 'hello'], null, Argument::type('array'))->willReturn(['foo' => 'hello']);
×
563
        $serializerProphecy->normalize([['foo' => 'hello']], null, Argument::type('array'))->willReturn([['foo' => 'hello']]);
×
564

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

568
        $expected = [
×
569
            'relatedDummy' => ['foo' => 'hello'],
×
570
            'relatedDummies' => [['foo' => 'hello']],
×
571
        ];
×
572
        $this->assertSame($expected, $normalizer->normalize($dummy, null, [
×
573
            'resources' => [],
×
574
            'force_resource_class' => Dummy::class,
×
575
        ]));
×
576
    }
577

578
    public function testNormalizePolymorphicRelations(): void
579
    {
580
        $concreteDummy = new DummyTableInheritanceChild();
×
581

582
        $dummy = new DummyTableInheritanceRelated();
×
583
        $dummy->addChild($concreteDummy);
×
584

585
        $abstractDummies = new ArrayCollection([$concreteDummy]);
×
586

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

590
        $abstractDummyType = new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyTableInheritance::class);
×
591
        $abstractDummiesType = new Type(Type::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new Type(Type::BUILTIN_TYPE_INT), $abstractDummyType);
×
592

593
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
594
        $propertyMetadataFactoryProphecy->create(DummyTableInheritanceRelated::class, 'children', [])->willReturn((new ApiProperty())->withBuiltinTypes([$abstractDummiesType])->withReadable(true)->withWritable(false)->withReadableLink(true));
×
595

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

599
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
600
        $propertyAccessorProphecy->getValue($dummy, 'children')->willReturn($abstractDummies);
×
601

602
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
603
        $resourceClassResolverProphecy->getResourceClass($dummy, null)->willReturn(DummyTableInheritanceRelated::class);
×
604
        $resourceClassResolverProphecy->getResourceClass(null, DummyTableInheritanceRelated::class)->willReturn(DummyTableInheritanceRelated::class);
×
605
        $resourceClassResolverProphecy->getResourceClass($concreteDummy, DummyTableInheritance::class)->willReturn(DummyTableInheritanceChild::class);
×
606
        $resourceClassResolverProphecy->getResourceClass($abstractDummies, DummyTableInheritance::class)->willReturn(DummyTableInheritance::class);
×
607
        $resourceClassResolverProphecy->isResourceClass(DummyTableInheritanceRelated::class)->willReturn(true);
×
608
        $resourceClassResolverProphecy->isResourceClass(DummyTableInheritance::class)->willReturn(true);
×
609

610
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
611
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
612
        $concreteDummyChildContext = Argument::allOf(
×
613
            Argument::type('array'),
×
614
            Argument::not(Argument::withKey('iri'))
×
615
        );
×
616
        $serializerProphecy->normalize($concreteDummy, null, $concreteDummyChildContext)->willReturn(['foo' => 'concrete']);
×
617
        $serializerProphecy->normalize([['foo' => 'concrete']], null, Argument::type('array'))->willReturn([['foo' => 'concrete']]);
×
618

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

622
        $expected = [
×
623
            'children' => [['foo' => 'concrete']],
×
624
        ];
×
625
        $this->assertSame($expected, $normalizer->normalize($dummy, null, [
×
626
            'resources' => [],
×
627
        ]));
×
628
    }
629

630
    public function testDenormalize(): void
631
    {
632
        $data = [
×
633
            'name' => 'foo',
×
634
            'relatedDummy' => '/dummies/1',
×
635
            'relatedDummies' => ['/dummies/2'],
×
636
        ];
×
637

638
        $relatedDummy1 = new RelatedDummy();
×
639
        $relatedDummy2 = new RelatedDummy();
×
640

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

644
        $relatedDummyType = new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
645
        $relatedDummiesType = new Type(Type::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new Type(Type::BUILTIN_TYPE_INT), $relatedDummyType);
×
646

647
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
648
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
649

650
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummyType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
651
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
652

653
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
654
        $iriConverterProphecy->getResourceFromIri('/dummies/1', Argument::type('array'))->willReturn($relatedDummy1);
×
655
        $iriConverterProphecy->getResourceFromIri('/dummies/2', Argument::type('array'))->willReturn($relatedDummy2);
×
656

657
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
658

659
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
660
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
661
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
662
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
663
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
664

665
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
666
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
667

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

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

673
        $this->assertInstanceOf(Dummy::class, $actual);
×
674

675
        $propertyAccessorProphecy->setValue($actual, 'name', 'foo')->shouldHaveBeenCalled();
×
676
        $propertyAccessorProphecy->setValue($actual, 'relatedDummy', $relatedDummy1)->shouldHaveBeenCalled();
×
677
        $propertyAccessorProphecy->setValue($actual, 'relatedDummies', [$relatedDummy2])->shouldHaveBeenCalled();
×
678
    }
679

680
    public function testCanDenormalizeInputClassWithDifferentFieldsThanResourceClass(): void
681
    {
682
        $this->markTestSkipped('TODO: check why this test has been commented');
×
683

684
        // $data = [
685
        //     'dummyName' => 'Dummy Name',
686
        // ];
687
        //
688
        // $context = [
689
        //     'resource_class' => DummyForAdditionalFields::class,
690
        //     'input' => ['class' => DummyForAdditionalFieldsInput::class],
691
        //     'output' => ['class' => DummyForAdditionalFields::class],
692
        // ];
693
        // $augmentedContext = $context + ['api_denormalize' => true];
694
        //
695
        // $preHydratedDummy = new DummyForAdditionalFieldsInput('Name Dummy');
696
        // $cleanedContext = array_diff_key($augmentedContext, [
697
        //     'input' => null,
698
        //     'resource_class' => null,
699
        // ]);
700
        // $cleanedContextWithObjectToPopulate = array_merge($cleanedContext, [
701
        //     AbstractObjectNormalizer::OBJECT_TO_POPULATE => $preHydratedDummy,
702
        //     AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE => true,
703
        // ]);
704
        //
705
        // $dummyInputDto = new DummyForAdditionalFieldsInput('Dummy Name');
706
        // $dummy = new DummyForAdditionalFields('Dummy Name', 'name-dummy');
707
        //
708
        // $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
709
        //
710
        // $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
711
        //
712
        // $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
713
        //
714
        // $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
715
        // $resourceClassResolverProphecy->getResourceClass(null, DummyForAdditionalFields::class)->willReturn(DummyForAdditionalFields::class);
716
        //
717
        // $inputDataTransformerProphecy = $this->prophesize(DataTransformerInitializerInterface::class);
718
        // $inputDataTransformerProphecy->willImplement(DataTransformerInitializerInterface::class);
719
        // $inputDataTransformerProphecy->initialize(DummyForAdditionalFieldsInput::class, $cleanedContext)->willReturn($preHydratedDummy);
720
        // $inputDataTransformerProphecy->supportsTransformation($data, DummyForAdditionalFields::class, $augmentedContext)->willReturn(true);
721
        // $inputDataTransformerProphecy->transform($dummyInputDto, DummyForAdditionalFields::class, $augmentedContext)->willReturn($dummy);
722
        //
723
        // $serializerProphecy = $this->prophesize(SerializerInterface::class);
724
        // $serializerProphecy->willImplement(DenormalizerInterface::class);
725
        // $serializerProphecy->denormalize($data, DummyForAdditionalFieldsInput::class, 'json', $cleanedContextWithObjectToPopulate)->willReturn($dummyInputDto);
726
        //
727
        // $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), null, null, null, [], null, null) extends AbstractItemNormalizer {
728
        // };
729
        // $normalizer->setSerializer($serializerProphecy->reveal());
730
        //
731
        // $actual = $normalizer->denormalize($data, DummyForAdditionalFields::class, 'json', $context);
732
        //
733
        // $this->assertInstanceOf(DummyForAdditionalFields::class, $actual);
734
        // $this->assertSame('Dummy Name', $actual->getName());
735
    }
736

737
    public function testDenormalizeWritableLinks(): void
738
    {
739
        $data = [
×
740
            'name' => 'foo',
×
741
            'relatedDummy' => ['foo' => 'bar'],
×
742
            'relatedDummies' => [['bar' => 'baz']],
×
743
            'relatedDummiesWithUnionTypes' => [0 => ['bar' => 'qux'], 1. => ['bar' => 'quux']],
×
744
        ];
×
745

746
        $relatedDummy1 = new RelatedDummy();
×
747
        $relatedDummy2 = new RelatedDummy();
×
748
        $relatedDummy3 = new RelatedDummy();
×
749
        $relatedDummy4 = new RelatedDummy();
×
750

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

754
        $relatedDummyType = new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
755
        $relatedDummiesType = new Type(Type::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new Type(Type::BUILTIN_TYPE_INT), $relatedDummyType);
×
756
        $relatedDummiesWithUnionTypesIntType = new Type(Type::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new Type(Type::BUILTIN_TYPE_INT), $relatedDummyType);
×
757
        $relatedDummiesWithUnionTypesFloatType = new Type(Type::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new Type(Type::BUILTIN_TYPE_FLOAT), $relatedDummyType);
×
758

759
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
760
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
761
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummyType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
762
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
763
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummiesWithUnionTypes', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesWithUnionTypesIntType, $relatedDummiesWithUnionTypesFloatType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
764

765
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
766

767
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
768

769
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
770
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
771
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
772
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
773
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
774

775
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
776
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
777
        $serializerProphecy->denormalize(['foo' => 'bar'], RelatedDummy::class, null, Argument::type('array'))->willReturn($relatedDummy1);
×
778
        $serializerProphecy->denormalize(['bar' => 'baz'], RelatedDummy::class, null, Argument::type('array'))->willReturn($relatedDummy2);
×
779
        $serializerProphecy->denormalize(['bar' => 'qux'], RelatedDummy::class, null, Argument::type('array'))->willReturn($relatedDummy3);
×
780
        $serializerProphecy->denormalize(['bar' => 'quux'], RelatedDummy::class, null, Argument::type('array'))->willReturn($relatedDummy4);
×
781

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

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

787
        $this->assertInstanceOf(Dummy::class, $actual);
×
788

789
        $propertyAccessorProphecy->setValue($actual, 'name', 'foo')->shouldHaveBeenCalled();
×
790
        $propertyAccessorProphecy->setValue($actual, 'relatedDummy', $relatedDummy1)->shouldHaveBeenCalled();
×
791
        $propertyAccessorProphecy->setValue($actual, 'relatedDummies', [$relatedDummy2])->shouldHaveBeenCalled();
×
792
        $propertyAccessorProphecy->setValue($actual, 'relatedDummiesWithUnionTypes', [0 => $relatedDummy3, 1. => $relatedDummy4])->shouldHaveBeenCalled();
×
793
    }
794

795
    public function testBadRelationType(): void
796
    {
797
        $this->expectException(NotNormalizableValueException::class);
×
798
        $this->expectExceptionMessage('The type of the "relatedDummy" attribute must be "array" (nested document) or "string" (IRI), "integer" given.');
×
799

800
        $data = [
×
801
            'relatedDummy' => 22,
×
802
        ];
×
803

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

807
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
808
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
×
809
            (new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false)
×
810
        );
×
811

812
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
813

814
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
815
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
816
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
817
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
818
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
819

820
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
821

822
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
823
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
824

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

828
        $normalizer->denormalize($data, Dummy::class);
×
829
    }
830

831
    public function testBadRelationTypeWithExceptionToValidationErrors(): void
832
    {
833
        $data = [
×
834
            'relatedDummy' => 22,
×
835
        ];
×
836

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

840
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
841
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
×
842
            (new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false)
×
843
        );
×
844

845
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
846

847
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
848
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
849
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
850
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
851
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
852

853
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
854

855
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
856
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
857

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

861
        // 'not_normalizable_value_exceptions' is set by Serializer thanks to DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS
862
        $actual = $normalizer->denormalize($data, Dummy::class, null, ['not_normalizable_value_exceptions' => []]);
×
863
        $this->assertNull($actual->relatedDummy);
×
864
    }
865

866
    public function testDeserializationPathForNotDenormalizableRelations(): void
867
    {
868
        $data = [
×
869
            'relatedDummies' => ['wrong'],
×
870
        ];
×
871

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

875
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
876
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn(
×
877
            (new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_OBJECT, false, null, true, null, new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class))])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true)
×
878
        );
×
879

880
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
881
        $iriConverterProphecy->getResourceFromIri(Argument::cetera())->willThrow(new InvalidArgumentException('Invalid IRI'));
×
882

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

889
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
890

891
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
892
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
893

894
        $normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
×
895
            $propertyNameCollectionFactoryProphecy->reveal(),
×
896
            $propertyMetadataFactoryProphecy->reveal(),
×
897
            $iriConverterProphecy->reveal(),
×
898
            $resourceClassResolverProphecy->reveal(),
×
899
            $propertyAccessorProphecy->reveal(),
×
900
            null,
×
901
            null,
×
902
            [],
×
903
            null,
×
904
            null,
×
905
        ]);
×
906
        $normalizer->setSerializer($serializerProphecy->reveal());
×
907

908
        $errors = [];
×
909
        $actual = $normalizer->denormalize($data, Dummy::class, null, ['not_normalizable_value_exceptions' => &$errors]);
×
910
        $this->assertEmpty($actual->relatedDummies);
×
911
        $this->assertCount(1, $errors); // @phpstan-ignore-line method.impossibleType (false positive)
×
912
        $this->assertInstanceOf(NotNormalizableValueException::class, $errors[0]);
×
913
        $this->assertSame('relatedDummies[0]', $errors[0]->getPath());
×
NEW
914
        $this->assertSame('Invalid IRI "wrong".', $errors[0]->getMessage());
×
915
    }
916

917
    public function testDeserializationPathForNotDenormalizableResource(): void
918
    {
919
        $this->expectException(NotNormalizableValueException::class);
×
NEW
920
        $this->expectExceptionMessage('Invalid IRI "wrong IRI".');
×
921

922
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
923

924
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
925

926
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
927
        $iriConverterProphecy->getResourceFromIri(Argument::cetera())->willThrow(new InvalidArgumentException('Invalid IRI'));
×
928

929
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
930
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
931
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
932

933
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
934

935
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
936
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
937

938
        $normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
×
939
            $propertyNameCollectionFactoryProphecy->reveal(),
×
940
            $propertyMetadataFactoryProphecy->reveal(),
×
941
            $iriConverterProphecy->reveal(),
×
942
            $resourceClassResolverProphecy->reveal(),
×
943
            $propertyAccessorProphecy->reveal(),
×
944
            null,
×
945
            null,
×
946
            [],
×
947
            null,
×
948
            null,
×
949
        ]);
×
950
        $normalizer->setSerializer($serializerProphecy->reveal());
×
951

952
        $normalizer->denormalize('wrong IRI', Dummy::class, null, ['not_normalizable_value_exceptions' => []]);
×
953
    }
954

955
    public function testDeserializationPathForNotFoundResource(): void
956
    {
NEW
957
        $this->expectException(NotNormalizableValueException::class);
×
NEW
958
        $this->expectExceptionMessage('Some item not found exception.');
×
959

NEW
960
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
961

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

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

NEW
967
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
NEW
968
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
NEW
969
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
970

NEW
971
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
972

NEW
973
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
NEW
974
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
975

NEW
976
        $normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
×
NEW
977
            $propertyNameCollectionFactoryProphecy->reveal(),
×
NEW
978
            $propertyMetadataFactoryProphecy->reveal(),
×
NEW
979
            $iriConverterProphecy->reveal(),
×
NEW
980
            $resourceClassResolverProphecy->reveal(),
×
NEW
981
            $propertyAccessorProphecy->reveal(),
×
NEW
982
            null,
×
NEW
983
            null,
×
NEW
984
            [],
×
NEW
985
            null,
×
NEW
986
            null,
×
NEW
987
        ]);
×
NEW
988
        $normalizer->setSerializer($serializerProphecy->reveal());
×
989

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

993
    public function testInnerDocumentNotAllowed(): void
994
    {
995
        $this->expectException(UnexpectedValueException::class);
×
996
        $this->expectExceptionMessage('Nested documents for attribute "relatedDummy" are not allowed. Use IRIs instead.');
×
997

998
        $data = [
×
999
            'relatedDummy' => [
×
1000
                'foo' => 'bar',
×
1001
            ],
×
1002
        ];
×
1003

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

1007
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1008
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
×
1009
            (new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false)
×
1010
        );
×
1011

1012
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1013

1014
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1015
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1016
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
1017
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1018
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1019

1020
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1021

1022
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1023
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1024

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

1028
        $normalizer->denormalize($data, Dummy::class);
×
1029
    }
1030

1031
    public function testBadType(): void
1032
    {
1033
        $this->expectException(UnexpectedValueException::class);
×
1034
        $this->expectExceptionMessage('The type of the "foo" attribute must be "float", "integer" given.');
×
1035

1036
        $data = [
×
1037
            'foo' => 42,
×
1038
        ];
×
1039

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

1043
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1044
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1045

1046
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1047

1048
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1049
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1050
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1051

1052
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1053

1054
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1055
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1056

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

1060
        $normalizer->denormalize($data, Dummy::class);
×
1061
    }
1062

1063
    public function testTypeChecksCanBeDisabled(): void
1064
    {
1065
        $data = [
×
1066
            'foo' => 42,
×
1067
        ];
×
1068

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

1072
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1073
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1074

1075
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1076

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

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

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

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

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

1091
        $this->assertInstanceOf(Dummy::class, $actual);
×
1092

1093
        $propertyAccessorProphecy->setValue($actual, 'foo', 42)->shouldHaveBeenCalled();
×
1094
    }
1095

1096
    public function testJsonAllowIntAsFloat(): void
1097
    {
1098
        $data = [
×
1099
            'foo' => 42,
×
1100
        ];
×
1101

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

1105
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1106
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'foo', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1107

1108
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1109

1110
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1111
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1112
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1113

1114
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1115

1116
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1117
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1118

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

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

1124
        $this->assertInstanceOf(Dummy::class, $actual);
×
1125

1126
        $propertyAccessorProphecy->setValue($actual, 'foo', 42)->shouldHaveBeenCalled();
×
1127
    }
1128

1129
    public function testDenormalizeBadKeyType(): void
1130
    {
1131
        $this->expectException(NotNormalizableValueException::class);
×
1132
        $this->expectExceptionMessage('The type of the key "a" must be "int", "string" given.');
×
1133

1134
        $data = [
×
1135
            'name' => 'foo',
×
1136
            'relatedDummy' => [
×
1137
                'foo' => 'bar',
×
1138
            ],
×
1139
            'relatedDummies' => [
×
1140
                'a' => [
×
1141
                    'bar' => 'baz',
×
1142
                ],
×
1143
            ],
×
1144
            'relatedDummiesWithUnionTypes' => [
×
1145
                'a' => [
×
1146
                    'bar' => 'baz',
×
1147
                ],
×
1148
            ],
×
1149
        ];
×
1150

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

1154
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1155
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
1156
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)])->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1157

1158
        $type = new Type(
×
1159
            Type::BUILTIN_TYPE_OBJECT,
×
1160
            false,
×
1161
            ArrayCollection::class,
×
1162
            true,
×
1163
            new Type(Type::BUILTIN_TYPE_INT),
×
1164
            new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)
×
1165
        );
×
1166
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$type])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
1167

1168
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1169

1170
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1171
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1172
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
1173
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1174
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1175

1176
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1177

1178
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1179
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1180

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

1184
        $normalizer->denormalize($data, Dummy::class);
×
1185
    }
1186

1187
    public function testNullable(): void
1188
    {
1189
        $data = [
×
1190
            'name' => null,
×
1191
        ];
×
1192

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

1196
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1197
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING, true)])->withDescription('')->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(false));
×
1198

1199
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1200

1201
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1202
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1203
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1204

1205
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1206

1207
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1208
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1209

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

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

1215
        $this->assertInstanceOf(Dummy::class, $actual);
×
1216

1217
        $propertyAccessorProphecy->setValue($actual, 'name', null)->shouldHaveBeenCalled();
×
1218
    }
1219

1220
    public function testDenormalizeBasicTypePropertiesFromXml(): void
1221
    {
1222
        $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
×
1223
        $propertyNameCollectionFactoryProphecy->create(ObjectWithBasicProperties::class, [])->willReturn(new PropertyNameCollection([
×
1224
            'boolTrue1',
×
1225
            'boolFalse1',
×
1226
            'boolTrue2',
×
1227
            'boolFalse2',
×
1228
            'int1',
×
1229
            'int2',
×
1230
            'float1',
×
1231
            'float2',
×
1232
            'float3',
×
1233
            'floatNaN',
×
1234
            'floatInf',
×
1235
            'floatNegInf',
×
1236
        ]));
×
1237

1238
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1239
        $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolTrue1', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_BOOL)])->withDescription('')->withReadable(false)->withWritable(true));
×
1240
        $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolFalse1', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_BOOL)])->withDescription('')->withReadable(false)->withWritable(true));
×
1241
        $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolTrue2', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_BOOL)])->withDescription('')->withReadable(false)->withWritable(true));
×
1242
        $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'boolFalse2', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_BOOL)])->withDescription('')->withReadable(false)->withWritable(true));
×
1243
        $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'int1', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_INT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1244
        $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'int2', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_INT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1245
        $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float1', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1246
        $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float2', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1247
        $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'float3', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1248
        $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatNaN', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1249
        $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatInf', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1250
        $propertyMetadataFactoryProphecy->create(ObjectWithBasicProperties::class, 'floatNegInf', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_FLOAT)])->withDescription('')->withReadable(false)->withWritable(true));
×
1251

1252
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1253

1254
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1255
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'boolTrue1', true)->shouldBeCalled();
×
1256
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'boolFalse1', false)->shouldBeCalled();
×
1257
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'boolTrue2', true)->shouldBeCalled();
×
1258
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'boolFalse2', false)->shouldBeCalled();
×
1259
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'int1', 4711)->shouldBeCalled();
×
1260
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'int2', -4711)->shouldBeCalled();
×
1261
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'float1', Argument::approximate(123.456, 0))->shouldBeCalled();
×
1262
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'float2', Argument::approximate(-1.2344e56, 1))->shouldBeCalled();
×
1263
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'float3', Argument::approximate(45E-6, 1))->shouldBeCalled();
×
1264
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'floatNaN', Argument::that(static fn (float $arg) => is_nan($arg)))->shouldBeCalled();
×
1265
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'floatInf', \INF)->shouldBeCalled();
×
1266
        $propertyAccessorProphecy->setValue(Argument::type(ObjectWithBasicProperties::class), 'floatNegInf', -\INF)->shouldBeCalled();
×
1267

1268
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1269
        $resourceClassResolverProphecy->getResourceClass(null, ObjectWithBasicProperties::class)->willReturn(ObjectWithBasicProperties::class);
×
1270
        $resourceClassResolverProphecy->isResourceClass(ObjectWithBasicProperties::class)->willReturn(true);
×
1271

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

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

1278
        $objectWithBasicProperties = $normalizer->denormalize(
×
1279
            [
×
1280
                'boolTrue1' => 'true',
×
1281
                'boolFalse1' => 'false',
×
1282
                'boolTrue2' => '1',
×
1283
                'boolFalse2' => '0',
×
1284
                'int1' => '4711',
×
1285
                'int2' => '-4711',
×
1286
                'float1' => '123.456',
×
1287
                'float2' => '-1.2344e56',
×
1288
                'float3' => '45E-6',
×
1289
                'floatNaN' => 'NaN',
×
1290
                'floatInf' => 'INF',
×
1291
                'floatNegInf' => '-INF',
×
1292
            ],
×
1293
            ObjectWithBasicProperties::class,
×
1294
            'xml'
×
1295
        );
×
1296

1297
        $this->assertInstanceOf(ObjectWithBasicProperties::class, $objectWithBasicProperties);
×
1298
    }
1299

1300
    public function testDenormalizeCollectionDecodedFromXmlWithOneChild(): void
1301
    {
1302
        $data = [
×
1303
            'relatedDummies' => [
×
1304
                'name' => 'foo',
×
1305
            ],
×
1306
        ];
×
1307

1308
        $relatedDummy = new RelatedDummy();
×
1309
        $relatedDummy->setName('foo');
×
1310

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

1314
        $relatedDummyType = new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
1315
        $relatedDummiesType = new Type(Type::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new Type(Type::BUILTIN_TYPE_INT), $relatedDummyType);
×
1316

1317
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1318
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true));
×
1319

1320
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1321

1322
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1323
        $propertyAccessorProphecy->setValue(Argument::type(Dummy::class), 'relatedDummies', Argument::type('array'))->shouldBeCalled();
×
1324

1325
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1326
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1327
        $resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
1328
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1329
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1330

1331
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1332
        $serializerProphecy->willImplement(DenormalizerInterface::class);
×
1333
        $serializerProphecy->denormalize(['name' => 'foo'], RelatedDummy::class, 'xml', Argument::type('array'))->willReturn($relatedDummy);
×
1334

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

1338
        $normalizer->denormalize($data, Dummy::class, 'xml');
×
1339
    }
1340

1341
    public function testDenormalizePopulatingNonCloneableObject(): void
1342
    {
1343
        $dummy = new NonCloneableDummy();
×
1344
        $dummy->setName('foo');
×
1345

1346
        $data = [
×
1347
            'name' => 'bar',
×
1348
        ];
×
1349

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

1353
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1354
        $propertyMetadataFactoryProphecy->create(NonCloneableDummy::class, 'name', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true));
×
1355

1356
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1357
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1358
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1359
        $resourceClassResolverProphecy->getResourceClass(null, NonCloneableDummy::class)->willReturn(NonCloneableDummy::class);
×
1360
        $resourceClassResolverProphecy->getResourceClass($dummy, NonCloneableDummy::class)->willReturn(NonCloneableDummy::class);
×
1361
        $resourceClassResolverProphecy->isResourceClass(NonCloneableDummy::class)->willReturn(true);
×
1362

1363
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1364
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
1365

1366
        $normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
×
1367
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1368
        $normalizer->setSerializer($serializerProphecy->reveal());
×
1369

1370
        $context = [AbstractItemNormalizer::OBJECT_TO_POPULATE => $dummy];
×
1371
        $actual = $normalizer->denormalize($data, NonCloneableDummy::class, null, $context);
×
1372

1373
        $this->assertInstanceOf(NonCloneableDummy::class, $actual);
×
1374
        $this->assertSame($dummy, $actual);
×
1375
        $propertyAccessorProphecy->setValue($actual, 'name', 'bar')->shouldHaveBeenCalled();
×
1376
    }
1377

1378
    public function testDenormalizeObjectWithNullDisabledTypeEnforcement(): void
1379
    {
1380
        $data = [
×
1381
            'dummy' => null,
×
1382
        ];
×
1383

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

1387
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1388
        $propertyMetadataFactoryProphecy->create(DtoWithNullValue::class, 'dummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_OBJECT, nullable: true)])->withDescription('')->withReadable(true)->withWritable(true));
×
1389

1390
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1391
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1392
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1393
        $resourceClassResolverProphecy->getResourceClass(null, DtoWithNullValue::class)->willReturn(DtoWithNullValue::class);
×
1394
        $resourceClassResolverProphecy->isResourceClass(DtoWithNullValue::class)->willReturn(true);
×
1395

1396
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1397
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
1398

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

1402
        $context = [AbstractItemNormalizer::DISABLE_TYPE_ENFORCEMENT => true];
×
1403
        $actual = $normalizer->denormalize($data, DtoWithNullValue::class, null, $context);
×
1404

1405
        $this->assertInstanceOf(DtoWithNullValue::class, $actual);
×
1406
        $this->assertEquals(new DtoWithNullValue(), $actual);
×
1407
    }
1408

1409
    public function testCacheKey(): void
1410
    {
1411
        $relatedDummy = new RelatedDummy();
×
1412

1413
        $dummy = new Dummy();
×
1414
        $dummy->setName('foo');
×
1415
        $dummy->setAlias('ignored');
×
1416
        $dummy->setRelatedDummy($relatedDummy);
×
1417
        $dummy->relatedDummies->add(new RelatedDummy());
×
1418

1419
        $relatedDummies = new ArrayCollection([$relatedDummy]);
×
1420

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

1424
        $relatedDummyType = new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
×
1425
        $relatedDummiesType = new Type(Type::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new Type(Type::BUILTIN_TYPE_INT), $relatedDummyType);
×
1426

1427
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
1428
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
×
1429
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'alias', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
×
1430
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummyType])->withDescription('')->withReadable(true)->withWritable(false)->withReadableLink(false));
×
1431
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(true)->withWritable(false)->withReadableLink(false));
×
1432

1433
        $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
×
1434
        $iriConverterProphecy->getIriFromResource($dummy, Argument::cetera())->willReturn('/dummies/1');
×
1435
        $iriConverterProphecy->getIriFromResource($relatedDummy, Argument::cetera())->willReturn('/dummies/2');
×
1436

1437
        $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
×
1438
        $propertyAccessorProphecy->getValue($dummy, 'name')->willReturn('foo');
×
1439
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummy')->willReturn($relatedDummy);
×
1440
        $propertyAccessorProphecy->getValue($dummy, 'relatedDummies')->willReturn($relatedDummies);
×
1441

1442
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
1443
        $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
×
1444
        $resourceClassResolverProphecy->getResourceClass($dummy, null)->willReturn(Dummy::class);
×
1445
        $resourceClassResolverProphecy->getResourceClass($relatedDummy, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
1446
        $resourceClassResolverProphecy->getResourceClass($relatedDummies, RelatedDummy::class)->willReturn(RelatedDummy::class);
×
1447
        $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
×
1448
        $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
×
1449

1450
        $serializerProphecy = $this->prophesize(SerializerInterface::class);
×
1451
        $serializerProphecy->willImplement(NormalizerInterface::class);
×
1452
        $serializerProphecy->normalize('foo', null, Argument::type('array'))->willReturn('foo');
×
1453
        $serializerProphecy->normalize(['/dummies/2'], null, Argument::type('array'))->willReturn(['/dummies/2']);
×
1454

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

1458
        $expected = [
×
1459
            'name' => 'foo',
×
1460
            'relatedDummy' => '/dummies/2',
×
1461
            'relatedDummies' => ['/dummies/2'],
×
1462
        ];
×
1463
        $this->assertSame($expected, $normalizer->normalize($dummy, null, [
×
1464
            'resources' => [],
×
1465
            'groups' => ['group'],
×
1466
            'ignored_attributes' => ['alias'],
×
1467
            'operation_name' => 'operation_name',
×
1468
            'root_operation_name' => 'root_operation_name',
×
1469
        ]));
×
1470

1471
        $operationCacheKey = (new \ReflectionClass($normalizer))->getProperty('localFactoryOptionsCache')->getValue($normalizer);
×
1472
        $this->assertEquals(array_keys($operationCacheKey), [\sprintf('%s%s%s%s', Dummy::class, 'operation_name', 'root_operation_name', 'n')]);
×
1473
        $this->assertEquals(current($operationCacheKey), ['serializer_groups' => ['group']]);
×
1474
    }
1475
}
1476

1477
class ObjectWithBasicProperties
1478
{
1479
    /** @var bool */
1480
    public $boolTrue1;
1481

1482
    /** @var bool */
1483
    public $boolFalse1;
1484

1485
    /** @var bool */
1486
    public $boolTrue2;
1487

1488
    /** @var bool */
1489
    public $boolFalse2;
1490

1491
    /** @var int */
1492
    public $int1;
1493

1494
    /** @var int */
1495
    public $int2;
1496

1497
    /** @var float */
1498
    public $float1;
1499

1500
    /** @var float */
1501
    public $float2;
1502

1503
    /** @var float */
1504
    public $float3;
1505

1506
    /** @var float */
1507
    public $floatNaN;
1508

1509
    /** @var float */
1510
    public $floatInf;
1511

1512
    /** @var float */
1513
    public $floatNegInf;
1514
}
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