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

api-platform / core / 18042144451

26 Sep 2025 03:27PM UTC coverage: 21.839% (-0.008%) from 21.847%
18042144451

push

github

soyuka
ci: missing apt-update

11886 of 54426 relevant lines covered (21.84%)

25.39 hits per line

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

0.0
/src/Symfony/Tests/Validator/State/ParameterValidatorProviderTest.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\Symfony\Tests\Validator\State;
15

16
use ApiPlatform\Metadata\Get;
17
use ApiPlatform\Metadata\Link;
18
use ApiPlatform\Metadata\Parameters;
19
use ApiPlatform\Metadata\QueryParameter;
20
use ApiPlatform\OpenApi\Model\Parameter as ModelParameter;
21
use ApiPlatform\State\ParameterNotFound;
22
use ApiPlatform\State\ProviderInterface;
23
use ApiPlatform\Symfony\Validator\State\ParameterValidatorProvider;
24
use ApiPlatform\Validator\Exception\ValidationException;
25
use PHPUnit\Framework\TestCase;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\Validator\Constraints\NotBlank;
28
use Symfony\Component\Validator\ConstraintViolationList;
29
use Symfony\Component\Validator\Validator\ValidatorInterface;
30

31
final class ParameterValidatorProviderTest extends TestCase
32
{
33
    public function testProvideWithoutRequest(): void
34
    {
35
        $validator = $this->createMock(ValidatorInterface::class);
×
36
        $decorated = $this->createMock(ProviderInterface::class);
×
37
        $decorated->expects($this->once())->method('provide')->willReturn(new \stdClass());
×
38

39
        $provider = new ParameterValidatorProvider($validator, $decorated);
×
40
        $result = $provider->provide(new Get(), [], []);
×
41

42
        $this->assertInstanceOf(\stdClass::class, $result);
×
43
    }
44

45
    public function testProvideWithValidationDisabled(): void
46
    {
47
        $validator = $this->createMock(ValidatorInterface::class);
×
48
        $validator->expects($this->never())->method('validate');
×
49
        $decorated = $this->createMock(ProviderInterface::class);
×
50
        $decorated->expects($this->once())->method('provide')->willReturn(new \stdClass());
×
51

52
        $operation = (new Get())->withQueryParameterValidationEnabled(false);
×
53
        $request = new Request();
×
54
        $request->attributes->set('_api_operation', $operation);
×
55

56
        $provider = new ParameterValidatorProvider($validator, $decorated);
×
57
        $result = $provider->provide($operation, [], ['request' => $request]);
×
58

59
        $this->assertInstanceOf(\stdClass::class, $result);
×
60
    }
61

62
    public function testProvideWithNoConstraints(): void
63
    {
64
        $validator = $this->createMock(ValidatorInterface::class);
×
65
        $validator->expects($this->never())->method('validate');
×
66
        $decorated = $this->createMock(ProviderInterface::class);
×
67
        $decorated->expects($this->once())->method('provide')->willReturn(new \stdClass());
×
68

69
        $operation = new Get(parameters: new Parameters([
×
70
            'foo' => new QueryParameter(key: 'foo'),
×
71
        ]));
×
72
        $request = new Request();
×
73
        $request->attributes->set('_api_operation', $operation);
×
74

75
        $provider = new ParameterValidatorProvider($validator, $decorated);
×
76
        $result = $provider->provide($operation, [], ['request' => $request]);
×
77

78
        $this->assertInstanceOf(\stdClass::class, $result);
×
79
    }
80

81
    public function testProvideWithValidParameters(): void
82
    {
83
        $constraint = new NotBlank();
×
84
        $validator = $this->createMock(ValidatorInterface::class);
×
85
        $validator->expects($this->once())->method('validate')->willReturn(new ConstraintViolationList());
×
86
        $decorated = $this->createMock(ProviderInterface::class);
×
87
        $decorated->expects($this->once())->method('provide')->willReturn(new \stdClass());
×
88

89
        $operation = new Get(parameters: new Parameters([
×
90
            'foo' => (new QueryParameter(key: 'foo'))->withConstraints([$constraint])->setValue('bar'),
×
91
        ]));
×
92
        $request = new Request();
×
93
        $request->attributes->set('_api_operation', $operation);
×
94

95
        $provider = new ParameterValidatorProvider($validator, $decorated);
×
96
        $result = $provider->provide($operation, [], ['request' => $request]);
×
97

98
        $this->assertInstanceOf(\stdClass::class, $result);
×
99
    }
100

101
    public function testProvideWithInvalidParameters(): void
102
    {
103
        $this->expectException(ValidationException::class);
×
104

105
        $constraint = new NotBlank();
×
106
        $violationList = new ConstraintViolationList();
×
107
        $violationList->add($this->createMock(\Symfony\Component\Validator\ConstraintViolationInterface::class));
×
108

109
        $validator = $this->createMock(ValidatorInterface::class);
×
110
        $validator->expects($this->once())->method('validate')->willReturn($violationList);
×
111
        $decorated = $this->createMock(ProviderInterface::class);
×
112
        $decorated->expects($this->never())->method('provide');
×
113

114
        $operation = new Get(parameters: new Parameters([
×
115
            'foo' => (new QueryParameter(key: 'foo'))->withConstraints([$constraint])->setValue(new ParameterNotFound()),
×
116
        ]));
×
117
        $request = new Request();
×
118
        $request->attributes->set('_api_operation', $operation);
×
119

120
        $provider = new ParameterValidatorProvider($validator, $decorated);
×
121
        $provider->provide($operation, [], ['request' => $request]);
×
122
    }
123

124
    public function testProvideWithUriVariables(): void
125
    {
126
        $constraint = new NotBlank();
×
127
        $validator = $this->createMock(ValidatorInterface::class);
×
128
        $validator->expects($this->once())->method('validate')->willReturn(new ConstraintViolationList());
×
129
        $decorated = $this->createMock(ProviderInterface::class);
×
130
        $decorated->expects($this->once())->method('provide')->willReturn(new \stdClass());
×
131

132
        $operation = new Get(uriVariables: [
×
133
            'id' => (new Link())->withConstraints([$constraint])->setValue('1'),
×
134
        ]);
×
135
        $request = new Request();
×
136
        $request->attributes->set('_api_operation', $operation);
×
137

138
        $provider = new ParameterValidatorProvider($validator, $decorated);
×
139
        $result = $provider->provide($operation, ['id' => 1], ['request' => $request]);
×
140

141
        $this->assertInstanceOf(\stdClass::class, $result);
×
142
    }
143

144
    public function testGetPropertyWithDeepObject(): void
145
    {
146
        $constraint = new NotBlank();
×
147
        $violationList = new ConstraintViolationList();
×
148
        $violation = $this->createMock(\Symfony\Component\Validator\ConstraintViolationInterface::class);
×
149
        $violation->method('getPropertyPath')->willReturn('[bar]');
×
150
        $violationList->add($violation);
×
151

152
        $validator = $this->createMock(ValidatorInterface::class);
×
153
        $validator->expects($this->once())->method('validate')->willReturn($violationList);
×
154
        $decorated = $this->createMock(ProviderInterface::class);
×
155
        $decorated->expects($this->never())->method('provide');
×
156

157
        $parameter = (new QueryParameter(key: 'foo'))->withConstraints([$constraint])->setValue(new ParameterNotFound());
×
158
        $parameter = $parameter->withOpenApi(new ModelParameter(name: 'foo', in: 'query', style: 'deepObject'));
×
159

160
        $operation = new Get(parameters: new Parameters([
×
161
            'foo' => $parameter,
×
162
        ]));
×
163
        $request = new Request();
×
164
        $request->attributes->set('_api_operation', $operation);
×
165

166
        $provider = new ParameterValidatorProvider($validator, $decorated);
×
167
        try {
168
            $provider->provide($operation, [], ['request' => $request]);
×
169
        } catch (ValidationException $e) {
×
170
            $this->assertEquals('foo[bar]', $e->getConstraintViolationList()->get(0)->getPropertyPath());
×
171
        }
172
    }
173
}
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

© 2026 Coveralls, Inc