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

api-platform / core / 13175309672

06 Feb 2025 09:04AM UTC coverage: 7.663% (-0.2%) from 7.841%
13175309672

push

github

web-flow
fix: ensure template files have a tpl file extension (#6826) (#6829)

2 of 5 new or added lines in 4 files covered. (40.0%)

3676 existing lines in 122 files now uncovered.

13073 of 170593 relevant lines covered (7.66%)

27.3 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 Composer\InstalledVersions;
24
use PackageVersions\Versions;
25
use PHPUnit\Framework\MockObject\MockObject;
26
use PHPUnit\Framework\TestCase;
27
use Prophecy\PhpUnit\ProphecyTrait;
28
use Prophecy\Prophecy\ObjectProphecy;
29
use Psr\Container\ContainerInterface;
30
use Symfony\Component\HttpFoundation\ParameterBag;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpFoundation\Response;
33
use Symfony\Component\VarDumper\Cloner\Data;
34

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

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

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

58
        $this->metadataFactory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
UNCOV
59
        $this->filterLocator = $this->prophesize(ContainerInterface::class);
×
60
    }
61

62
    public function testNoResourceClass(): void
63
    {
UNCOV
64
        $this->apiResourceClassWillReturn(null);
×
65

66
        $dataCollector = new RequestDataCollector(
×
67
            $this->metadataFactory->reveal(),
×
68
            $this->filterLocator->reveal()
×
UNCOV
69
        );
×
70

71
        $dataCollector->collect(
×
72
            $this->request->reveal(),
×
73
            $this->response
×
UNCOV
74
        );
×
75

76
        $this->assertEquals([], $dataCollector->getRequestAttributes());
×
77
        $this->assertEquals(['foo', 'bar'], $dataCollector->getAcceptableContentTypes());
×
UNCOV
78
        $this->assertEquals([], $dataCollector->getResources());
×
79
    }
80

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

91
        $this->assertEquals([], $dataCollector->getRequestAttributes());
×
92
        $this->assertEquals([], $dataCollector->getAcceptableContentTypes());
×
UNCOV
93
        $this->assertEquals([], $dataCollector->getResources());
×
94
    }
95

96
    public function testWithResource(): void
97
    {
98
        $this->apiResourceClassWillReturn(DummyEntity::class, ['_api_operation_name' => 'get', '_api_receive' => true]);
×
UNCOV
99
        $this->request->attributes = $this->attributes->reveal();
×
100

101
        $this->filterLocator->has('foo')->willReturn(false)->shouldBeCalled();
×
102
        $this->filterLocator->has('a_filter')->willReturn(true)->shouldBeCalled();
×
UNCOV
103
        $this->filterLocator->get('a_filter')->willReturn(new \stdClass())->shouldBeCalled();
×
104

105
        $dataCollector = new RequestDataCollector(
×
106
            $this->metadataFactory->reveal(),
×
107
            $this->filterLocator->reveal()
×
UNCOV
108
        );
×
109

110
        $dataCollector->collect(
×
111
            $this->request->reveal(),
×
112
            $this->response
×
UNCOV
113
        );
×
114

115
        $this->assertEquals([
×
116
            'resource_class' => DummyEntity::class,
×
117
            'has_composite_identifier' => false,
×
118
            'operation_name' => 'get',
×
119
            'receive' => true,
×
120
            'respond' => true,
×
121
            'persist' => true,
×
122
        ], $dataCollector->getRequestAttributes());
×
UNCOV
123
        $this->assertEquals(['foo', 'bar'], $dataCollector->getAcceptableContentTypes());
×
124

125
        $resource = $dataCollector->getResources()[0];
×
126
        $this->assertSame(DummyEntity::class, $resource->getResourceClass());
×
127
        $this->assertEquals([['foo' => null, 'a_filter' => \stdClass::class]], $resource->getFilters());
×
128
        $this->assertEquals(['ignored_filters' => 1], $resource->getCounters());
×
UNCOV
129
        $this->assertInstanceOf(Data::class, $resource->getResourceMetadataCollection());
×
130
    }
131

132
    public function testWithResourceWithTraceables(): void
