• 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/AddFormatListenerTest.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\Exception\OperationNotFoundException;
18
use ApiPlatform\Metadata\Get;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
21
use ApiPlatform\State\ProviderInterface;
22
use ApiPlatform\Symfony\EventListener\AddFormatListener;
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 AddFormatListenerTest 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 Get(),
×
39
            ]),
×
40
        ]));
×
41

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

52
    public function testCallProvider(): void
53
    {
54
        $provider = $this->createMock(ProviderInterface::class);
×
55
        $provider->expects($this->once())->method('provide');
×
56
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
57

58
        $listener = new AddFormatListener($provider, $metadata);
×
59
        $listener->onKernelRequest(
×
60
            new RequestEvent(
×
61
                $this->createStub(HttpKernelInterface::class),
×
62
                new Request([], [], ['_api_operation' => new Get(), '_api_operation_name' => 'operation', '_api_resource_class' => 'class']),
×
63
                HttpKernelInterface::MAIN_REQUEST
×
64
            )
×
65
        );
×
66
    }
67

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

85
    public static function provideNonApiAttributes(): array
86
    {
87
        return [
×
88
            ['_api_respond' => false, '_api_operation_name' => 'dummy'],
×
89
            [],
×
90
        ];
×
91
    }
92

93
    #[DataProvider('provideOperationNotFound')]
94
    public function testNoOperation(...$attributes): void
95
    {
NEW
96
        $this->expectException(OperationNotFoundException::class);
×
NEW
97
        $provider = $this->createMock(ProviderInterface::class);
×
NEW
98
        $provider->expects($this->never())->method('provide');
×
NEW
99
        $metadata = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
×
NEW
100
        $metadata->method('create')->willReturn(new ResourceMetadataCollection($attributes['_api_resource_class'] ?? ''));
×
NEW
101
        $listener = new AddFormatListener($provider, $metadata);
×
NEW
102
        $listener->onKernelRequest(
×
NEW
103
            new RequestEvent(
×
NEW
104
                $this->createStub(HttpKernelInterface::class),
×
NEW
105
                new Request([], [], $attributes),
×
NEW
106
                HttpKernelInterface::MAIN_REQUEST
×
NEW
107
            )
×
NEW
108
        );
×
109
    }
110

111
    public static function provideOperationNotFound(): array
112
    {
NEW
113
        return [
×
NEW
114
            ['_api_resource_class' => 'dummy'],
×
NEW
115
            ['_api_resource_class' => 'dummy', '_api_operation_name' => 'dummy'],
×
NEW
116
        ];
×
117
    }
118
}
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