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

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 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
        'auth' => new HeaderParameter(provider: [self::class, 'restrictAccess']),
×
43
        'priority' => new QueryParameter(provider: [self::class, 'assertSecond'], priority: 10),
×
44
        'priorityb' => new QueryParameter(provider: [self::class, 'assertFirst'], priority: 20),
×
45
        'array' => new QueryParameter(provider: [self::class, 'assertArray'], openApi: false),
×
46
    ],
×
47
    provider: [self::class, 'provide']
×
UNCOV
48
)]
×
49
#[GetCollection(
50
    uriTemplate: 'with_parameters_collection{._format}',
×
51
    parameters: [
×
52
        'hydra' => new QueryParameter(property: 'a', required: true),
×
53
    ],
×
54
    provider: [self::class, 'collectionProvider']
×
UNCOV
55
)]
×
56
#[GetCollection(
57
    uriTemplate: 'validate_parameters{._format}',
×
58
    parameters: [
×
59
        'enum' => new QueryParameter(schema: ['enum' => ['a', 'b'], 'uniqueItems' => true]),
×
60
        'num' => new QueryParameter(schema: ['minimum' => 1, 'maximum' => 3]),
×
61
        'exclusiveNum' => new QueryParameter(schema: ['exclusiveMinimum' => 1, 'exclusiveMaximum' => 3]),
×
62
        'blank' => new QueryParameter(openApi: new OpenApiParameter(name: 'blank', in: 'query', allowEmptyValue: false)),
×
63
        'length' => new QueryParameter(schema: ['maxLength' => 1, 'minLength' => 3]),
×
64
        'array' => new QueryParameter(schema: ['minItems' => 2, 'maxItems' => 3]),
×
65
        'multipleOf' => new QueryParameter(schema: ['multipleOf' => 2]),
×
66
        'int' => new QueryParameter(property: 'a', constraints: [new Assert\Type('integer')], provider: [self::class, 'toInt']),
×
67
        'pattern' => new QueryParameter(schema: ['pattern' => '\d']),
×
68
    ],
×
UNCOV
69
    provider: [self::class, 'collectionProvider']
×
70
)]
×
71
#[GetCollection(
72
    uriTemplate: 'with_disabled_parameter_validation{._format}',
×
73
    parameters: new Parameters([new QueryParameter(key: 'bla', required: true)]),
×
74
    queryParameterValidationEnabled: false,
×
UNCOV
75
    provider: [self::class, 'collectionProvider']
×
76
)]
×
77
#[GetCollection(
78
    uriTemplate: 'with_parameters_header_and_query{._format}',
×
79
    parameters: new Parameters([new QueryParameter(key: 'q'), new HeaderParameter(key: 'q')]),
×
UNCOV
80
    provider: [self::class, 'headerAndQueryProvider']
×
UNCOV
81
)]
×
82
#[QueryParameter(key: 'everywhere')]
83
class WithParameter
84
{
85
    protected static int $counter = 1;
86
    public int $id = 1;
87

88
    #[Groups(['a'])]
89
    public $a = 'foo';
90
    #[Groups(['b', 'custom'])]
91
    public $b = 'bar';
92

93
    public static function collectionProvider()
94
    {
UNCOV
95
        return [new self()];
×
96
    }
97

98
    public static function provide()
99
    {
UNCOV
100
        return new self();
×
101
    }
102

103
    public static function assertArray(): void
104
    {
UNCOV
105
    }
×
106

107
    public static function assertFirst(): void
108
    {
UNCOV
109
        \assert(1 === static::$counter);
×
UNCOV
110
        ++static::$counter;
×
111
    }
112

113
    public static function assertSecond(): void
114
    {
UNCOV
115
        \assert(2 === static::$counter);
×
116
    }
117

118
    public static function provideGroup(Parameter $parameter, array $parameters = [], array $context = [])
119
    {
120
        $operation = $context['operation'];
×
121

UNCOV
122
        return $operation->withNormalizationContext(['groups' => $parameters['group']]);
×
123
    }
124

125
    public static function restrictAccess(): void
126
    {
UNCOV
127
        throw new AccessDeniedHttpException();
×
128
    }
129

130
    public static function headerAndQueryProvider(Operation $operation, array $uriVariables = [], array $context = [])
131
    {
UNCOV
132
        $parameters = $operation->getParameters();
×
133
        $values = [$parameters->get('q', HeaderParameter::class)->getValue(), $parameters->get('q', QueryParameter::class)->getValue()];
×
134

UNCOV
135
        return new JsonResponse($values);
×
136
    }
137

138
    public static function toInt(Parameter $parameter, array $parameters = [], array $context = []): ?Operation
139
    {
UNCOV
140
        if (null === ($operation = $context['operation'] ?? null)) {
×
UNCOV
141
            return null;
×
142
        }
143

UNCOV
144
        $value = $parameter->getValue();
×
145

UNCOV
146
        if (is_numeric($value)) {
×
UNCOV
147
            $value = (int) $value;
×
148
        }
149

UNCOV
150
        $parameters = $operation->getParameters();
×
UNCOV
151
        $parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties(
×
UNCOV
152
            $parameter->getExtraProperties() + ['_api_values' => $value]
×
UNCOV
153
        ));
×
154

UNCOV
155
        return $operation->withParameters($parameters);
×
156
    }
157
}
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