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

api-platform / core / 7047458607

30 Nov 2023 01:53PM UTC coverage: 37.263% (+0.002%) from 37.261%
7047458607

push

github

soyuka
Merge 3.2

20 of 35 new or added lines in 12 files covered. (57.14%)

10 existing lines in 3 files now uncovered.

10295 of 27628 relevant lines covered (37.26%)

21.04 hits per line

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

0.0
/src/GraphQl/Tests/Resolver/Stage/SerializeStageTest.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\GraphQl\Tests\Resolver\Stage;
15

16
use ApiPlatform\GraphQl\Resolver\Stage\SerializeStage;
17
use ApiPlatform\GraphQl\Serializer\ItemNormalizer;
18
use ApiPlatform\GraphQl\Serializer\SerializerContextBuilderInterface;
19
use ApiPlatform\Metadata\GraphQl\Mutation;
20
use ApiPlatform\Metadata\GraphQl\Operation;
21
use ApiPlatform\Metadata\GraphQl\Query;
22
use ApiPlatform\Metadata\GraphQl\QueryCollection;
23
use ApiPlatform\Metadata\GraphQl\Subscription;
24
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
25
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
26
use ApiPlatform\State\Pagination\ArrayPaginator;
27
use ApiPlatform\State\Pagination\Pagination;
28
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
29
use GraphQL\Type\Definition\ResolveInfo;
30
use PHPUnit\Framework\TestCase;
31
use Prophecy\Argument;
32
use Prophecy\PhpUnit\ProphecyTrait;
33
use Prophecy\Prophecy\ObjectProphecy;
34
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
35

36
/**
37
 * @author Alan Poulain <contact@alanpoulain.eu>
38
 */
