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

api-platform / core / 18370496742

09 Oct 2025 08:35AM UTC coverage: 0.0% (-21.9%) from 21.92%
18370496742

push

github

soyuka
docs: changelog 4.2.2

0 of 56436 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/Symfony/Tests/EventListener/WriteListenerTest.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\ProcessorInterface;
24
use ApiPlatform\Symfony\EventListener\WriteListener;
25
use PHPUnit\Framework\Attributes\DataProvider;
26
use PHPUnit\Framework\TestCase;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpFoundation\Response;
29
use Symfony\Component\HttpKernel\Event\ViewEvent;
30
use Symfony\Component\HttpKernel\HttpKernelInterface;
31

32
class WriteListenerTest extends TestCase
33
{
34
    public function testFetchOperation(): void
35
    {
36
        $controllerResult = new \stdClass();
×
37
        $processor = $this->createMock(ProcessorInterface::class);
×
38
        $processor->expects($this->once())->method('process')->willReturn(new Response());
×
39
        $metadata = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
×
40
        $metadata->expects($this->once())->method('create')->with(\stdClass::class)->willReturn(new ResourceMetadataCollection(\stdClass::class, [
×
41
            new ApiResource(operations: [
×
42
                'operation' => new Get(),
×
43
            ]),
×
44
        ]));
×
45

46
        $request = new Request([], [], ['_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class]);
×
47
        $listener = new WriteListener($processor, $metadata);
×
48
        $listener->onKernelView(
×
49
            new ViewEvent(
×
50
                $this->createStub(HttpKernelInterface::class),
×
51
                $request,
×
52
                HttpKernelInterface::MAIN_REQUEST,
×
53
                $controllerResult
×
54
            )
×
55
        );
×
56
    }
57

58
    public function testCallProcessor(): void
59
    {
60
        $controllerResult = new \stdClass();
×
61
        $processor = $this->createMock(ProcessorInterface::class);
×
62
        $processor->expects($this->once())->method('process')->willReturn(new Response());
×
63
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
64
        $request = new Request([], [], ['_api_operation' => new Get(), '_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class]);
×
65
        $listener = new WriteListener($processor, $metadata);
×
66
        $listener->onKernelView(
×
67
            new ViewEvent(
×
68
                $this->createStub(HttpKernelInterface::class),
×
69
                $request,
×
70
                HttpKernelInterface::MAIN_REQUEST,
×
71
                $controllerResult
×
72
            )
×
73
        );
×
74
    }
75

76
    public function testCallProcessorContext(): void
77
    {
78
        $operation = new Get(class: \stdClass::class);
×
79
        $controllerResult = new \stdClass();
×
80
        $originalData = new \stdClass();
×
81
        $uriVariables = ['id' => 3];
×
82
        $returnValue = new \stdClass();
×
83
        $request = new Request([], [], ['_api_operation' => $operation, '_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class, '_api_uri_variables' => $uriVariables, 'previous_data' => $originalData]);
×
84
        $processor = $this->createMock(ProcessorInterface::class);
×
85
        $processor->expects($this->once())->method('process')
×
86
            ->with($controllerResult, $operation, $uriVariables, [
×
87
                'request' => $request,
×
88
                'uri_variables' => $uriVariables,
×
89
                'resource_class' => \stdClass::class,
×
90
                'previous_data' => $originalData,
×
91
                'read_data' => null,
×
92
                'data' => null,
×
93
            ])->willReturn($returnValue);
×
94
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
95
        $listener = new WriteListener($processor, $metadata);
×
96
        $listener->onKernelView(
×
97
            new ViewEvent(
×
98
                $this->createStub(HttpKernelInterface::class),
×
99
                $request,
×
100
                HttpKernelInterface::MAIN_REQUEST,
×
101
                $controllerResult
×
102
            )
×
103
        );
×
104
        $this->assertEquals($returnValue, $request->attributes->get('original_data'));
×
105
    }
106

107
    #[DataProvider('provideNonApiAttributes')]
108
    public function testNoCallProcessor(...$attributes): void
109
    {
110
        $controllerResult = new \stdClass();
×
111
        $processor = $this->createMock(ProcessorInterface::class);
×
112
        $processor->expects($this->never())->method('process')->willReturn(new Response());
×
113
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
114
        $metadata->method('create')->willReturn(new ResourceMetadataCollection(\stdClass::class));
×
115
        $request = new Request([], [], $attributes);
×
116
        $listener = new WriteListener($processor, $metadata);
×
117
        $listener->onKernelView(
×
118
            new ViewEvent(
×
119
                $this->createStub(HttpKernelInterface::class),
×
120
                $request,
×
121
                HttpKernelInterface::MAIN_REQUEST,
×
122
                $controllerResult
×
123
            )
×
124
        );
×
125
    }
126

127
    public static function provideNonApiAttributes(): array
128
    {
129
        return [
×
130
            ['_api_persist' => false, '_api_operation_name' => 'dummy'],
×
131
            [],
×
132
        ];
×
133
    }
134

135
    public function testWriteWithUriVariables(): void
136
    {
137
        $controllerResult = new \stdClass();
×
138
        $operation = new Post(uriVariables: ['id' => new Link(identifiers: ['id'])], class: \stdClass::class);
×
139
        $provider = $this->createMock(ProcessorInterface::class);
×
140
        $provider->expects($this->once())->method('process')->with($controllerResult, $operation->withWrite(true), ['id' => 3]);
×
141
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
142
        $uriVariablesConverter = $this->createMock(UriVariablesConverterInterface::class);
×
143
        $uriVariablesConverter->expects($this->once())->method('convert')->with(['id' => '3'], \stdClass::class)->willReturn(['id' => 3]);
×
144
        $request = new Request([], [], ['_api_operation' => $operation, '_api_operation_name' => 'operation', '_api_resource_class' => \stdClass::class, 'id' => '3']);
×
145
        $request->setMethod($operation->getMethod());
×
146
        $listener = new WriteListener($provider, $metadata, uriVariablesConverter: $uriVariablesConverter);
×
147
        $listener->onKernelView(
×
148
            new ViewEvent(
×
149
                $this->createStub(HttpKernelInterface::class),
×
150
                $request,
×
151
                HttpKernelInterface::MAIN_REQUEST,
×
152
                $controllerResult
×
153
            )
×
154
        );
×
155
    }
156
}
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