• 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/SerializeListenerTest.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\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
20
use ApiPlatform\State\ProcessorInterface;
21
use ApiPlatform\Symfony\EventListener\SerializeListener;
22
use PHPUnit\Framework\Attributes\DataProvider;
23
use PHPUnit\Framework\TestCase;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\HttpKernel\Event\ViewEvent;
27
use Symfony\Component\HttpKernel\HttpKernelInterface;
28

29
class SerializeListenerTest extends TestCase
30
{
31
    public function testFetchOperation(): void
32
    {
33
        $controllerResult = new \stdClass();
×
34
        $processor = $this->createMock(ProcessorInterface::class);
×
35
        $processor->expects($this->once())->method('process')->willReturn(new Response());
×
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 Get(),
×
40
            ]),
×
41
        ]));
×
42

NEW
43
        $request = new Request([], [], ['_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class]);
×
44
        $listener = new SerializeListener($processor, $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 testCallProcessor(): void
56
    {
57
        $controllerResult = new \stdClass();
×
58
        $processor = $this->createMock(ProcessorInterface::class);
×
59
        $processor->expects($this->once())->method('process')->willReturn(new Response());
×
60
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
NEW
61
        $request = new Request([], [], ['_api_operation' => new Get(), '_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class]);
×
62
        $listener = new SerializeListener($processor, $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 testCallProcessorContext(): void
74
    {
NEW
75
        $operation = new Get(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
        $processor = $this->createMock(ProcessorInterface::class);
×
80
        $processor->expects($this->once())->method('process')
×
NEW
81
            ->with($controllerResult, $operation->withSerialize(true), $uriVariables, ['request' => $request, 'uri_variables' => $uriVariables, 'resource_class' => \stdClass::class])->willReturn(new Response());
×
82
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
83
        $listener = new SerializeListener($processor, $metadata);
×
84
        $listener->onKernelView(
×
85
            new ViewEvent(
×
86
                $this->createStub(HttpKernelInterface::class),
×
87
                $request,
×
88
                HttpKernelInterface::MAIN_REQUEST,
×
89
                $controllerResult
×
90
            )
×
91
        );
×
92
    }
93

94
    #[DataProvider('provideNonApiAttributes')]
95
    public function testNoCallProcessor(...$attributes): void
96
    {
97
        $controllerResult = new \stdClass();
×
98
        $processor = $this->createMock(ProcessorInterface::class);
×
99
        $processor->expects($this->never())->method('process')->willReturn(new Response());
×
100
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
NEW
101
        $metadata->method('create')->willReturn(new ResourceMetadataCollection(\stdClass::class));
×
102
        $request = new Request([], [], $attributes);
×
103
        $listener = new SerializeListener($processor, $metadata);
×
104
        $listener->onKernelView(
×
105
            new ViewEvent(
×
106
                $this->createStub(HttpKernelInterface::class),
×
107
                $request,
×
108
                HttpKernelInterface::MAIN_REQUEST,
×
109
                $controllerResult
×
110
            )
×
111
        );
×
112
    }
113

114
    public static function provideNonApiAttributes(): array
115
    {
116
        return [
×
117
            ['_api_respond' => false, '_api_operation_name' => 'dummy'],
×
118
            [],
×
119
        ];
×
120
    }
121
}
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