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

api-platform / core / 20135591630

11 Dec 2025 01:58PM UTC coverage: 25.294% (+0.002%) from 25.292%
20135591630

push

github

web-flow
feat(symfony): isGranted before provider (#7500)

35 of 41 new or added lines in 3 files covered. (85.37%)

193 existing lines in 6 files now uncovered.

14672 of 58005 relevant lines covered (25.29%)

29.35 hits per line

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

0.0
/src/Metadata/Tests/Resource/Factory/ParameterResourceMetadataCollectionFactoryTest.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\Metadata\Tests\Resource\Factory;
15

16
use ApiPlatform\Metadata\ApiProperty;
17
use ApiPlatform\Metadata\ApiResource;
18
use ApiPlatform\Metadata\FilterInterface;
19
use ApiPlatform\Metadata\GetCollection;
20
use ApiPlatform\Metadata\Parameters;
21
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
22
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
23
use ApiPlatform\Metadata\Property\PropertyNameCollection;
24
use ApiPlatform\Metadata\QueryParameter;
25
use ApiPlatform\Metadata\Resource\Factory\AttributesResourceMetadataCollectionFactory;
26
use ApiPlatform\Metadata\Resource\Factory\ParameterResourceMetadataCollectionFactory;
27
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\WithLimitedPropertyParameter;
28
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\WithParameter;
29
use ApiPlatform\OpenApi\Model\Parameter;
30
use PHPUnit\Framework\TestCase;
31
use Psr\Container\ContainerInterface;
32

33
class ParameterResourceMetadataCollectionFactoryTest extends TestCase
34
{
35
    public function testParameterFactory(): void
36
    {
37
        $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class);
×
38
        $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'hydra', 'everywhere']));
×
39
        $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class);
×
40
        $propertyMetadata->method('create')->willReturnOnConsecutiveCalls(
×
41
            new ApiProperty(identifier: true),
×
42
            new ApiProperty(readable: true),
×
43
            new ApiProperty(readable: true),
×
44
            new ApiProperty(identifier: true),
×
UNCOV
45
            new ApiProperty(readable: true),
×
UNCOV
46
            new ApiProperty(readable: true)
×
UNCOV
47
        );
×
48
        $filterLocator = $this->createStub(ContainerInterface::class);
×
49
        $filterLocator->method('has')->willReturn(true);
×
50
        $filterLocator->method('get')->willReturn(new class implements FilterInterface {
×
51
            public function getDescription(string $resourceClass): array
52
            {
53
                // @phpstan-ignore-next-line return.type
54
                return [
×
55
                    'hydra' => [
×
56
                        'property' => 'hydra',
×
57
                        'type' => 'string',
×
58
                        'required' => false,
×
59
                        'schema' => ['type' => 'foo'],
×
60
                        'openapi' => new Parameter('test', 'query'),
×
61
                    ],
×
62
                    'everywhere' => [
×
UNCOV
63
                        'property' => 'everywhere',
×
64
                        'type' => 'string',
×
65
                        'required' => false,
×
66
                        'openapi' => ['allowEmptyValue' => true],
×
67
                    ],
×
68
                ];
×
69
            }
70
        });
×
71
        $parameter = new ParameterResourceMetadataCollectionFactory(
×
72
            $nameCollection,
×
73
            $propertyMetadata,
×
74
            new AttributesResourceMetadataCollectionFactory(),
×
75
            $filterLocator
×
76
        );
×
77
        $operation = $parameter->create(WithParameter::class)->getOperation('collection');
×
UNCOV
78
        $this->assertInstanceOf(Parameters::class, $parameters = $operation->getParameters());
×
UNCOV
79
        $hydraParameter = $parameters->get('hydra', QueryParameter::class);
×
UNCOV
80
        $this->assertEquals(['type' => 'foo'], $hydraParameter->getSchema());
×
UNCOV
81
        $this->assertEquals(new Parameter('test', 'query'), $hydraParameter->getOpenApi());
×
82
        $everywhere = $parameters->get('everywhere', QueryParameter::class);
×
83
        $this->assertNull($everywhere->getOpenApi());
×
84
    }
85

86
    public function testQueryParameterWithPropertyPlaceholder(): void
87
    {
88
        $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class);
×
89
        $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'name', 'description']));
×
90

91
        $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class);
×
92
        $propertyMetadata->method('create')->willReturn(
×
93
            new ApiProperty(readable: true),
×
94
        );
×
95

96
        $filterLocator = $this->createStub(ContainerInterface::class);
×
97
        $filterLocator->method('has')->willReturn(false); // No specific filter logic needed for this test
×
98

UNCOV
99
        $parameterFactory = new ParameterResourceMetadataCollectionFactory(
×
UNCOV
100
            $nameCollection,
×
UNCOV
101
            $propertyMetadata,
×
UNCOV
102
            new AttributesResourceMetadataCollectionFactory(),
×
103
            $filterLocator
×
104
        );
×
105

106
        $resourceMetadataCollection = $parameterFactory->create(HasParameterAttribute::class);
×
107
        $operation = $resourceMetadataCollection->getOperation(forceCollection: true);
×
108
        $parameters = $operation->getParameters();
×
109

UNCOV
110
        $this->assertInstanceOf(Parameters::class, $parameters);
×
111

112
        // Assert that the original parameter with ':property' is removed
UNCOV
113
        $this->assertFalse($parameters->has('search[:property]'));
×
114

115
        // Assert that the new parameters are created and have the correct properties
116
        $this->assertTrue($parameters->has('search[name]'));
×
117
        $this->assertTrue($parameters->has('search[description]'));
×
118
        $this->assertTrue($parameters->has('static_param'));
