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

api-platform / core / 13999305480

21 Mar 2025 07:18PM UTC coverage: 8.515% (+0.001%) from 8.514%
13999305480

push

github

soyuka
docs: changelog 4.1.2

13386 of 157201 relevant lines covered (8.52%)

22.91 hits per line

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

0.0
/tests/Fixtures/TestBundle/ApiResource/WithParameter.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\Fixtures\TestBundle\ApiResource;
15

16
use ApiPlatform\Metadata\Get;
17
use ApiPlatform\Metadata\GetCollection;
18
use ApiPlatform\Metadata\HeaderParameter;
19
use ApiPlatform\Metadata\Link;
20
use ApiPlatform\Metadata\Operation;
21
use ApiPlatform\Metadata\Parameter;
22
use ApiPlatform\Metadata\Parameters;
23
use ApiPlatform\Metadata\QueryParameter;
24
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
25
use ApiPlatform\Serializer\Filter\GroupFilter;
26
use ApiPlatform\Tests\Fixtures\TestBundle\Parameter\CustomGroupParameterProvider;
27
use Symfony\Component\HttpFoundation\JsonResponse;
28
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
29
use Symfony\Component\Serializer\Attribute\Groups;
30
use Symfony\Component\Validator\Constraints as Assert;
31

32
#[Get(
33
    uriTemplate: 'with_parameters/{id}{._format}',
×
34
    uriVariables: [
×
35
        'id' => new Link(schema: ['type' => 'uuid'], property: 'id'),
×
36
    ],
×
37
    parameters: [
×
38
        'groups' => new QueryParameter(filter: new GroupFilter(parameterName: 'groups', overrideDefaultGroups: false)),
×
39
        'group' => new QueryParameter(provider: [self::class, 'provideGroup']),
×
40
        'properties' => new QueryParameter(filter: 'my_dummy.property'),
×
41
        'service' => new QueryParameter(provider: CustomGroupParameterProvider::class),
×
42
        'object' => new QueryParameter(provider: new CustomGroupParameterProvider()),
×
43
        'auth' => new HeaderParameter(provider: [self::class, 'restrictAccess']),
×
44
        'priority' => new QueryParameter(provider: [self::class, 'assertSecond'], priority: 10),
×
45
        'priorityb' => new QueryParameter(provider: [self::class, 'assertFirst'], priority: 20),
×
46
        'array' => new QueryParameter(provider: [self::class, 'assertArray'], openApi: false),
×
47
    ],
×
48
    provider: [self::class, 'provide']
×
49
)]
×
50
#[GetCollection(
51
    uriTemplate: 'with_parameters_collection{._format}',
×
52
    parameters: [
×
53
        'hydra' => new QueryParameter(property: 'a', required: true),
×
54
    ],
×
55
    provider: [self::class, 'collectionProvider']
×
56
)]
×
57
#[GetCollection(
58
    uriTemplate: 'validate_parameters{._format}',
×
59
    parameters: [
×
60
        'enum' => new QueryParameter(schema: ['enum' => ['a', 'b'], 'uniqueItems' => true]),
×
61
        'num' => new QueryParameter(schema: ['minimum' => 1, 'maximum' => 3]),
×
62
        'exclusiveNum' => new QueryParameter(schema: ['exclusiveMinimum' => 1, 'exclusiveMaximum' => 3]),
×
63
        'blank' => new QueryParameter(openApi: new OpenApiParameter(name: 'blank', in: 'query', allowEmptyValue: false)),
×
64
        'length' => new QueryParameter(schema: ['maxLength' => 1, 'minLength' => 3]),
×
65
        'array' => new QueryParameter(schema: ['minItems' => 2, 'maxItems' => 3]),
×
66
        'multipleOf' => new QueryParameter(schema: ['multipleOf' => 2]),
×
67
        'int' => new QueryParameter(property: 'a', constraints: [new Assert\Type('integer')], provider: [self::class, 'toInt']),
×
68
        'pattern' => new QueryParameter(schema: ['pattern' => '\d']),
×
69
    ],
×
70
    provider: [self::class, 'collectionProvider']
×
71
)]
×
72
#[GetCollection(
73
    uriTemplate: 'with_disabled_parameter_validation{._format}',
×
74
    parameters: new Parameters([new QueryParameter(key: 'bla', required: true)]),
×
75
    queryParameterValidationEnabled: false,
×
76
    provider: [self::class, 'collectionProvider']
×
77
)]
×
78
#[GetCollection(
79
    uriTemplate: 'with_parameters_header_and_query{._format}',
×
80
    parameters: new Parameters([new QueryParameter(key: 'q'), new HeaderParameter(key: 'q')]),
×
81
    provider: [self::class, 'headerAndQueryProvider']
×
82
)]
×
83
#[GetCollection(
84
    uriTemplate: 'header_required',
×
85
    parameters: [
×
86
        'Req' => new HeaderParameter(required: true, schema: ['type' => 'string']),
×
87
    ],
×
88
    provider: [self::class, 'headerProvider']
×
89
)]
×
90
#[QueryParameter(key: 'everywhere')]
91
class WithParameter
92
{
93
    protected static int $counter = 1;
94
    public int $id = 1;
95

96
    #[Groups(['a'])]
97
    public $a = 'foo';
98
    #[Groups(['b', 'custom'])]
99
    public $b = 'bar';
100

101
    public static function collectionProvider()
102
    {
103
        return [new self()];
×
104
    }
105

106
    public static function provide()
107
    {
108
        return new self();
×
109
    }
110

111
    public static function assertArray(): void
112
    {
113
    }
×
114

115
    public static function assertFirst(): void
116
    {
117
        \assert(1 === static::$counter);
×
118
        ++static::$counter;
×
119
    }
120

121
    public static function assertSecond(): void
122
    {
123
        \assert(2 === static::$counter);
×
124
    }
125

126
    public static function provideGroup(Parameter $parameter, array $parameters = [], array $context = [])
127
    {
128
        $operation = $context['operation'];
×
129

130
        return $operation->withNormalizationContext(['groups' => $parameters['group']]);
×
131
    }
132

133
    public static function restrictAccess(): void
134
    {
135
        throw new AccessDeniedHttpException();
×
136
    }
137

138
    public static function headerAndQueryProvider(Operation $operation, array $uriVariables = [], array $context = []): JsonResponse
139
    {
140
        $parameters = $operation->getParameters();
×
141
        $values = [$parameters->get('q', HeaderParameter::class)->getValue(), $parameters->get('q', QueryParameter::class)->getValue()];
×
142

143
        return new JsonResponse($values);
×
144
    }
145

146
    public static function toInt(Parameter $parameter, array $parameters = [], array $context = []): ?Operation
147
    {
148
        if (null === ($operation = $context['operation'] ?? null)) {
×
149
            return null;
×
150
        }
151

152
        $value = $parameter->getValue();
×
153

154
        if (is_numeric($value)) {
×
155
            $value = (int) $value;
×
156
        }
157

158
        $parameters = $operation->getParameters();
×
159
        $parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties(
×
160
            $parameter->getExtraProperties() + ['_api_values' => $value]
×
161
        ));
×
162

163
        return $operation->withParameters($parameters);
×
164
    }
165

166
    public static function headerProvider(Operation $operation, array $uriVariables = [], array $context = []): JsonResponse
167
    {
168
        $parameters = $operation->getParameters();
×
169
        $values = [$parameters->get('Req', HeaderParameter::class)->getValue()];
×
170

171
        return new JsonResponse($values);
×
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

© 2025 Coveralls, Inc