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

api-platform / core / 10315659289

09 Aug 2024 07:49AM UTC coverage: 7.841% (-0.006%) from 7.847%
10315659289

push

github

soyuka
style: cs fixes

70 of 529 new or added lines in 176 files covered. (13.23%)

160 existing lines in 58 files now uncovered.

12688 of 161818 relevant lines covered (7.84%)

26.86 hits per line

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

0.0
/src/Hydra/Tests/Serializer/PartialCollectionViewNormalizerTest.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\Hydra\Tests\Serializer;
15

16
use ApiPlatform\Hydra\Serializer\PartialCollectionViewNormalizer;
17
use ApiPlatform\Hydra\Tests\Fixtures\Entity\SoMany;
18
use ApiPlatform\Metadata\ApiResource;
19
use ApiPlatform\Metadata\GetCollection;
20
use ApiPlatform\Metadata\Operations;
21
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
22
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
23
use ApiPlatform\State\Pagination\PaginatorInterface;
24
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
25
use PHPUnit\Framework\TestCase;
26
use Prophecy\Argument;
27
use Prophecy\PhpUnit\ProphecyTrait;
28
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
29
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
30
use Symfony\Component\Serializer\Serializer;
31

32
/**
33
 * @author Kévin Dunglas <dunglas@gmail.com>
34
 */