×
119

120
        $searchNameParam = $parameters->get('search[name]');
×
UNCOV
121
        $this->assertInstanceOf(QueryParameter::class, $searchNameParam);
×
122
        $this->assertSame('Search by property', $searchNameParam->getDescription());
×
123
        $this->assertSame('name', $searchNameParam->getProperty());
×
124
        $this->assertSame('search[name]', $searchNameParam->getKey());
×
125

126
        $searchDescriptionParam = $parameters->get('search[description]');
×
127
        $this->assertInstanceOf(QueryParameter::class, $searchDescriptionParam);
×
128
        $this->assertSame('Search by property', $searchDescriptionParam->getDescription());
×
129
        $this->assertSame('description', $searchDescriptionParam->getProperty());
×
130
        $this->assertSame('search[description]', $searchDescriptionParam->getKey());
×
131

132
        $staticParam = $parameters->get('static_param');
×
133
        $this->assertInstanceOf(QueryParameter::class, $staticParam);
×
134
        $this->assertSame('A static parameter', $staticParam->getDescription());
×
135
        $this->assertNull($staticParam->getProperty());
×
UNCOV
136
        $this->assertSame('static_param', $staticParam->getKey());
×
137
    }
138

139
    public function testParameterFactoryNoFilter(): void
140
    {
UNCOV
141
        $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class);
×
UNCOV
142
        $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'hydra', 'everywhere']));
×
UNCOV
143
        $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class);
×
UNCOV
144
        $propertyMetadata->method('create')->willReturnOnConsecutiveCalls(
×
UNCOV
145
            new ApiProperty(identifier: true),
×
UNCOV
146
            new ApiProperty(readable: true),
×
UNCOV
147
            new ApiProperty(readable: true),
×
UNCOV
148
            new ApiProperty(identifier: true),
×
UNCOV
149
            new ApiProperty(readable: true),
×
UNCOV
150
            new ApiProperty(readable: true)
×
UNCOV
151
        );
×
UNCOV
152
        $filterLocator = $this->createStub(ContainerInterface::class);
×
UNCOV
153
        $filterLocator->method('has')->willReturn(false);
×
UNCOV
154
        $parameter = new ParameterResourceMetadataCollectionFactory(
×
UNCOV
155
            $nameCollection,
×
UNCOV
156
            $propertyMetadata,
×
UNCOV
157
            new AttributesResourceMetadataCollectionFactory(),
×
UNCOV
158
            $filterLocator
×
UNCOV
159
        );
×
UNCOV
160
        $operation = $parameter->create(WithParameter::class)->getOperation('collection');
×
UNCOV
161
        $this->assertInstanceOf(Parameters::class, $parameters = $operation->getParameters());
×
162
    }
163

164
    public function testParameterFactoryWithLimitedProperties(): void
165
    {
UNCOV
166
        $nameCollection = $this->createMock(PropertyNameCollectionFactoryInterface::class);
×
UNCOV
167
        $nameCollection->expects($this->never())->method('create');
×
168

UNCOV
169
        $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class);
×
UNCOV
170
        $propertyMetadata->method('create')->willReturnMap([
×
UNCOV
171
            [WithLimitedPropertyParameter::class, 'name', [], new ApiProperty(readable: true)],
×
UNCOV
172
        ]);
×
173

UNCOV
174
        $filterLocator = $this->createStub(ContainerInterface::class);
×
UNCOV
175
        $filterLocator->method('has')->willReturn(false);
×
176

UNCOV
177
        $attributesFactory = new AttributesResourceMetadataCollectionFactory();
×
UNCOV
178
        $parameterFactory = new ParameterResourceMetadataCollectionFactory(
×
UNCOV
179
            $nameCollection,
×
UNCOV
180
            $propertyMetadata,
×
UNCOV
181
            $attributesFactory,
×
UNCOV
182
            $filterLocator
×
UNCOV
183
        );
×
184

UNCOV
185
        $resourceCollection = $parameterFactory->create(WithLimitedPropertyParameter::class);
×
UNCOV
186
        $operation = $resourceCollection->getOperation('collection');
×
UNCOV
187
        $parameters = $operation->getParameters();
×
188

UNCOV
189
        $this->assertInstanceOf(Parameters::class, $parameters);
×
UNCOV
190
        $this->assertCount(1, $parameters);
×
UNCOV
191
        $this->assertTrue($parameters->has('name'));
×
UNCOV
192
        $this->assertFalse($parameters->has('id'));
×
UNCOV
193
        $this->assertFalse($parameters->has('description'));
×
194

UNCOV
195
        $param = $parameters->get('name');
×
UNCOV
196
        $this->assertInstanceOf(QueryParameter::class, $param);
×
UNCOV
197
        $this->assertSame('name', $param->getKey());
×
UNCOV
198
        $this->assertSame(['name'], $param->getProperties());
×
199
    }
200
}
201

202
#[ApiResource(
UNCOV
203
    operations: [
×
UNCOV
204
        new GetCollection(
×
UNCOV
205
            parameters: [
×
UNCOV
206
                'search[:property]' => new QueryParameter(
×
UNCOV
207
                    description: 'Search by property',
×
UNCOV
208
                    properties: ['name', 'description']
×
UNCOV
209
                ),
×
UNCOV
210
                'static_param' => new QueryParameter(
×
UNCOV
211
                    description: 'A static parameter'
×
UNCOV
212
                ),
×
UNCOV
213
            ]
×
UNCOV
214
        ),
×
UNCOV
215
    ]
×
UNCOV
216
)]
×
217
class HasParameterAttribute
218
{
219
    public $id;
220
    public $name;
221
    public $description;
222
}
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