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

api-platform / core / 14125571141

28 Mar 2025 09:08AM UTC coverage: 8.516% (+0.004%) from 8.512%
14125571141

push

github

web-flow
fix(symfony): serialization of closure inside the profiler (#7054)

12 of 19 new or added lines in 3 files covered. (63.16%)

15 existing lines in 2 files now uncovered.

13386 of 157195 relevant lines covered (8.52%)

22.91 hits per line

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

0.0
/tests/Symfony/Bundle/DataCollector/RequestDataCollectorTest.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\Tests\Symfony\Bundle\DataCollector;
15

16
use ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\Get;
18
use ApiPlatform\Metadata\Link;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
21
use ApiPlatform\Symfony\Bundle\DataCollector\RequestDataCollector;
22
use ApiPlatform\Tests\Fixtures\DummyEntity;
23
use PHPUnit\Framework\MockObject\MockObject;
24
use PHPUnit\Framework\TestCase;
25
use Prophecy\PhpUnit\ProphecyTrait;
26
use Prophecy\Prophecy\ObjectProphecy;
27
use Psr\Container\ContainerInterface;
28
use Symfony\Component\HttpFoundation\ParameterBag;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpFoundation\Response;
31
use Symfony\Component\VarDumper\Cloner\Data;
32

33
/**
34
 * @author Julien DENIAU <julien.deniau@gmail.com>
35
 */
36
class RequestDataCollectorTest extends TestCase
37
{
38
    use ProphecyTrait;
39

40
    private ObjectProphecy|Request $request;
41
    private MockObject|Response $response;
42
    private ObjectProphecy|ParameterBag $attributes;
43
    private ObjectProphecy|ResourceMetadataCollectionFactoryInterface $metadataFactory;
44
    private ObjectProphecy|ContainerInterface $filterLocator;
45

46
    protected function setUp(): void
47
    {
48
        $this->response = $this->createMock(Response::class);
×
49
        $this->attributes = $this->prophesize(ParameterBag::class);
×
50
        $this->request = $this->prophesize(Request::class);
×
51
        $this->request
×
52
            ->getAcceptableContentTypes()
×
53
            ->shouldBeCalled()
×
54
            ->willReturn(['foo', 'bar']);
×
55

56
        $this->metadataFactory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
57
        $this->filterLocator = $this->prophesize(ContainerInterface::class);
×
58
    }
59

60
    public function testNoResourceClass(): void
61
    {
62
        $this->apiResourceClassWillReturn(null);
×
63

64
        $dataCollector = new RequestDataCollector(
×
65
            $this->metadataFactory->reveal(),
×
66
            $this->filterLocator->reveal()
×
67
        );
×
68

69
        $dataCollector->collect(
×
70
            $this->request->reveal(),
×
71
            $this->response
×
72
        );
×
73

74
        $this->assertEquals(['foo', 'bar'], $dataCollector->getAcceptableContentTypes());
×
75
        $this->assertEquals([], $dataCollector->getResources());
×
76
    }
77

78
    public function testNotCallingCollect(): void
79
    {
80
        $this->request
×
81
            ->getAcceptableContentTypes()
×
82
            ->shouldNotBeCalled();
×
83
        $dataCollector = new RequestDataCollector(
×
84
            $this->metadataFactory->reveal(),
×
85
            $this->filterLocator->reveal()
×
86
        );
×
87

88
        $this->assertEquals([], $dataCollector->getAcceptableContentTypes());
×
89
        $this->assertEquals([], $dataCollector->getResources());
×
90
    }
91

92
    public function testWithResource(): void
93
    {
NEW
94
        $this->apiResourceClassWillReturn(DummyEntity::class, ['_api_operation_name' => 'get']);
×
95
        $this->request->attributes = $this->attributes->reveal();
×
96

97
        $this->filterLocator->has('foo')->willReturn(false)->shouldBeCalled();
×
98
        $this->filterLocator->has('a_filter')->willReturn(true)->shouldBeCalled();
×
99
        $this->filterLocator->get('a_filter')->willReturn(new \stdClass())->shouldBeCalled();
×
100

101
        $dataCollector = new RequestDataCollector(
×
102
            $this->metadataFactory->reveal(),
×
103
            $this->filterLocator->reveal()
×
104
        );
×
105

106
        $dataCollector->collect(
×
107
            $this->request->reveal(),
×
108
            $this->response
×
109
        );
×
110

111
        $this->assertEquals(['foo', 'bar'], $dataCollector->getAcceptableContentTypes());
×
112

113
        $resource = $dataCollector->getResources()[0];
×
114
        $this->assertSame(DummyEntity::class, $resource->getResourceClass());
×
115
        $this->assertEquals([['foo' => null, 'a_filter' => \stdClass::class]], $resource->getFilters());
×
116
        $this->assertEquals(['ignored_filters' => 1], $resource->getCounters());
×
117
        $this->assertInstanceOf(Data::class, $resource->getResourceMetadataCollection());
×
118
    }
119

120
    public function testWithResourceWithTraceables(): void
121
    {
122
        $this->apiResourceClassWillReturn(DummyEntity::class);
×
123

124
        $this->filterLocator->has('a_filter')->willReturn(false);
×
125
        $this->filterLocator->has('foo')->willReturn(false);
×
126

127
        $dataCollector = new RequestDataCollector(
×
128
            $this->metadataFactory->reveal(),
×
129
            $this->filterLocator->reveal(),
×
130
        );
×
131

132
        $dataCollector->collect(
×
133
            $this->request->reveal(),
×
134
            $this->response
×
135
        );
×
136
    }
137

138
    private function apiResourceClassWillReturn(?string $data, array $context = []): void
139
    {
140
        $this->attributes->get('_api_resource_class')->shouldBeCalled()->willReturn($data);
×
NEW
141
        $this->attributes->get('_api_operation_name')->shouldBeCalled()->willReturn($context['_api_operation_name'] ?? null);
×
NEW
142
        $this->attributes->get('_api_operation')->shouldBeCalled()->willReturn($context['_api_operation'] ?? new Get());
×
143
        $this->attributes->get('_graphql', false)->shouldBeCalled()->willReturn(false);
×
144
        $this->attributes->all()->willReturn([
×
145
            '_api_resource_class' => $data,
×
146
        ] + $context);
×
147
        $this->request->attributes = $this->attributes->reveal();
×
148

149
        if (!$data) {
×
150
            $this->metadataFactory
×
151
                ->create()
×
152
                ->shouldNotBeCalled();
×
153
        } else {
154
            $this->metadataFactory
×
155
                ->create($data)
×
156
                ->shouldBeCalled()
×
157
                ->willReturn(
×
158
                    new ResourceMetadataCollection($data, [(new ApiResource(operations: [new Get(filters: ['foo', 'a_filter'], uriVariables: ['id' => new Link(parameterName: 'id')])]))->withFilters(['foo', 'a_filter'])])
×
159
                );
×
160
        }
161
    }
162
}
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