35
class PartialCollectionViewNormalizerTest extends TestCase
36
{
37
    use ProphecyTrait;
38

39
    public function testNormalizeDoesNotChangeSubLevel(): void
40
    {
41
        $decoratedNormalizerProphecy = $this->prophesize(NormalizerInterface::class);
×
42
        $decoratedNormalizerProphecy->normalize(Argument::any(), null, ['jsonld_sub_level' => true])->willReturn(['foo' => 'bar'])->shouldBeCalled();
×
43
        $resourceMetadataFactory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
44

45
        $normalizer = new PartialCollectionViewNormalizer($decoratedNormalizerProphecy->reveal(), 'page', 'pagination', $resourceMetadataFactory->reveal());
×
46
        $this->assertEquals(['foo' => 'bar'], $normalizer->normalize(new \stdClass(), null, ['jsonld_sub_level' => true]));
×
47
    }
48

49
    public function testNormalizeDoesNotChangeWhenNoFilterNorPagination(): void
50
    {
51
        $decoratedNormalizerProphecy = $this->prophesize(NormalizerInterface::class);
×
52
        $decoratedNormalizerProphecy->normalize(Argument::any(), null, Argument::type('array'))->willReturn(['foo' => 'bar'])->shouldBeCalled();
×
53
        $resourceMetadataFactory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
54

55
        $normalizer = new PartialCollectionViewNormalizer($decoratedNormalizerProphecy->reveal(), 'page', 'pagination', $resourceMetadataFactory->reveal());
×
56
        $this->assertEquals(['foo' => 'bar'], $normalizer->normalize(new \stdClass(), null, ['request_uri' => '/?page=1&pagination=1']));
×
57
    }
58

59
    public function testNormalizePaginator(): void
60
    {
61
        $this->assertEquals(
×
62
            [
×
63
                'hydra:totalItems' => 40,
×
64
                'foo' => 'bar',
×
65
                'hydra:view' => [
×
66
                    '@id' => '/?_page=3',
×
67
                    '@type' => 'hydra:PartialCollectionView',
×
68
                    'hydra:first' => '/?_page=1',
×
69
                    'hydra:last' => '/?_page=20',
×
70
                    'hydra:previous' => '/?_page=2',
×
71
                    'hydra:next' => '/?_page=4',
×
72
                ],
×
73
            ],
×
74
            $this->normalizePaginator()
×
75
        );
×
76
    }
77

78
    public function testNormalizePartialPaginator(): void
79
    {
80
        $this->assertEquals(
×
81
            [
×
82
                'foo' => 'bar',
×
83
                'hydra:view' => [
×
84
                    '@id' => '/?_page=3',
×
85
                    '@type' => 'hydra:PartialCollectionView',
×
86
                    'hydra:previous' => '/?_page=2',
×
87
                    'hydra:next' => '/?_page=4',
×
88
                ],
×
89
            ],
×
90
            $this->normalizePaginator(true)
×
91
        );
×
92
    }
93

94
    public function testNormalizeWithCursorBasedPagination(): void
95
    {
96
        self::assertEquals(
×
97
            [
×
98
                'foo' => 'bar',
×
99
                'hydra:totalItems' => 40,
×
100
                'hydra:view' => [
×
101
                    '@id' => '/',
×
102
                    '@type' => 'hydra:PartialCollectionView',
×
103
                    'hydra:previous' => '/?id%5Bgt%5D=1',
×
104
                    'hydra:next' => '/?id%5Blt%5D=2',
×
105
                ],
×
106
            ],
×
107
            $this->normalizePaginator(false, true)
×
108
        );
×
109
    }
110

111
    private function normalizePaginator(bool $partial = false, bool $cursor = false)
112
    {
113
        $paginatorProphecy = $this->prophesize($partial ? PartialPaginatorInterface::class : PaginatorInterface::class);
×
114
        $paginatorProphecy->getCurrentPage()->willReturn(3)->shouldBeCalled();
×
115

116
        $decoratedNormalize = ['foo' => 'bar'];
×
117

118
        if ($partial) {
×
119
            $paginatorProphecy->getItemsPerPage()->willReturn(42)->shouldBeCalled();
×
120
            $paginatorProphecy->count()->willReturn(42)->shouldBeCalled();
×
121
        } else {
122
            $decoratedNormalize['hydra:totalItems'] = 40;
×
123
            $paginatorProphecy->getLastPage()->willReturn(20)->shouldBeCalled();
×
124
        }
125

126
        $decoratedNormalizerProphecy = $this->prophesize(NormalizerInterface::class);
×
127
        $decoratedNormalizerProphecy->normalize(Argument::type($partial ? PartialPaginatorInterface::class : PaginatorInterface::class), null, Argument::type('array'))->willReturn($decoratedNormalize)->shouldBeCalled();
×
128

129
        $resourceMetadataFactoryProphecy = null;
×
130

131
        if ($cursor) {
×
132
            $firstSoMany = new SoMany();
×
133
            $firstSoMany->id = 1;
×
134
            $firstSoMany->content = 'SoMany #1';
×
135

136
            $lastSoMany = new SoMany();
×
137
            $lastSoMany->id = 2;
×
138
            $lastSoMany->content = 'SoMany #2';
×
139

140
            $paginatorProphecy->rewind()->shouldBeCalledOnce();
×
141
            $paginatorProphecy->valid()->willReturn(true, true, false)->shouldBeCalledTimes(3);
×
142
            $paginatorProphecy->key()->willReturn(1, 2)->shouldBeCalledTimes(2);
×
143
            $paginatorProphecy->current()->willReturn($firstSoMany, $lastSoMany)->shouldBeCalledTimes(2);
×
144
            $paginatorProphecy->next()->shouldBeCalledTimes(2);
×
145

146
            $soManyMetadata = new ResourceMetadataCollection(SoMany::class, [
×
147
                (new ApiResource())->withShortName('SoMany')->withOperations(new Operations([
×
148
                    'get' => (new GetCollection())->withPaginationViaCursor([['field' => 'id', 'direction' => 'desc']]),
×
149
                ])),
×
150
            ]);
×
151

152
            $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
153
            $resourceMetadataFactoryProphecy->create(SoMany::class)->willReturn($soManyMetadata)->shouldBeCalledOnce();
×
154
        }
155

156
        $normalizer = new PartialCollectionViewNormalizer($decoratedNormalizerProphecy->reveal(), '_page', 'pagination', $resourceMetadataFactoryProphecy ? $resourceMetadataFactoryProphecy->reveal() : null);
×
157

158
        return $normalizer->normalize($paginatorProphecy->reveal(), null, ['resource_class' => SoMany::class, 'operation_name' => 'get']);
×
159
    }
160

161
    public function testSupportsNormalization(): void
162
    {
163
        $decoratedNormalizerProphecy = $this->prophesize(NormalizerInterface::class);
×
164
        $decoratedNormalizerProphecy->supportsNormalization(Argument::any(), null, Argument::type('array'))->willReturn(true)->shouldBeCalled();
×
165

166
        $resourceMetadataFactory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
167

168
        $normalizer = new PartialCollectionViewNormalizer($decoratedNormalizerProphecy->reveal(), 'page', 'pagination', $resourceMetadataFactory->reveal());
×
169
        $this->assertTrue($normalizer->supportsNormalization(new \stdClass()));
×
170
    }
171

172
    public function testSetNormalizer(): void
173
    {
174
        $injectedNormalizer = $this->prophesize(NormalizerInterface::class)->reveal();
×
175

176
        $decoratedNormalizerProphecy = $this->prophesize(NormalizerInterface::class);
×
177
        $decoratedNormalizerProphecy->willImplement(NormalizerAwareInterface::class);
×
178
        $decoratedNormalizerProphecy->setNormalizer(Argument::type(NormalizerInterface::class))->shouldBeCalled();
×
179
        $resourceMetadataFactory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
180

181
        $normalizer = new PartialCollectionViewNormalizer($decoratedNormalizerProphecy->reveal(), 'page', 'pagination', $resourceMetadataFactory->reveal());
×
182
        $normalizer->setNormalizer($injectedNormalizer);
×
183
    }
184

185
    public function testGetSupportedTypes(): void
186
    {
187
        if (!method_exists(Serializer::class, 'getSupportedTypes')) {
×
188
            $this->markTestSkipped('Symfony Serializer < 6.3');
×
189
        }
190

191
        // TODO: use prophecy when getSupportedTypes() will be added to the interface
NEW
192
        $normalizer = new PartialCollectionViewNormalizer(new class implements NormalizerInterface {
×
193
            public function normalize(mixed $object, ?string $format = null, array $context = []): \ArrayObject|array|string|int|float|bool|null
194
            {
195
                return null;
×
196
            }
197

198
            public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
199
            {
200
                return true;
×
201
            }
202

203
            public function getSupportedTypes(?string $format): array
204
            {
205
                return ['*' => true];
×
206
            }
207
        });
×
208

209
        $this->assertSame(['*' => true], $normalizer->getSupportedTypes('jsonld'));
×
210
    }
211
}
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