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

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

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

54
    public function testCallProvider(): void
55
    {
56
        $provider = $this->createMock(ProviderInterface::class);
×
57
        $provider->expects($this->once())->method('provide');
×
58
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
59
        $request = new Request([], [], ['_api_operation' => new Post(), '_api_operation_name' => 'operation', '_api_resource_class' => 'class']);
×
60
        $request->setMethod('POST');
×
61
        $listener = new DeserializeListener($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('class'));
×
78
        $listener = new DeserializeListener($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
    {
NEW
90
        return [
×
NEW
91
            ['_api_receive' => false, '_api_operation_name' => 'dummy'],
×
NEW
92
            [],
×
NEW
93
        ];
×
94
    }
95

96
    public function testDeserializeFalse(): void
97
    {
98
        $provider = $this->createMock(ProviderInterface::class);
×
99
        $provider->expects($this->never())->method('provide');
×
100
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
101
        $request = new Request([], [], ['_api_operation' => new Post(deserialize: false), '_api_operation_name' => 'operation', '_api_resource_class' => 'class']);
×
102
        $request->setMethod('POST');
×
103
        $listener = new DeserializeListener($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 testDeserializeNullWithGetMethod(): void
114
    {
115
        $provider = $this->createMock(ProviderInterface::class);
×
116
        $provider->expects($this->never())->method('provide');
×
117
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
118
        $request = new Request([], [], ['_api_operation' => new Get(), '_api_operation_name' => 'operation', '_api_resource_class' => 'class']);
×
119
        $listener = new DeserializeListener($provider, $metadata);
×
120
        $listener->onKernelRequest(
×
121
            new RequestEvent(
×
122
                $this->createStub(HttpKernelInterface::class),
×
123
                $request,
×
124
                HttpKernelInterface::MAIN_REQUEST
×
125
            )
×
126
        );
×
127
    }
128
}
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

© 2026 Coveralls, Inc