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

api-platform / core / 10014117656

19 Jul 2024 08:44PM UTC coverage: 7.856% (-56.3%) from 64.185%
10014117656

push

github

soyuka
Merge branch 'sf/remove-flag'

0 of 527 new or added lines in 83 files covered. (0.0%)

10505 existing lines in 362 files now uncovered.

12705 of 161727 relevant lines covered (7.86%)

26.85 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('class')->willReturn(new ResourceMetadataCollection('class', [
×
41
            new ApiResource(operations: [
×
42
                'operation' => new Get(),
×
43
            ]),
×
44
        ]));
×
45

46
        $request = new Request([], [], ['_api_operation_name' => 'operation', '_api_resource_class' => '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' => '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: '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' => '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, ['request' => $request, 'uri_variables' => $uriVariables, 'resource_class' => 'class', 'previous_data' => $originalData])->willReturn($returnValue);
×
87
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
88
        $listener = new WriteListener($processor, $metadata);
×
89
        $listener->onKernelView(
×
90
            new ViewEvent(
×
91
                $this->createStub(HttpKernelInterface::class),
×
92
                $request,
×
93
                HttpKernelInterface::MAIN_REQUEST,
×
94
                $controllerResult
×
95
            )
×
96
        );
×
97
        $this->assertEquals($returnValue, $request->attributes->get('original_data'));
×
98
    }
99

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

120
    public static function provideNonApiAttributes(): array
121
    {
122
        return [
×
123
            ['_api_persist' => false, '_api_operation_name' => 'dummy'],
×
124
            [],
×
125
        ];
×
126
    }
127

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