39
class SerializeStageTest extends TestCase
40
{
41
    use ProphecyTrait;
42

43
    private ObjectProphecy $normalizerProphecy;
44
    private ObjectProphecy $serializerContextBuilderProphecy;
45

46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function setUp(): void
50
    {
51
        $this->normalizerProphecy = $this->prophesize(NormalizerInterface::class);
×
52
        $this->serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
×
53
    }
54

55
    /**
56
     * @dataProvider applyDisabledProvider
57
     */
58
    public function testApplyDisabled(Operation $operation, bool $paginationEnabled, ?array $expectedResult): void
59
    {
60
        $resourceClass = 'myResource';
×
61
        /** @var Operation $operation */
62
        $operation = $operation->withSerialize(false);
×
63

64
        $result = ($this->createSerializeStage($paginationEnabled))(null, $resourceClass, $operation, []);
×
65

66
        $this->assertSame($expectedResult, $result);
×
67
    }
68

69
    public static function applyDisabledProvider(): array
70
    {
71
        return [
×
72
            'item' => [new Query(), false, null],
×
73
            'collection with pagination' => [new QueryCollection(), true, ['totalCount' => 0., 'edges' => [], 'pageInfo' => ['startCursor' => null, 'endCursor' => null, 'hasNextPage' => false, 'hasPreviousPage' => false]]],
×
74
            'collection without pagination' => [new QueryCollection(), false, []],
×
75
            'mutation' => [new Mutation(), false, ['clientMutationId' => null]],
×
76
            'subscription' => [new Subscription(), false, ['clientSubscriptionId' => null]],
×
77
        ];
×
78
    }
79

80
    /**
81
     * @dataProvider applyProvider
82
     */
83
    public function testApply(object|array $itemOrCollection, string $operationName, callable $contextFactory, bool $paginationEnabled, ?array $expectedResult): void
84
    {
85
        $context = $contextFactory($this);
×
86

87
        $resourceClass = 'myResource';
×
88
        $operation = $context['is_mutation'] ? new Mutation() : new Query();
×
89
        if ($context['is_subscription']) {
×
90
            $operation = new Subscription();
×
91
        }
92

93
        if ($context['is_collection'] ?? false) {
×
94
            $operation = new QueryCollection();
×
95
        }
96

97
        /** @var Operation $operation */
98
        $operation = $operation->withShortName('shortName')->withName($operationName)->withClass($resourceClass);
×
99

100
        $normalizationContext = ['normalization' => true];
×
101
        $this->serializerContextBuilderProphecy->create($resourceClass, $operation, $context, true)->shouldBeCalled()->willReturn($normalizationContext);
×
102

103
        $this->normalizerProphecy->normalize(Argument::type(\stdClass::class), ItemNormalizer::FORMAT, $normalizationContext)->willReturn(['normalized_item']);
×
104

105
        $result = ($this->createSerializeStage($paginationEnabled))($itemOrCollection, $resourceClass, $operation, $context);
×
106

107
        $this->assertSame($expectedResult, $result);
×
108
    }
109

110
    public static function applyProvider(): iterable
111
    {
112
        $defaultContextFactory = fn (self $that): array => [
×
113
            'args' => [],
×
114
            'info' => $that->prophesize(ResolveInfo::class)->reveal(),
×
115
        ];
×
116

117
        yield 'item' => [new \stdClass(), 'item_query', fn (self $that): array => $defaultContextFactory($that) + ['is_collection' => false, 'is_mutation' => false, 'is_subscription' => false], false, ['normalized_item']];
×
118
        yield 'collection without pagination' => [[new \stdClass(), new \stdClass()], 'collection_query', fn (self $that): array => $defaultContextFactory($that) + ['is_collection' => true, 'is_mutation' => false, 'is_subscription' => false], false, [['normalized_item'], ['normalized_item']]];
×
119
        yield 'mutation' => [new \stdClass(), 'create', fn (self $that): array => array_merge($defaultContextFactory($that), ['args' => ['input' => ['clientMutationId' => 'clientMutationId']], 'is_collection' => false, 'is_mutation' => true, 'is_subscription' => false]), false, ['shortName' => ['normalized_item'], 'clientMutationId' => 'clientMutationId']];
×
120
        yield 'delete mutation' => [new \stdClass(), 'delete', fn (self $that): array => array_merge($defaultContextFactory($that), ['args' => ['input' => ['id' => '/iri/4']], 'is_collection' => false, 'is_mutation' => true, 'is_subscription' => false]), false, ['shortName' => ['id' => '/iri/4'], 'clientMutationId' => null]];
×
121
        yield 'subscription' => [new \stdClass(), 'update', fn (self $that): array => array_merge($defaultContextFactory($that), ['args' => ['input' => ['clientSubscriptionId' => 'clientSubscriptionId']], 'is_collection' => false, 'is_mutation' => false, 'is_subscription' => true]), false, ['shortName' => ['normalized_item'], 'clientSubscriptionId' => 'clientSubscriptionId']];
×
122
    }
123

124
    /**
125
     * @dataProvider applyCollectionWithPaginationProvider
126
     */
127
    public function testApplyCollectionWithPagination(iterable|callable $collection, array $args, ?array $expectedResult, string $expectedExceptionClass = null, string $expectedExceptionMessage = null): void
128
    {
129
        $operationName = 'collection_query';
×
130
        $resourceClass = 'myResource';
×
131
        $context = [
×
132
            'is_collection' => true,
×
133
            'is_mutation' => false,
×
134
            'is_subscription' => false,
×
135
            'args' => $args,
×
136
            'info' => self::createMock(ResolveInfo::class),
×
137
        ];
×
138

139
        /** @var Operation $operation */
140
        $operation = (new QueryCollection())->withShortName('shortName')->withName($operationName);
×
141

142
        $normalizationContext = ['normalization' => true];
×
143
        $this->serializerContextBuilderProphecy->create($resourceClass, $operation, $context, true)->shouldBeCalled()->willReturn($normalizationContext);
×
144

145
        $this->normalizerProphecy->normalize(Argument::type(\stdClass::class), ItemNormalizer::FORMAT, $normalizationContext)->willReturn(['normalized_item']);
×
146

147
        if ($expectedExceptionClass) {
×
148
            $this->expectException($expectedExceptionClass);
×
149
            $this->expectExceptionMessage($expectedExceptionMessage);
×
150
        }
151

152
        $result = ($this->createSerializeStage(true))(\is_callable($collection) ? $collection($this) : $collection, $resourceClass, $operation, $context);
×
153

154
        $this->assertSame($expectedResult, $result);
×
155
    }
156

157
    public static function applyCollectionWithPaginationProvider(): iterable
158
    {
159
        $partialPaginatorFactory = function (self $that): PartialPaginatorInterface {
×
160
            $partialPaginatorProphecy = $that->prophesize(PartialPaginatorInterface::class);
×
161
            $partialPaginatorProphecy->count()->willReturn(2);
×
162
            $partialPaginatorProphecy->valid()->willReturn(false);
×
163
            $partialPaginatorProphecy->rewind();
×
164

165
            return $partialPaginatorProphecy->reveal();
×
166
        };
×
167

168
        yield 'not paginator' => [[], [], null, \LogicException::class, 'Collection returned by the collection data provider must implement ApiPlatform\State\Pagination\PaginatorInterface'];
×
169
        yield 'empty paginator' => [new ArrayPaginator([], 0, 0), [], ['totalCount' => 0., 'edges' => [], 'pageInfo' => ['startCursor' => null, 'endCursor' => null, 'hasNextPage' => false, 'hasPreviousPage' => false]]];
×
170
        yield 'paginator' => [new ArrayPaginator([new \stdClass(), new \stdClass(), new \stdClass()], 0, 2), [], ['totalCount' => 3., 'edges' => [['node' => ['normalized_item'], 'cursor' => 'MA=='], ['node' => ['normalized_item'], 'cursor' => 'MQ==']], 'pageInfo' => ['startCursor' => 'MA==', 'endCursor' => 'MQ==', 'hasNextPage' => true, 'hasPreviousPage' => false]]];
×
171
        yield 'paginator with after cursor' => [new ArrayPaginator([new \stdClass(), new \stdClass(), new \stdClass()], 1, 2), ['after' => 'MA=='], ['totalCount' => 3., 'edges' => [['node' => ['normalized_item'], 'cursor' => 'MQ=='], ['node' => ['normalized_item'], 'cursor' => 'Mg==']], 'pageInfo' => ['startCursor' => 'MQ==', 'endCursor' => 'Mg==', 'hasNextPage' => false, 'hasPreviousPage' => true]]];
×
172
        yield 'paginator with bad after cursor' => [new ArrayPaginator([], 0, 0), ['after' => '-'], null, \UnexpectedValueException::class, 'Cursor - is invalid'];
×
173
        yield 'paginator with empty after cursor' => [new ArrayPaginator([], 0, 0), ['after' => ''], null, \UnexpectedValueException::class, 'Empty cursor is invalid'];
×
174
        yield 'paginator with before cursor' => [new ArrayPaginator([new \stdClass(), new \stdClass(), new \stdClass()], 1, 1), ['before' => 'Mg=='], ['totalCount' => 3., 'edges' => [['node' => ['normalized_item'], 'cursor' => 'MQ==']], 'pageInfo' => ['startCursor' => 'MQ==', 'endCursor' => 'MQ==', 'hasNextPage' => true, 'hasPreviousPage' => true]]];
×
175
        yield 'paginator with bad before cursor' => [new ArrayPaginator([], 0, 0), ['before' => '-'], null, \UnexpectedValueException::class, 'Cursor - is invalid'];
×
176
        yield 'paginator with empty before cursor' => [new ArrayPaginator([], 0, 0), ['before' => ''], null, \UnexpectedValueException::class, 'Empty cursor is invalid'];
×
177
        yield 'paginator with last' => [new ArrayPaginator([new \stdClass(), new \stdClass(), new \stdClass()], 1, 2), ['last' => 2], ['totalCount' => 3., 'edges' => [['node' => ['normalized_item'], 'cursor' => 'MQ=='], ['node' => ['normalized_item'], 'cursor' => 'Mg==']], 'pageInfo' => ['startCursor' => 'MQ==', 'endCursor' => 'Mg==', 'hasNextPage' => false, 'hasPreviousPage' => true]]];
×
178
        yield 'partial paginator' => [$partialPaginatorFactory, [], ['totalCount' => 0., 'edges' => [], 'pageInfo' => ['startCursor' => 'MA==', 'endCursor' => 'MQ==', 'hasNextPage' => false, 'hasPreviousPage' => false]]];
×
179
        yield 'partial paginator with after cursor' => [$partialPaginatorFactory, ['after' => 'MA=='], ['totalCount' => 0., 'edges' => [], 'pageInfo' => ['startCursor' => 'MQ==', 'endCursor' => 'Mg==', 'hasNextPage' => false, 'hasPreviousPage' => true]]];
×
180
    }
181

182
    public function testApplyBadNormalizedData(): void
183
    {
184
        $operationName = 'item_query';
×
185
        $resourceClass = 'myResource';
×
186
        $context = ['is_collection' => false, 'is_mutation' => false, 'is_subscription' => false, 'args' => [], 'info' => $this->prophesize(ResolveInfo::class)->reveal()];
×
187
        /** @var Operation $operation */
188
        $operation = (new Query())->withName($operationName);
×
189

190
        $normalizationContext = ['normalization' => true];
×
191
        $this->serializerContextBuilderProphecy->create($resourceClass, $operation, $context, true)->shouldBeCalled()->willReturn($normalizationContext);
×
192

NEW
193
        $this->normalizerProphecy->normalize(Argument::type(\stdClass::class), ItemNormalizer::FORMAT, $normalizationContext)->willReturn(0);
×
194

195
        $this->expectException(\UnexpectedValueException::class);
×
196
        $this->expectExceptionMessage('Expected serialized data to be a nullable array.');
×
197

198
        ($this->createSerializeStage(false))(new \stdClass(), $resourceClass, $operation, $context);
×
199
    }
200

201
    private function createSerializeStage(bool $paginationEnabled): SerializeStage
202
    {
203
        $resourceMetadataCollectionFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
204
        $resourceMetadataCollectionFactoryProphecy->create(Argument::type('string'))->willReturn(new ResourceMetadataCollection(''));
×
205
        $pagination = new Pagination([], ['enabled' => $paginationEnabled]);
×
206

207
        return new SerializeStage(
×
208
            $this->normalizerProphecy->reveal(),
×
209
            $this->serializerContextBuilderProphecy->reveal(),
×
210
            $pagination
×
211
        );
×
212
    }
213
}
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