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

api-platform / core / 20001323174

07 Dec 2025 08:10AM UTC coverage: 25.292% (+0.001%) from 25.291%
20001323174

push

github

soyuka
chore: bump metadata to 4.3.x-dev

14642 of 57891 relevant lines covered (25.29%)

28.94 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\FilterInterface;
18
use ApiPlatform\Metadata\Parameters;
19
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
20
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Property\PropertyNameCollection;
22
use ApiPlatform\Metadata\QueryParameter;
23
use ApiPlatform\Metadata\Resource\Factory\AttributesResourceMetadataCollectionFactory;
24
use ApiPlatform\Metadata\Resource\Factory\ParameterResourceMetadataCollectionFactory;
25
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\WithLimitedPropertyParameter;
26
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\WithParameter;
27
use ApiPlatform\OpenApi\Model\Parameter;
28
use PHPUnit\Framework\TestCase;
29
use Psr\Container\ContainerInterface;
30

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

80
    public function testParameterFactoryNoFilter(): void
81
    {
82
        $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class);
×
83
        $nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'hydra', 'everywhere']));
×
84
        $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class);
×
85
        $propertyMetadata->method('create')->willReturnOnConsecutiveCalls(
×
86
            new ApiProperty(identifier: true), new ApiProperty(readable: true), new ApiProperty(readable: true),
×
87
            new ApiProperty(identifier: true), new ApiProperty(readable: true), new ApiProperty(readable: true)
×
88
        );
×
89
        $filterLocator = $this->createStub(ContainerInterface::class);
×
90
        $filterLocator->method('has')->willReturn(false);
×
91
        $parameter = new ParameterResourceMetadataCollectionFactory(
×
92
            $nameCollection,
×
93
            $propertyMetadata,
×
94
            new AttributesResourceMetadataCollectionFactory(),
×
95
            $filterLocator
×
96
        );
×
97
        $operation = $parameter->create(WithParameter::class)->getOperation('collection');
×
98
        $this->assertInstanceOf(Parameters::class, $parameters = $operation->getParameters());
×
99
    }
100

101
    public function testParameterFactoryWithLimitedProperties(): void
102
    {
103
        $nameCollection = $this->createMock(PropertyNameCollectionFactoryInterface::class);
×
104
        $nameCollection->expects($this->never())->method('create');
×
105

106
        $propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class);
×
107
        $propertyMetadata->method('create')->willReturnMap([
×
108
            [WithLimitedPropertyParameter::class, 'name', [], new ApiProperty(readable: true)],
×
109
        ]);
×
110

111
        $filterLocator = $this->createStub(ContainerInterface::class);
×
112
        $filterLocator->method('has')->willReturn(false);
×
113

114
        $attributesFactory = new AttributesResourceMetadataCollectionFactory();
×
115
        $parameterFactory = new ParameterResourceMetadataCollectionFactory(
×
116
            $nameCollection,
×
117
            $propertyMetadata,
×
118
            $attributesFactory,
×
119
            $filterLocator
×
120
        );
×
121

122
        $resourceCollection = $parameterFactory->create(WithLimitedPropertyParameter::class);
×
123
        $operation = $resourceCollection->getOperation('collection');
×
124
        $parameters = $operation->getParameters();
×
125

126
        $this->assertInstanceOf(Parameters::class, $parameters);
×
127
        $this->assertCount(1, $parameters);
×
128
        $this->assertTrue($parameters->has('name'));
×
129
        $this->assertFalse($parameters->has('id'));
×
130
        $this->assertFalse($parameters->has('description'));
×
131

132
        $param = $parameters->get('name');
×
133
        $this->assertInstanceOf(QueryParameter::class, $param);
×
134
        $this->assertSame('name', $param->getKey());
×
135
        $this->assertSame(['name'], $param->getProperties());
×
136
    }
137
}
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