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

api-platform / core / 16414174899

21 Jul 2025 10:10AM UTC coverage: 22.039%. Remained the same
16414174899

Pull #7307

github

web-flow
Merge 66cefee02 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%)

1 existing line in 1 file now uncovered.

11533 of 52331 relevant lines covered (22.04%)

23.45 hits per line

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

0.0
/src/Symfony/Tests/EventListener/ReadListenerTest.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\Get;
18
use ApiPlatform\Metadata\Link;
19
use ApiPlatform\Metadata\Post;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
22
use ApiPlatform\Metadata\UriVariablesConverterInterface;
23
use ApiPlatform\State\ProviderInterface;
24
use ApiPlatform\Symfony\EventListener\ReadListener;
25
use PHPUnit\Framework\Attributes\DataProvider;
26
use PHPUnit\Framework\TestCase;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpKernel\Event\RequestEvent;
29
use Symfony\Component\HttpKernel\HttpKernelInterface;
30

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

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

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

71
    #[DataProvider('provideNonApiAttributes')]
72
    public function testNoCallProvider(...$attributes): void
73
    {
74
        $provider = $this->createMock(ProviderInterface::class);
×
75
        $provider->expects($this->never())->method('provide');
×
76
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
NEW
77
        $metadata->method('create')->willReturn(new ResourceMetadataCollection(\stdClass::class));
×
78
        $listener = new ReadListener($provider, $metadata);
×
79
        $listener->onKernelRequest(
×
80
            new RequestEvent(
×
81
                $this->createStub(HttpKernelInterface::class),
×
82
                new Request([], [], $attributes),
×
83
                HttpKernelInterface::MAIN_REQUEST
×
84
            )
×
85
        );
×
86
    }
87

88
    public static function provideNonApiAttributes(): array
89
    {
90
        return [
×
91
            ['_api_receive' => false, '_api_operation_name' => 'dummy'],
×
92
            [],
×
93
        ];
×
94
    }
95

96
    public function testReadFalse(): void
97
    {
98
        $operation = new Get(read: false);
×
99
        $provider = $this->createMock(ProviderInterface::class);
×
100
        $provider->expects($this->once())->method('provide')->with($operation);
×
101
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
NEW
102
        $request = new Request([], [], ['_api_operation' => $operation, '_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class]);
×
103
        $listener = new ReadListener($provider, $metadata);
×
104
        $listener->onKernelRequest(
×
105
            new RequestEvent(
×
106
                $this->createStub(HttpKernelInterface::class),
×
107
                $request,
×
108
                HttpKernelInterface::MAIN_REQUEST
×
109
            )
×
110
        );
×
111
    }
112

113
    public function testReadWithUriVariables(): void
114
    {
NEW
115
        $operation = new Get(uriVariables: ['id' => new Link(identifiers: ['id'])], class: \stdClass::class);
×
116
        $provider = $this->createMock(ProviderInterface::class);
×
117
        $provider->expects($this->once())->method('provide')->with($operation->withRead(true), ['id' => 3]);
×
118
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
119
        $uriVariablesConverter = $this->createMock(UriVariablesConverterInterface::class);
×
NEW
120
        $uriVariablesConverter->expects($this->once())->method('convert')->with(['id' => '3'], \stdClass::class)->willReturn(['id' => 3]);
×
NEW
121
        $request = new Request([], [], ['_api_operation' => $operation, '_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class, 'id' => '3']);
×
122
        $listener = new ReadListener($provider, $metadata, $uriVariablesConverter);
×
123
        $listener->onKernelRequest(
×
124
            new RequestEvent(
×
125
                $this->createStub(HttpKernelInterface::class),
×
126
                $request,
×
127
                HttpKernelInterface::MAIN_REQUEST
×
128
            )
×
129
        );
×
130
    }
131

132
    public function testReadNullWithPostMethod(): void
133
    {
134
        $operation = new Post(read: false);
×
135
        $provider = $this->createMock(ProviderInterface::class);
×
136
        $provider->expects($this->once())->method('provide')->with($operation);
×
137
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
NEW
138
        $request = new Request([], [], ['_api_operation' => new Post(), '_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class]);
×
139
        $request->setMethod('POST');
×
140
        $listener = new ReadListener($provider, $metadata);
×
141
        $listener->onKernelRequest(
×
142
            new RequestEvent(
×
143
                $this->createStub(HttpKernelInterface::class),
×
144
                $request,
×
145
                HttpKernelInterface::MAIN_REQUEST
×
146
            )
×
147
        );
×
148
    }
149
}
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