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

api-platform / core / 14925420478

09 May 2025 09:03AM UTC coverage: 7.739% (+0.6%) from 7.179%
14925420478

Pull #7134

github

web-flow
Merge a0984c209 into 4ddce1f53
Pull Request #7134: refactor(metadata): type parameters to list<string>|string

48 of 134 new or added lines in 15 files covered. (35.82%)

2 existing lines in 2 files now uncovered.

12267 of 158510 relevant lines covered (7.74%)

15.54 hits per line

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

0.0
/tests/Functional/Parameters/ValidationTest.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\Tests\Functional\Parameters;
15

16
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ValidateParameterBeforeProvider;
18
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\WithParameter;
19
use ApiPlatform\Tests\SetupClassResourcesTrait;
20

21
final class ValidationTest extends ApiTestCase
22
{
23
    use SetupClassResourcesTrait;
24

25
    protected static ?bool $alwaysBootKernel = false;
26

27
    /**
28
     * @return class-string[]
29
     */
30
    public static function getResources(): array
31
    {
32
        return [WithParameter::class, ValidateParameterBeforeProvider::class];
×
33
    }
34

35
    public function testWithGroupFilter(): void
36
    {
37
        $response = self::createClient()->request('GET', 'with_parameters_collection');
×
38
        $this->assertArraySubset(['violations' => [['message' => 'The parameter "hydra" is required.']]], $response->toArray(false));
×
39
        $response = self::createClient()->request('GET', 'with_parameters_collection?hydra');
×
40
        $this->assertResponseIsSuccessful();
×
41
    }
42

43
    /**
44
     * @param array<int,array{propertyPath: string, message: string}> $expectedViolations
45
     */
46
    #[\PHPUnit\Framework\Attributes\DataProvider('provideQueryStrings')]
47
    public function testValidation(string $queryString, array $expectedViolations): void
48
    {
49
        $response = self::createClient()->request('GET', 'validate_parameters?'.$queryString);
×
50
        $this->assertArraySubset([
×
51
            'violations' => $expectedViolations,
×
52
        ], $response->toArray(false));
×
53
    }
54

55
    public static function provideQueryStrings(): array
56
    {
57
        return [
×
58
            [
×
59
                'enum[]=c&enum[]=c',
×
60
                [
×
61
                    [
×
NEW
62
                        'propertyPath' => 'enum[0]', 'message' => 'The value you selected is not a valid choice.',
×
NEW
63
                    ],
×
NEW
64
                    [
×
NEW
65
                        'propertyPath' => 'enum[1]', 'message' => 'The value you selected is not a valid choice.',
×
66
                    ],
×
67
                    [
×
NEW
68
                        'message' => 'This collection should contain only unique elements.',
×
69
                    ],
×
70
                ],
×
71
            ],
×
72
            [
×
73
                'blank=',
×
74
                [
×
75
                    [
×
76
                        'propertyPath' => 'blank', 'message' => 'This value should not be blank.',
×
77
                    ],
×
78
                ],
×
79
            ],
×
80
            [
×
81
                'length=toolong',
×
82
                [
×
83
                    ['propertyPath' => 'length', 'message' => 'This value is too long. It should have 1 character or less.'],
×
84
                ],
×
85
            ],
×
86
            [
×
87
                'multipleOf=3',
×
88
                [
×
89
                    ['propertyPath' => 'multipleOf', 'message' => 'This value should be a multiple of 2.'],
×
90
                ],
×
91
            ],
×
92
            [
×
93
                'pattern=no',
×
94
                [
×
95
                    ['propertyPath' => 'pattern', 'message' => 'This value is not valid.'],
×
96
                ],
×
97
            ],
×
98
            [
×
99
                'array[]=1',
×
100
                [
×
101
                    ['propertyPath' => 'array', 'message' => 'This collection should contain 2 elements or more.'],
×
102
                ],
×
103
            ],
×
104
            [
×
105
                'num=5',
×
106
                [
×
NEW
107
                    ['propertyPath' => 'num', 'message' => 'This value should be between 1 and 3.'],
×
108
                ],
×
109
            ],
×
110
            [
×
111
                'exclusiveNum=5',
×
112
                [
×
113
                    ['propertyPath' => 'exclusiveNum', 'message' => 'This value should be less than 3.'],
×
114
                ],
×
115
            ],
×
116
        ];
×
117
    }
118

119
    public function testBlank(): void
120
    {
121
        self::createClient()->request('GET', 'validate_parameters?blank=f');
×
122
        $this->assertResponseIsSuccessful();
×
123
    }
124

125
    public function testValidateBeforeRead(): void
126
    {
127
        $response = self::createClient()->request('GET', 'query_parameter_validate_before_read');
×
128
        $this->assertArraySubset(['violations' => [['propertyPath' => 'search', 'message' => 'This value should not be blank.']]], $response->toArray(false));
×
129
    }
130

131
    public function testValidatePropertyPlaceholder(): void
132
    {
133
        self::createClient()->request('GET', 'query_parameter_validate_before_read?search=t&sort[id]=asc');
×
134
        $this->assertResponseIsSuccessful();
×
135
        $response = self::createClient()->request('GET', 'query_parameter_validate_before_read?search=t&sort[bar]=asc');
×
136
        $this->assertArraySubset([
×
137
            'violations' => [
×
138
                [
×
139
                    'propertyPath' => 'sort[bar]',
×
140
                    'message' => 'This field was not expected.',
×
141
                ],
×
142
            ],
×
143
        ], $response->toArray(false));
×
144
        $response = self::createClient()->request('GET', 'query_parameter_validate_before_read?search=t&sort[id]=foo');
×
145
        $this->assertArraySubset([
×
146
            'violations' => [
×
147
                [
×
148
                    'propertyPath' => 'sort[id]',
×
149
                    'message' => 'The value you selected is not a valid choice.',
×
150
                ],
×
151
            ],
×
152
        ], $response->toArray(false));
×
153
    }
154

155
    public function testValidateMessage(): void
156
    {
157
        $response = self::createClient()->request('GET', 'validate_parameters?int=test');
×
158
        $this->assertArraySubset([
×
159
            'detail' => 'int: This value should be of type integer.',
×
160
        ], $response->toArray(false));
×
161
    }
162

163
    public function testValidatePattern(): void
164
    {
165
        self::createClient()->request('GET', 'validate_parameters?pattern=2');
×
166
        $this->assertResponseIsSuccessful();
×
167
    }
168
}
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