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

api-platform / core / 15023181448

14 May 2025 02:19PM UTC coverage: 0.0% (-8.4%) from 8.418%
15023181448

Pull #7139

github

web-flow
Merge 9f45709da into 1862d03b7
Pull Request #7139: refactor(symfony): remove obsolete option `validator.query-parameter-validation`

0 of 4 new or added lines in 1 file covered. (0.0%)

11266 existing lines in 366 files now uncovered.

0 of 50828 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/State/Tests/Provider/DeserializeProviderTest.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\State\Tests\Provider;
15

16
use ApiPlatform\Metadata\Get;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\Patch;
19
use ApiPlatform\Metadata\Post;
20
use ApiPlatform\Metadata\Put;
21
use ApiPlatform\State\Provider\DeserializeProvider;
22
use ApiPlatform\State\ProviderInterface;
23
use ApiPlatform\State\SerializerContextBuilderInterface;
24
use PHPUnit\Framework\Attributes\DataProvider;
25
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
26
use PHPUnit\Framework\TestCase;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
29
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
30
use Symfony\Component\Serializer\SerializerInterface;
31

32
class DeserializeProviderTest extends TestCase
33
{
34
    #[IgnoreDeprecations]
35
    public function testDeserialize(): void
36
    {
37
        $this->expectUserDeprecationMessage('Since api-platform/core 5.0: To assign an object to populate you should set "api_assign_object_to_populate" in your denormalizationContext, not defining it is deprecated.');
×
38
        $objectToPopulate = new \stdClass();
×
39
        $serializerContext = [];
×
40
        $operation = new Post(deserialize: true, class: 'Test');
×
UNCOV
41
        $decorated = $this->createStub(ProviderInterface::class);
×
42
        $decorated->method('provide')->willReturn($objectToPopulate);
×
43

44
        $serializerContextBuilder = $this->createMock(SerializerContextBuilderInterface::class);
×
45
        $serializerContextBuilder->expects($this->once())->method('createFromRequest')->willReturn($serializerContext);
×
46
        $serializer = $this->createMock(SerializerInterface::class);
×
UNCOV
47
        $serializer->expects($this->once())->method('deserialize')->with('test', 'Test', 'format', ['uri_variables' => ['id' => 1], AbstractNormalizer::OBJECT_TO_POPULATE => $objectToPopulate] + $serializerContext)->willReturn(new \stdClass());
×
48

UNCOV
49
        $provider = new DeserializeProvider($decorated, $serializer, $serializerContextBuilder);
×
UNCOV
50
        $request = new Request(content: 'test');
×
51
        $request->headers->set('CONTENT_TYPE', 'ok');
×
52
        $request->attributes->set('input_format', 'format');
×
53
        $provider->provide($operation, ['id' => 1], ['request' => $request]);
×
54
    }
55

56
    public function testDeserializeNoContentType(): void
57
    {
UNCOV
58
        $this->expectException(UnsupportedMediaTypeHttpException::class);
×
59
        $operation = new Get(deserialize: true, class: 'Test');
×
60
        $decorated = $this->createStub(ProviderInterface::class);
×
61
        $decorated->method('provide')->willReturn(null);
×
62

UNCOV
63
        $serializerContextBuilder = $this->createMock(SerializerContextBuilderInterface::class);
×
UNCOV
64
        $serializer = $this->createMock(SerializerInterface::class);
×
65

UNCOV
66
        $provider = new DeserializeProvider($decorated, $serializer, $serializerContextBuilder);
×
67
        $request = new Request(content: 'test');
×
68
        $request->attributes->set('input_format', 'format');
×
69
        $provider->provide($operation, ['id' => 1], ['request' => $request]);
×
70
    }
71

72
    public function testDeserializeNoInput(): void
73
    {
UNCOV
74
        $this->expectException(UnsupportedMediaTypeHttpException::class);
×
75
        $operation = new Get(deserialize: true, class: 'Test');
×
76
        $decorated = $this->createStub(ProviderInterface::class);
×
77
        $decorated->method('provide')->willReturn(null);
×
78

UNCOV
79
        $serializerContextBuilder = $this->createMock(SerializerContextBuilderInterface::class);
×
UNCOV
80
        $serializer = $this->createMock(SerializerInterface::class);
×
81

UNCOV
82
        $provider = new DeserializeProvider($decorated, $serializer, $serializerContextBuilder);
×
83
        $request = new Request(content: 'test');
×
84
        $request->headers->set('CONTENT_TYPE', 'ok');
×
85
        $provider->provide($operation, ['id' => 1], ['request' => $request]);
×
86
    }
87

88
    public function testDeserializeWithContextClass(): void
89
    {
90
        $serializerContext = ['deserializer_type' => 'Test'];
×
91
        $operation = new Get(deserialize: true);
×
UNCOV
92
        $decorated = $this->createStub(ProviderInterface::class);
×
93
        $decorated->method('provide')->willReturn(null);
×
94

95
        $serializerContextBuilder = $this->createMock(SerializerContextBuilderInterface::class);
×
96
        $serializerContextBuilder->expects($this->once())->method('createFromRequest')->willReturn($serializerContext);
×
97
        $serializer = $this->createMock(SerializerInterface::class);
×
UNCOV
98
        $serializer->expects($this->once())->method('deserialize')->with('test', 'Test', 'format', ['uri_variables' => ['id' => 1]] + $serializerContext)->willReturn(new \stdClass());
×
99

UNCOV
100
        $provider = new DeserializeProvider($decorated, $serializer, $serializerContextBuilder);
×
UNCOV
101
        $request = new Request(content: 'test');
×
102
        $request->headers->set('CONTENT_TYPE', 'ok');
×
103
        $request->attributes->set('input_format', 'format');
×
104
        $provider->provide($operation, ['id' => 1], ['request' => $request]);
×
105
    }
106

107
    public function testRequestWithEmptyContentType(): void
108
    {
109
        $expectedResult = new \stdClass();
×
UNCOV
110
        $decorated = $this->createMock(ProviderInterface::class);
×
UNCOV
111
        $decorated->method('provide')->willReturn($expectedResult);
×
112

UNCOV
113
        $serializer = $this->createStub(SerializerInterface::class);
×
UNCOV
114
        $serializerContextBuilder = $this->createStub(SerializerContextBuilderInterface::class);
×
115

116
        $provider = new DeserializeProvider($decorated, $serializer, $serializerContextBuilder);
×
117

118
        // in Symfony (at least up to 7.0.2, 6.4.2, 6.3.11, 5.4.34), a request
119
        // without a content-type and content-length header will result in the
120
        // variables set to an empty string, not null
121

122
        $request = new Request(
×
123
            server: [
×
UNCOV
124
                'REQUEST_METHOD' => 'POST',
×
125
                'REQUEST_URI' => '/',
×
126
                'CONTENT_TYPE' => '',
×
UNCOV
127
                'CONTENT_LENGTH' => '',
×
128
            ],
×
129
            content: ''
×
UNCOV
130
        );
×
131

UNCOV
132
        $operation = new Post(deserialize: true);
×
UNCOV
133
        $context = ['request' => $request];
×
134

UNCOV
135
        $this->expectException(UnsupportedMediaTypeHttpException::class);
×
UNCOV
136
        $provider->provide($operation, [], $context);
×
137
    }
138

139
    #[DataProvider('provideMethodsTriggeringDeprecation')]
140
    #[IgnoreDeprecations]
141
    public function testDeserializeTriggersDeprecationWhenContextNotSet(HttpOperation $operation): void
142
    {
UNCOV
143
        $this->expectUserDeprecationMessage('Since api-platform/core 5.0: To assign an object to populate you should set "api_assign_object_to_populate" in your denormalizationContext, not defining it is deprecated.');
×
144

UNCOV
145
        $objectToPopulate = new \stdClass();
×
UNCOV
146
        $serializerContext = [];
×
UNCOV
147
        $decorated = $this->createStub(ProviderInterface::class);
×
UNCOV
148
        $decorated->method('provide')->willReturn($objectToPopulate);
×
149

UNCOV
150
        $serializerContextBuilder = $this->createMock(SerializerContextBuilderInterface::class);
×
UNCOV
151
        $serializerContextBuilder->method('createFromRequest')->willReturn($serializerContext);
×
152

UNCOV
153
        $serializer = $this->createMock(SerializerInterface::class);
×
UNCOV
154
        $serializer->expects($this->once())->method('deserialize')->with(
×
UNCOV
155
            'test',
×
UNCOV
156
            'Test',
×
UNCOV
157
            'format',
×
UNCOV
158
            ['uri_variables' => ['id' => 1], 'object_to_populate' => $objectToPopulate] + $serializerContext
×
UNCOV
159
        )->willReturn(new \stdClass());
×
160

UNCOV
161
        $provider = new DeserializeProvider($decorated, $serializer, $serializerContextBuilder);
×
UNCOV
162
        $request = new Request(content: 'test');
×
UNCOV
163
        $request->headers->set('CONTENT_TYPE', 'ok');
×
UNCOV
164
        $request->attributes->set('input_format', 'format');
×
UNCOV
165
        $provider->provide($operation, ['id' => 1], ['request' => $request]);
×
166
    }
167

168
    public static function provideMethodsTriggeringDeprecation(): iterable
169
    {
UNCOV
170
        yield 'POST method' => [new Post(deserialize: true, class: 'Test')];
×
UNCOV
171
        yield 'PATCH method' => [new Patch(deserialize: true, class: 'Test')];
×
UNCOV
172
        yield 'PUT method (non-standard)' => [new Put(deserialize: true, class: 'Test', extraProperties: ['standard_put' => false])];
×
173
    }
174

175
    public function testDeserializeSetsObjectToPopulateWhenContextIsTrue(): void
176
    {
UNCOV
177
        $objectToPopulate = new \stdClass();
×
UNCOV
178
        $serializerContext = [SerializerContextBuilderInterface::ASSIGN_OBJECT_TO_POPULATE => true];
×
UNCOV
179
        $operation = new Post(deserialize: true, class: 'Test');
×
UNCOV
180
        $decorated = $this->createStub(ProviderInterface::class);
×
UNCOV
181
        $decorated->method('provide')->willReturn($objectToPopulate);
×
182

UNCOV
183
        $serializerContextBuilder = $this->createMock(SerializerContextBuilderInterface::class);
×
UNCOV
184
        $serializerContextBuilder->method('createFromRequest')->willReturn($serializerContext);
×
185

UNCOV
186
        $serializer = $this->createMock(SerializerInterface::class);
×
UNCOV
187
        $serializer->expects($this->once())->method('deserialize')->with(
×
UNCOV
188
            'test',
×
UNCOV
189
            'Test',
×
UNCOV
190
            'format',
×
UNCOV
191
            $this->callback(function (array $context) use ($objectToPopulate) {
×
UNCOV
192
                $this->assertArrayHasKey(AbstractNormalizer::OBJECT_TO_POPULATE, $context);
×
UNCOV
193
                $this->assertSame($objectToPopulate, $context[AbstractNormalizer::OBJECT_TO_POPULATE]);
×
194

UNCOV
195
                return true;
×
UNCOV
196
            })
×
UNCOV
197
        )->willReturn(new \stdClass());
×
198

UNCOV
199
        $provider = new DeserializeProvider($decorated, $serializer, $serializerContextBuilder);
×
UNCOV
200
        $request = new Request(content: 'test');
×
UNCOV
201
        $request->headers->set('CONTENT_TYPE', 'ok');
×
UNCOV
202
        $request->attributes->set('input_format', 'format');
×
UNCOV
203
        $provider->provide($operation, ['id' => 1], ['request' => $request]);
×
204
    }
205

206
    public function testDeserializeDoesNotSetObjectToPopulateWhenContextIsFalse(): void
207
    {
UNCOV
208
        $objectToPopulate = new \stdClass();
×
UNCOV
209
        $serializerContext = [SerializerContextBuilderInterface::ASSIGN_OBJECT_TO_POPULATE => false];
×
UNCOV
210
        $operation = new Post(deserialize: true, class: 'Test');
×
UNCOV
211
        $decorated = $this->createStub(ProviderInterface::class);
×
UNCOV
212
        $decorated->method('provide')->willReturn($objectToPopulate);
×
213

UNCOV
214
        $serializerContextBuilder = $this->createMock(SerializerContextBuilderInterface::class);
×
UNCOV
215
        $serializerContextBuilder->method('createFromRequest')->willReturn($serializerContext);
×
216

UNCOV
217
        $serializer = $this->createMock(SerializerInterface::class);
×
UNCOV
218
        $serializer->expects($this->once())->method('deserialize')->with(
×
UNCOV
219
            'test',
×
UNCOV
220
            'Test',
×
UNCOV
221
            'format',
×
UNCOV
222
            $this->callback(function (array $context) {
×
UNCOV
223
                $this->assertArrayNotHasKey(AbstractNormalizer::OBJECT_TO_POPULATE, $context);
×
224

UNCOV
225
                return true;
×
UNCOV
226
            })
×
UNCOV
227
        )->willReturn(new \stdClass());
×
228

UNCOV
229
        $provider = new DeserializeProvider($decorated, $serializer, $serializerContextBuilder);
×
UNCOV
230
        $request = new Request(content: 'test');
×
UNCOV
231
        $request->headers->set('CONTENT_TYPE', 'ok');
×
UNCOV
232
        $request->attributes->set('input_format', 'format');
×
UNCOV
233
        $provider->provide($operation, ['id' => 1], ['request' => $request]);
×
234
    }
235
}
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