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

api-platform / core / 16414261225

21 Jul 2025 10:14AM UTC coverage: 21.806% (-0.2%) from 22.039%
16414261225

Pull #7307

github

web-flow
Merge 92df6bf50 into d3b4b7b40
Pull Request #7307: Use class-string param type for Metadata::getClass

0 of 191 new or added lines in 19 files covered. (0.0%)

44 existing lines in 2 files now uncovered.

11408 of 52317 relevant lines covered (21.81%)

11.64 hits per line

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

0.0
/src/Symfony/Tests/EventListener/ValidateListenerTest.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\EventListener;
15

16
use ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\Delete;
18
use ApiPlatform\Metadata\Post;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
21
use ApiPlatform\State\ProviderInterface;
22
use ApiPlatform\Symfony\EventListener\ValidateListener;
23
use PHPUnit\Framework\Attributes\DataProvider;
24
use PHPUnit\Framework\TestCase;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpKernel\Event\ViewEvent;
27
use Symfony\Component\HttpKernel\HttpKernelInterface;
28

29
class ValidateListenerTest extends TestCase
30
{
31
    public function testFetchOperation(): void
32
    {
33
        $controllerResult = new \stdClass();
×
34
        $provider = $this->createMock(ProviderInterface::class);
×
35
        $provider->expects($this->once())->method('provide');
×
36
        $metadata = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
×
NEW
37
        $metadata->expects($this->once())->method('create')->with(\stdClass::class)->willReturn(new ResourceMetadataCollection(\stdClass::class, [
×
38
            new ApiResource(operations: [
×
39
                'operation' => new Post(),
×
40
            ]),
×
41
        ]));
×
42

NEW
43
        $request = new Request([], [], ['_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class]);
×
44
        $listener = new ValidateListener($provider, $metadata);
×
45
        $listener->onKernelView(
×
46
            new ViewEvent(
×
47
                $this->createStub(HttpKernelInterface::class),
×
48
                $request,
×
49
                HttpKernelInterface::MAIN_REQUEST,
×
50
                $controllerResult
×
51
            )
×
52
        );
×
53
    }
54

55
    public function testCallprovider(): void
56
    {
57
        $controllerResult = new \stdClass();
×
58
        $provider = $this->createMock(ProviderInterface::class);
×
59
        $provider->expects($this->once())->method('provide');
×
60
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
NEW
61
        $request = new Request([], [], ['_api_operation' => new Post(), '_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class]);
×
62
        $listener = new ValidateListener($provider, $metadata);
×
63
        $listener->onKernelView(
×
64
            new ViewEvent(
×
65
                $this->createStub(HttpKernelInterface::class),
×
66
                $request,
×
67
                HttpKernelInterface::MAIN_REQUEST,
×
68
                $controllerResult
×
69
            )
×
70
        );
×
71
    }
72

73
    public function testCallproviderContext(): void
74
    {
NEW
75
        $operation = new Post(class: \stdClass::class);
×
76
        $controllerResult = new \stdClass();
×
77
        $uriVariables = ['id' => 3];
×
NEW
78
        $request = new Request([], [], ['_api_operation' => $operation, '_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class, '_api_uri_variables' => $uriVariables]);
×
79
        $request->setMethod($operation->getMethod());
×
80
        $provider = $this->createMock(ProviderInterface::class);
×
81
        $provider->expects($this->once())->method('provide')
×
NEW
82
            ->with($operation->withValidate(true), $uriVariables, ['request' => $request, 'uri_variables' => $uriVariables, 'resource_class' => \stdClass::class]);
×
83
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
84
        $listener = new ValidateListener($provider, $metadata);
×
85
        $listener->onKernelView(
×
86
            new ViewEvent(
×
87
                $this->createStub(HttpKernelInterface::class),
×
88
                $request,
×
89
                HttpKernelInterface::MAIN_REQUEST,
×
90
                $controllerResult
×
91
            )
×
92
        );
×
93
    }
94

95
    public function testDeleteNoValidate(): void
96
    {
NEW
97
        $operation = new Delete(class: \stdClass::class);
×
98
        $controllerResult = new \stdClass();
×
99
        $uriVariables = ['id' => 3];
×
NEW
100
        $request = new Request([], [], ['_api_operation' => $operation, '_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class, '_api_uri_variables' => $uriVariables]);
×
101
        $request->setMethod($operation->getMethod());
×
102
        $provider = $this->createMock(ProviderInterface::class);
×
103
        $provider->expects($this->once())->method('provide')
×
NEW
104
            ->with($operation->withValidate(false), $uriVariables, ['request' => $request, 'uri_variables' => $uriVariables, 'resource_class' => \stdClass::class]);
×
105
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
106
        $listener = new ValidateListener($provider, $metadata);
×
107
        $listener->onKernelView(
×
108
            new ViewEvent(
×
109
                $this->createStub(HttpKernelInterface::class),
×
110
                $request,
×
111
                HttpKernelInterface::MAIN_REQUEST,
×
112
                $controllerResult
×
113
            )
×
114
        );
×
115
    }
116

117
    public function testDeleteForceValidate(): void
118
    {
NEW
119
        $operation = new Delete(class: \stdClass::class, validate: true);
×
120
        $controllerResult = new \stdClass();
×
121
        $uriVariables = ['id' => 3];
×
NEW
122
        $request = new Request([], [], ['_api_operation' => $operation, '_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class, '_api_uri_variables' => $uriVariables]);
×
123
        $request->setMethod($operation->getMethod());
×
124
        $provider = $this->createMock(ProviderInterface::class);
×
125
        $provider->expects($this->once())->method('provide')
×
NEW
126
            ->with($operation->withValidate(true), $uriVariables, ['request' => $request, 'uri_variables' => $uriVariables, 'resource_class' => \stdClass::class]);
×
127
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
128
        $listener = new ValidateListener($provider, $metadata);
×
129
        $listener->onKernelView(
×
130
            new ViewEvent(
×
131
                $this->createStub(HttpKernelInterface::class),
×
132
                $request,
×
133
                HttpKernelInterface::MAIN_REQUEST,
×
134
                $controllerResult
×
135
            )
×
136
        );
×
137
    }
138

139
    #[DataProvider('provideNonApiAttributes')]
140
    public function testNoCallprovider(...$attributes): void
141
    {
142
        $controllerResult = new \stdClass();
×
143
        $provider = $this->createMock(ProviderInterface::class);
×
144
        $provider->expects($this->never())->method('provide');
×
145
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
NEW
146
        $metadata->method('create')->willReturn(new ResourceMetadataCollection(\stdClass::class));
×
147
        $request = new Request([], [], $attributes);
×
148
        $listener = new ValidateListener($provider, $metadata);
×
149
        $listener->onKernelView(
×
150
            new ViewEvent(
×
151
                $this->createStub(HttpKernelInterface::class),
×
152
                $request,
×
153
                HttpKernelInterface::MAIN_REQUEST,
×
154
                $controllerResult
×
155
            )
×
156
        );
×
157
    }
158

159
    public static function provideNonApiAttributes(): array
160
    {
161
        return [
×
162
            ['_api_respond' => false, '_api_operation_name' => 'dummy'],
×
163
            [],
×
164
        ];
×
165
    }
166
}
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