File: QueryParameterValueResolverTest.php

package info (click to toggle)
symfony 6.4.25%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 138,776 kB
  • sloc: php: 1,443,643; xml: 6,601; sh: 605; javascript: 597; makefile: 188; pascal: 71
file content (294 lines) | stat: -rw-r--r-- 14,659 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\QueryParameterValueResolver;
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Tests\Fixtures\Suit;

class QueryParameterValueResolverTest extends TestCase
{
    private ValueResolverInterface $resolver;

    protected function setUp(): void
    {
        $this->resolver = new QueryParameterValueResolver();
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('provideTestResolve')]
    public function testResolve(Request $request, ArgumentMetadata $metadata, array $expected, ?string $exceptionClass = null, ?string $exceptionMessage = null)
    {
        if ($exceptionMessage) {
            self::expectException($exceptionClass);
            self::expectExceptionMessage($exceptionMessage);
        }

        self::assertSame($expected, $this->resolver->resolve($request, $metadata));
    }

    /**
     * @return iterable<string, array{
     *   Request,
     *   ArgumentMetadata,
     *   array<mixed>,
     *   null|class-string<\Exception>,
     *   null|string
     * }>
     */
    public static function provideTestResolve(): iterable
    {
        yield 'parameter found and array' => [
            Request::create('/', 'GET', ['ids' => ['1', '2']]),
            new ArgumentMetadata('ids', 'array', false, false, false, attributes: [new MapQueryParameter()]),
            [['1', '2']],
            null,
        ];
        yield 'parameter found and array variadic' => [
            Request::create('/', 'GET', ['ids' => [['1', '2'], ['2']]]),
            new ArgumentMetadata('ids', 'array', true, false, false, attributes: [new MapQueryParameter()]),
            [['1', '2'], ['2']],
            null,
        ];
        yield 'parameter found and array variadic with parameter not array failure' => [
            Request::create('/', 'GET', ['ids' => [['1', '2'], 1]]),
            new ArgumentMetadata('ids', 'array', true, false, false, attributes: [new MapQueryParameter()]),
            [],
            NotFoundHttpException::class,
            'Invalid query parameter "ids".',
        ];
        yield 'parameter found and string' => [
            Request::create('/', 'GET', ['firstName' => 'John']),
            new ArgumentMetadata('firstName', 'string', false, false, false, attributes: [new MapQueryParameter()]),
            ['John'],
            null,
        ];
        yield 'parameter found and string variadic' => [
            Request::create('/', 'GET', ['ids' => ['1', '2']]),
            new ArgumentMetadata('ids', 'string', true, false, false, attributes: [new MapQueryParameter()]),
            ['1', '2'],
            null,
        ];
        yield 'parameter found and string with regexp filter that matches' => [
            Request::create('/', 'GET', ['firstName' => 'John']),
            new ArgumentMetadata('firstName', 'string', false, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_REGEXP, flags: \FILTER_NULL_ON_FAILURE, options: ['regexp' => '/John/'])]),
            ['John'],
            null,
        ];
        yield 'parameter found and string with regexp filter that falls back to null on failure' => [
            Request::create('/', 'GET', ['firstName' => 'Fabien']),
            new ArgumentMetadata('firstName', 'string', false, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_REGEXP, flags: \FILTER_NULL_ON_FAILURE, options: ['regexp' => '/John/'])]),
            [null],
            null,
        ];
        yield 'parameter found and string with regexp filter that does not match' => [
            Request::create('/', 'GET', ['firstName' => 'Fabien']),
            new ArgumentMetadata('firstName', 'string', false, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_REGEXP, options: ['regexp' => '/John/'])]),
            [],
            NotFoundHttpException::class,
            'Invalid query parameter "firstName".',
        ];
        yield 'parameter found and string variadic with regexp filter that matches' => [
            Request::create('/', 'GET', ['firstName' => ['John', 'John']]),
            new ArgumentMetadata('firstName', 'string', true, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_REGEXP, flags: \FILTER_NULL_ON_FAILURE, options: ['regexp' => '/John/'])]),
            ['John', 'John'],
            null,
        ];
        yield 'parameter found and string variadic with regexp filter that falls back to null on failure' => [
            Request::create('/', 'GET', ['firstName' => ['John', 'Fabien']]),
            new ArgumentMetadata('firstName', 'string', true, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_REGEXP, flags: \FILTER_NULL_ON_FAILURE, options: ['regexp' => '/John/'])]),
            ['John'],
            null,
        ];
        yield 'parameter found and string variadic with regexp filter that does not match' => [
            Request::create('/', 'GET', ['firstName' => ['Fabien']]),
            new ArgumentMetadata('firstName', 'string', true, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_REGEXP, options: ['regexp' => '/John/'])]),
            [],
            NotFoundHttpException::class,
            'Invalid query parameter "firstName".',
        ];
        yield 'parameter found and integer' => [
            Request::create('/', 'GET', ['age' => 123]),
            new ArgumentMetadata('age', 'int', false, false, false, attributes: [new MapQueryParameter()]),
            [123],
            null,
        ];
        yield 'parameter found and integer variadic' => [
            Request::create('/', 'GET', ['age' => [123, 222]]),
            new ArgumentMetadata('age', 'int', true, false, false, attributes: [new MapQueryParameter()]),
            [123, 222],
            null,
        ];
        yield 'parameter found and float' => [
            Request::create('/', 'GET', ['price' => 10.99]),
            new ArgumentMetadata('price', 'float', false, false, false, attributes: [new MapQueryParameter()]),
            [10.99],
            null,
        ];
        yield 'parameter found and float variadic' => [
            Request::create('/', 'GET', ['price' => [10.99, 5.99]]),
            new ArgumentMetadata('price', 'float', true, false, false, attributes: [new MapQueryParameter()]),
            [10.99, 5.99],
            null,
        ];
        yield 'parameter found and boolean yes' => [
            Request::create('/', 'GET', ['isVerified' => 'yes']),
            new ArgumentMetadata('isVerified', 'bool', false, false, false, attributes: [new MapQueryParameter()]),
            [true],
            null,
        ];
        yield 'parameter found and boolean yes variadic' => [
            Request::create('/', 'GET', ['isVerified' => ['yes', 'yes']]),
            new ArgumentMetadata('isVerified', 'bool', true, false, false, attributes: [new MapQueryParameter()]),
            [true, true],
            null,
        ];
        yield 'parameter found and boolean true' => [
            Request::create('/', 'GET', ['isVerified' => 'true']),
            new ArgumentMetadata('isVerified', 'bool', false, false, false, attributes: [new MapQueryParameter()]),
            [true],
            null,
        ];
        yield 'parameter found and boolean 1' => [
            Request::create('/', 'GET', ['isVerified' => '1']),
            new ArgumentMetadata('isVerified', 'bool', false, false, false, attributes: [new MapQueryParameter()]),
            [true],
            null,
        ];
        yield 'parameter found and boolean no' => [
            Request::create('/', 'GET', ['isVerified' => 'no']),
            new ArgumentMetadata('isVerified', 'bool', false, false, false, attributes: [new MapQueryParameter()]),
            [false],
            null,
        ];
        yield 'parameter found and boolean invalid' => [
            Request::create('/', 'GET', ['isVerified' => 'whatever']),
            new ArgumentMetadata('isVerified', 'bool', false, false, false, attributes: [new MapQueryParameter()]),
            [],
            NotFoundHttpException::class,
            'Invalid query parameter "isVerified".',
        ];

        yield 'parameter found and backing value' => [
            Request::create('/', 'GET', ['suit' => 'H']),
            new ArgumentMetadata('suit', Suit::class, false, false, false, attributes: [new MapQueryParameter()]),
            [Suit::Hearts],
            null,
        ];
        yield 'parameter found and backing value variadic' => [
            Request::create('/', 'GET', ['suits' => ['H', 'D']]),
            new ArgumentMetadata('suits', Suit::class, true, false, false, attributes: [new MapQueryParameter()]),
            [Suit::Hearts, Suit::Diamonds],
            null,
        ];
        yield 'parameter found and backing value not int nor string' => [
            Request::create('/', 'GET', ['suit' => 1]),
            new ArgumentMetadata('suit', Suit::class, false, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_BOOL)]),
            [],
            NotFoundHttpException::class,
            'Invalid query parameter "suit".',
        ];
        yield 'parameter found and backing value not int nor string that fallbacks to null on failure' => [
            Request::create('/', 'GET', ['suit' => 1]),
            new ArgumentMetadata('suit', Suit::class, false, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_BOOL, flags: \FILTER_NULL_ON_FAILURE)]),
            [null],
            null,
        ];
        yield 'parameter found and value not valid backing value' => [
            Request::create('/', 'GET', ['suit' => 'B']),
            new ArgumentMetadata('suit', Suit::class, false, false, false, attributes: [new MapQueryParameter()]),
            [],
            NotFoundHttpException::class,
            'Invalid query parameter "suit".',
        ];
        yield 'parameter found and value not valid backing value that falls back to null on failure' => [
            Request::create('/', 'GET', ['suit' => 'B']),
            new ArgumentMetadata('suit', Suit::class, false, false, false, attributes: [new MapQueryParameter(flags: \FILTER_NULL_ON_FAILURE)]),
            [null],
            null,
        ];
        yield 'parameter found and backing type variadic and at least one backing value not int nor string' => [
            Request::create('/', 'GET', ['suits' => [1, 'D']]),
            new ArgumentMetadata('suits', Suit::class, false, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_BOOL)]),
            [],
            NotFoundHttpException::class,
            'Invalid query parameter "suits".',
        ];
        yield 'parameter found and backing type variadic and at least one backing value not int nor string that fallbacks to null on failure' => [
            Request::create('/', 'GET', ['suits' => [1, 'D']]),
            new ArgumentMetadata('suits', Suit::class, false, false, false, attributes: [new MapQueryParameter(flags: \FILTER_NULL_ON_FAILURE)]),
            [null],
            null,
        ];
        yield 'parameter found and backing type variadic and at least one value not valid backing value' => [
            Request::create('/', 'GET', ['suits' => ['B', 'D']]),
            new ArgumentMetadata('suits', Suit::class, false, false, false, attributes: [new MapQueryParameter()]),
            [],
            NotFoundHttpException::class,
            'Invalid query parameter "suits".',
        ];
        yield 'parameter found and backing type variadic and at least one value not valid backing value that falls back to null on failure' => [
            Request::create('/', 'GET', ['suits' => ['B', 'D']]),
            new ArgumentMetadata('suits', Suit::class, false, false, false, attributes: [new MapQueryParameter(flags: \FILTER_NULL_ON_FAILURE)]),
            [null],
            null,
        ];

        yield 'parameter not found but nullable' => [
            Request::create('/', 'GET'),
            new ArgumentMetadata('firstName', 'string', false, false, false, true, [new MapQueryParameter()]),
            [],
            null,
        ];

        yield 'parameter not found but optional' => [
            Request::create('/', 'GET'),
            new ArgumentMetadata('firstName', 'string', false, true, false, attributes: [new MapQueryParameter()]),
            [],
            null,
        ];

        yield 'parameter not found' => [
            Request::create('/', 'GET'),
            new ArgumentMetadata('firstName', 'string', false, false, false, attributes: [new MapQueryParameter()]),
            [],
            NotFoundHttpException::class,
            'Missing query parameter "firstName".',
        ];

        yield 'unsupported type' => [
            Request::create('/', 'GET', ['standardClass' => 'test']),
            new ArgumentMetadata('standardClass', \stdClass::class, false, false, false, attributes: [new MapQueryParameter()]),
            [],
            \LogicException::class,
            '#[MapQueryParameter] cannot be used on controller argument "$standardClass" of type "stdClass"; one of array, string, int, float, bool or \BackedEnum should be used.',
        ];
        yield 'unsupported type variadic' => [
            Request::create('/', 'GET', ['standardClass' => 'test']),
            new ArgumentMetadata('standardClass', \stdClass::class, true, false, false, attributes: [new MapQueryParameter()]),
            [],
            \LogicException::class,
            '#[MapQueryParameter] cannot be used on controller argument "...$standardClass" of type "stdClass"; one of array, string, int, float, bool or \BackedEnum should be used.',
        ];
    }

    public function testSkipWhenNoAttribute()
    {
        $metadata = new ArgumentMetadata('firstName', 'string', false, true, false);

        self::assertSame([], $this->resolver->resolve(Request::create('/'), $metadata));
    }
}