133
    {
UNCOV
134
        $this->apiResourceClassWillReturn(DummyEntity::class);
×
135

136
        $this->filterLocator->has('a_filter')->willReturn(false);
×
UNCOV
137
        $this->filterLocator->has('foo')->willReturn(false);
×
138

139
        $dataCollector = new RequestDataCollector(
×
140
            $this->metadataFactory->reveal(),
×
141
            $this->filterLocator->reveal(),
×
UNCOV
142
        );
×
143

144
        $dataCollector->collect(
×
145
            $this->request->reveal(),
×
146
            $this->response
×
UNCOV
147
        );
×
148
    }
149

150
    public function testVersionCollection(): void
151
    {
UNCOV
152
        $this->apiResourceClassWillReturn(DummyEntity::class);
×
153

154
        $this->filterLocator->has('a_filter')->willReturn(false);
×
UNCOV
155
        $this->filterLocator->has('foo')->willReturn(false);
×
156

157
        $dataCollector = new RequestDataCollector(
×
158
            $this->metadataFactory->reveal(),
×
159
            $this->filterLocator->reveal(),
×
UNCOV
160
        );
×
161

162
        $dataCollector->collect(
×
163
            $this->request->reveal(),
×
164
            $this->response
×
UNCOV
165
        );
×
166

UNCOV
167
        if (class_exists(InstalledVersions::class)) {
×
UNCOV
168
            $this->assertTrue(null !== $dataCollector->getVersion());
×
169
        } else {
UNCOV
170
            $this->assertSame(null !== $dataCollector->getVersion(), class_exists(Versions::class));
×
171
        }
172
    }
173

174
    public function testWithPreviousData(): void
175
    {
UNCOV
176
        $data = new \stdClass();
×
177
        $data->a = $data;
×
178

179
        $this->apiResourceClassWillReturn(DummyEntity::class, ['_api_operation_name' => 'get', '_api_receive' => true, 'previous_data' => $data]);
×
UNCOV
180
        $this->request->attributes = $this->attributes->reveal();
×
181

182
        $this->filterLocator->has('foo')->willReturn(false);
×
183
        $this->filterLocator->has('a_filter')->willReturn(true);
×
184
        $this->filterLocator->get('a_filter')->willReturn(new \stdClass());
×
185

186
        $dataCollector = new RequestDataCollector(
×
187
            $this->metadataFactory->reveal(),
×
188
            $this->filterLocator->reveal()
×
189
        );
×
190

191
        $dataCollector->collect(
×
192
            $this->request->reveal(),
×
UNCOV
193
            $this->response
×
UNCOV
194
        );
×
195

UNCOV
196
        $this->assertArrayHasKey('previous_data', $requestAttributes = $dataCollector->getRequestAttributes());
×
197
        $this->assertNotSame($requestAttributes['previous_data']->data, $requestAttributes['previous_data']);
×
198
    }
199

200
    private function apiResourceClassWillReturn(?string $data, array $context = []): void
201
    {
202
        $this->attributes->get('_api_resource_class')->shouldBeCalled()->willReturn($data);
×
UNCOV
203
        $this->attributes->get('_graphql', false)->shouldBeCalled()->willReturn(false);
×
204
        $this->attributes->all()->willReturn([
×
205
            '_api_resource_class' => $data,
×
206
        ] + $context);
×
207
        $this->request->attributes = $this->attributes->reveal();
×
208

209
        if (!$data) {
×
210
            $this->metadataFactory
×
211
                ->create()
×
212
                ->shouldNotBeCalled();
×
213
        } else {
214
            $this->metadataFactory
×
UNCOV
215
                ->create($data)
×
UNCOV
216
                ->shouldBeCalled()
×
UNCOV
217
                ->willReturn(
×
UNCOV
218
                    new ResourceMetadataCollection($data, [(new ApiResource(operations: [new Get(filters: ['foo', 'a_filter'], uriVariables: ['id' => new Link(parameterName: 'id')])]))->withFilters(['foo', 'a_filter'])])
×
UNCOV
219
                );
×
220
        }
221
    }
222
}
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