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

api-platform / core / 14980015570

12 May 2025 06:41PM UTC coverage: 26.309% (+2.6%) from 23.685%
14980015570

Pull #7140

github

web-flow
Merge 1e6b14143 into 202c60fcb
Pull Request #7140: Fix: PHPize HTTP cache headers

0 of 1 new or added line in 1 file covered. (0.0%)

4614 existing lines in 185 files now uncovered.

13550 of 51504 relevant lines covered (26.31%)

71.73 hits per line

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

0.0
/src/GraphQl/Tests/Type/TypeConverterTest.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\Type;
15

16
use ApiPlatform\GraphQl\Tests\Fixtures\Enum\GenderTypeEnum;
17
use ApiPlatform\GraphQl\Tests\Fixtures\Type\Definition\DateTimeType;
18
use ApiPlatform\GraphQl\Type\ContextAwareTypeBuilderInterface;
19
use ApiPlatform\GraphQl\Type\TypeConverter;
20
use ApiPlatform\GraphQl\Type\TypesContainerInterface;
21
use ApiPlatform\Metadata\ApiProperty;
22
use ApiPlatform\Metadata\ApiResource;
23
use ApiPlatform\Metadata\Exception\ResourceClassNotFoundException;
24
use ApiPlatform\Metadata\GraphQl\Operation;
25
use ApiPlatform\Metadata\GraphQl\Query;
26
use ApiPlatform\Metadata\GraphQl\QueryCollection;
27
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
28
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
29
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
30
use GraphQL\Type\Definition\EnumType;
31
use GraphQL\Type\Definition\ObjectType;
32
use GraphQL\Type\Definition\Type as GraphQLType;
33
use PHPUnit\Framework\Attributes\DataProvider;
34
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
35
use PHPUnit\Framework\TestCase;
36
use Prophecy\Argument;
37
use Prophecy\PhpUnit\ProphecyTrait;
38
use Prophecy\Prophecy\ObjectProphecy;
39
use Symfony\Component\PropertyInfo\Type as LegacyType;
40
use Symfony\Component\TypeInfo\Type;
41

42
/**
43
 * @author Alan Poulain <contact@alanpoulain.eu>
44
 */
45
class TypeConverterTest extends TestCase
46
{
47
    use ProphecyTrait;
48

49
    private ObjectProphecy $typeBuilderProphecy;
50
    private ObjectProphecy $typesContainerProphecy;
51
    private ObjectProphecy $resourceMetadataCollectionFactoryProphecy;
52
    private ObjectProphecy $propertyMetadataFactoryProphecy;
53
    private TypeConverter $typeConverter;
54

55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function setUp(): void
59
    {
60
        $this->typeBuilderProphecy = $this->prophesize(ContextAwareTypeBuilderInterface::class);
×
61
        $this->typesContainerProphecy = $this->prophesize(TypesContainerInterface::class);
×
UNCOV
62
        $this->resourceMetadataCollectionFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
UNCOV
63
        $this->propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
UNCOV
64
        $this->typeConverter = new TypeConverter($this->typeBuilderProphecy->reveal(), $this->typesContainerProphecy->reveal(), $this->resourceMetadataCollectionFactoryProphecy->reveal(), $this->propertyMetadataFactoryProphecy->reveal());
×
65
    }
66

67
    #[DataProvider('legacyConvertTypeProvider')]
68
    #[IgnoreDeprecations]
69
    public function testConvertTypeLegacy(LegacyType $type, bool $input, int $depth, GraphQLType|string|null $expectedGraphqlType): void
70
    {
UNCOV
71
        $this->expectUserDeprecationMessage('Since api-platform/graphql 4.2: The "ApiPlatform\GraphQl\Type\TypeConverter::convertType()" method is deprecated, use "ApiPlatform\GraphQl\Type\TypeConverter::convertPhpType()" instead.');
×
72

73
        $this->typeBuilderProphecy->isCollection($type)->willReturn(false);
×
74
        $this->resourceMetadataCollectionFactoryProphecy->create(Argument::type('string'))->willReturn(new ResourceMetadataCollection('resourceClass'));
×
UNCOV
75
        $this->typeBuilderProphecy->getEnumType(Argument::type(Operation::class))->willReturn($expectedGraphqlType);
×
76

77
        /** @var Operation $operation */
UNCOV
78
        $operation = (new Query())->withName('test');
×
79
        $graphqlType = $this->typeConverter->convertType($type, $input, $operation, 'resourceClass', 'rootClass', null, $depth);
×
80
        $this->assertSame($expectedGraphqlType, $graphqlType);
×
81
    }
82

83
    public static function legacyConvertTypeProvider(): array
84
    {
85
        return [
×
86
            [new LegacyType(LegacyType::BUILTIN_TYPE_BOOL), false, 0, GraphQLType::boolean()],
×
87
            [new LegacyType(LegacyType::BUILTIN_TYPE_INT), false, 0, GraphQLType::int()],
×
88
            [new LegacyType(LegacyType::BUILTIN_TYPE_FLOAT), false, 0, GraphQLType::float()],
×
89
            [new LegacyType(LegacyType::BUILTIN_TYPE_STRING), false, 0, GraphQLType::string()],
×
90
            [new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY), false, 0, 'Iterable'],
×
91
            [new LegacyType(LegacyType::BUILTIN_TYPE_ITERABLE), false, 0, 'Iterable'],
×
92
            [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, \DateTimeInterface::class), false, 0, GraphQLType::string()],
×
UNCOV
93
            [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, GenderTypeEnum::class), false, 0, new EnumType(['name' => 'GenderTypeEnum', 'values' => []])],
×
UNCOV
94
            [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT), false, 0, null],
×
UNCOV
95
            [new LegacyType(LegacyType::BUILTIN_TYPE_CALLABLE), false, 0, null],
×
UNCOV
96
            [new LegacyType(LegacyType::BUILTIN_TYPE_NULL), false, 0, null],
×
97
            [new LegacyType(LegacyType::BUILTIN_TYPE_RESOURCE), false, 0, null],
×
UNCOV
98
        ];
×
99
    }
100

101
    #[DataProvider('convertTypeProvider')]
102
    public function testConvertType(Type $type, bool $input, int $depth, GraphQLType|string|null $expectedGraphqlType): void
103
    {
104
        $this->resourceMetadataCollectionFactoryProphecy->create(Argument::type('string'))->willReturn(new ResourceMetadataCollection('resourceClass'));
×
105
        $this->typeBuilderProphecy->getEnumType(Argument::type(Operation::class))->willReturn($expectedGraphqlType);
×
106

107
        /** @var Operation $operation */
UNCOV
108
        $operation = (new Query())->withName('test');
×
UNCOV
109
        $graphqlType = $this->typeConverter->convertPhpType($type, $input, $operation, 'resourceClass', 'rootClass', null, $depth);
×
110
        $this->assertSame($expectedGraphqlType, $graphqlType);
×
111
    }
112

113
    public static function convertTypeProvider(): array
114
    {
115
        return [
×
116
            [Type::bool(), false, 0, GraphQLType::boolean()],
×
UNCOV
117
            [Type::int(), false, 0, GraphQLType::int()],
×
UNCOV
118
            [Type::float(), false, 0, GraphQLType::float()],
×
119
            [Type::string(), false, 0, GraphQLType::string()],
×
120
            [Type::array(), false, 0, 'Iterable'],
×
UNCOV
121
            [Type::iterable(), false, 0, 'Iterable'],
×
UNCOV
122
            [Type::object(\DateTimeInterface::class), false, 0, GraphQLType::string()],
×
UNCOV
123
            [Type::object(GenderTypeEnum::class), false, 0, new EnumType(['name' => 'GenderTypeEnum', 'values' => []])],
×
UNCOV
124
            [Type::object(), false, 0, null],
×
125
            [Type::callable(), false, 0, null],
×
UNCOV
126
            [Type::null(), false, 0, null],
×
127
            [Type::resource(), false, 0, null],
×
128
        ];
×
129
    }
130

131
    #[IgnoreDeprecations]
132
    public function testConvertTypeNoGraphQlResourceMetadataLegacy(): void
133
    {
UNCOV
134
        $type = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, 'dummy');
×
135

UNCOV
136
        $this->typeBuilderProphecy->isCollection($type)->shouldBeCalled()->willReturn(false);
×
UNCOV
137
        $this->resourceMetadataCollectionFactoryProphecy->create('dummy')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('dummy', [new ApiResource()]));
×
138

139
        /** @var Operation $operation */
140
        $operation = (new Query())->withName('test');
×
141
        $graphqlType = $this->typeConverter->convertType($type, false, $operation, 'resourceClass', 'rootClass', null, 0);
×
142
        $this->assertNull($graphqlType);
×
143
    }
144

145
    public function testConvertTypeNoGraphQlResourceMetadata(): void
146
    {
147
        $type = Type::object('dummy');
×
148

UNCOV
149
        $this->resourceMetadataCollectionFactoryProphecy->create('dummy')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('dummy', [new ApiResource()]));
×
150

151
        /** @var Operation $operation */
UNCOV
152
        $operation = (new Query())->withName('test');
×
153
        $graphqlType = $this->typeConverter->convertPhpType($type, false, $operation, 'resourceClass', 'rootClass', null, 0);
×
UNCOV
154
        $this->assertNull($graphqlType);
×
155
    }
156

157
    #[IgnoreDeprecations]
158
    public function testConvertTypeNodeResourceLegacy(): void
159
    {
UNCOV
160
        $type = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, 'node');
×
161

162
        $this->typeBuilderProphecy->isCollection($type)->shouldBeCalled()->willReturn(false);
×
163
        $this->resourceMetadataCollectionFactoryProphecy->create('node')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('node', [(new ApiResource())->withShortName('Node')->withGraphQlOperations(['test' => new Query()])]));
×
164

UNCOV
165
        $this->expectException(\UnexpectedValueException::class);
×
166
        $this->expectExceptionMessage('A "Node" resource cannot be used with GraphQL because the type is already used by the Relay specification.');
×
167

168
        /** @var Operation $operation */
UNCOV
169
        $operation = (new Query())->withName('test');
×
UNCOV
170
        $this->typeConverter->convertType($type, false, $operation, 'resourceClass', 'rootClass', null, 0);
×
171
    }
172

173
    public function testConvertTypeNodeResource(): void
174
    {
175
        $type = Type::object('node');
×
176

UNCOV
177
        $this->resourceMetadataCollectionFactoryProphecy->create('node')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('node', [(new ApiResource())->withShortName('Node')->withGraphQlOperations(['test' => new Query()])]));
×
178

179
        $this->expectException(\UnexpectedValueException::class);
×
180
        $this->expectExceptionMessage('A "Node" resource cannot be used with GraphQL because the type is already used by the Relay specification.');
×
181

182
        /** @var Operation $operation */
183
        $operation = (new Query())->withName('test');
×
184
        $this->typeConverter->convertPhpType($type, false, $operation, 'resourceClass', 'rootClass', null, 0);
×
185
    }
186

187
    #[IgnoreDeprecations]
188
    public function testConvertTypeResourceClassNotFoundLegacy(): void
189
    {
UNCOV
190
        $type = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, 'dummy');
×
191

UNCOV
192
        $this->typeBuilderProphecy->isCollection($type)->shouldBeCalled()->willReturn(false);
×
UNCOV
193
        $this->resourceMetadataCollectionFactoryProphecy->create('dummy')->shouldBeCalled()->willThrow(new ResourceClassNotFoundException());
×
194

195
        /** @var Operation $operation */
196
        $operation = (new Query())->withName('test');
×
197
        $graphqlType = $this->typeConverter->convertType($type, false, $operation, 'resourceClass', 'rootClass', null, 0);
×
UNCOV
198
        $this->assertNull($graphqlType);
×
199
    }
200

201
    public function testConvertTypeResourceClassNotFound(): void
202
    {
203
        $type = Type::object('dummy');
×
204

205
        $this->resourceMetadataCollectionFactoryProphecy->create('dummy')->shouldBeCalled()->willThrow(new ResourceClassNotFoundException());
×
206

207
        /** @var Operation $operation */
UNCOV
208
        $operation = (new Query())->withName('test');
×
209
        $graphqlType = $this->typeConverter->convertPhpType($type, false, $operation, 'resourceClass', 'rootClass', null, 0);
×
210
        $this->assertNull($graphqlType);
×
211
    }
212

213
    #[IgnoreDeprecations]
214
    public function testConvertTypeResourceIriLegacy(): void
215
    {
UNCOV
216
        $type = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, 'dummy');
×
217

218
        $graphqlResourceMetadata = new ResourceMetadataCollection('dummy', [(new ApiResource())->withGraphQlOperations(['test' => new Query()])]);
×
UNCOV
219
        $this->resourceMetadataCollectionFactoryProphecy->create('dummy')->willReturn($graphqlResourceMetadata);
×
220
        $this->typeBuilderProphecy->isCollection($type)->willReturn(false);
×
UNCOV
221
        $this->propertyMetadataFactoryProphecy->create('rootClass', 'dummyProperty', Argument::type('array'))->shouldBeCalled()->willReturn((new ApiProperty())->withWritableLink(false));
×
222

223
        /** @var Operation $operation */
UNCOV
224
        $operation = (new Query())->withName('test');
×
225
        $graphqlType = $this->typeConverter->convertType($type, true, $operation, 'dummy', 'rootClass', 'dummyProperty', 1);
×
226
        $this->assertSame(GraphQLType::string(), $graphqlType);
×
227
    }
228

229
    public function testConvertTypeResourceIri(): void
230
    {
231
        $type = Type::object('dummy');
×
232

233
        $graphqlResourceMetadata = new ResourceMetadataCollection('dummy', [(new ApiResource())->withGraphQlOperations(['test' => new Query()])]);
×
234
        $this->resourceMetadataCollectionFactoryProphecy->create('dummy')->willReturn($graphqlResourceMetadata);
×
235
        $this->propertyMetadataFactoryProphecy->create('rootClass', 'dummyProperty', Argument::type('array'))->shouldBeCalled()->willReturn((new ApiProperty())->withWritableLink(false));
×
236

237
        /** @var Operation $operation */
UNCOV
238
        $operation = (new Query())->withName('test');
×
UNCOV
239
        $graphqlType = $this->typeConverter->convertPhpType($type, true, $operation, 'dummy', 'rootClass', 'dummyProperty', 1);
×
UNCOV
240
        $this->assertSame(GraphQLType::string(), $graphqlType);
×
241
    }
242

243
    #[IgnoreDeprecations]
244
    public function testConvertTypeInputResourceLegacy(): void
245
    {
246
        $type = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, 'dummy');
×
247
        /** @var Operation $operation */
UNCOV
248
        $operation = new Query();
×
249
        /** @var ApiProperty $propertyMetadata */
UNCOV
250
        $propertyMetadata = (new ApiProperty())->withWritableLink(true);
×
251
        $graphqlResourceMetadata = new ResourceMetadataCollection('dummy', [(new ApiResource())->withGraphQlOperations(['item_query' => $operation])]);
×
252
        $expectedGraphqlType = new ObjectType(['name' => 'resourceObjectType', 'fields' => []]);
×
253

254
        $this->resourceMetadataCollectionFactoryProphecy->create('dummy')->willReturn($graphqlResourceMetadata);
×
255
        $this->typeBuilderProphecy->isCollection($type)->willReturn(false);
×
UNCOV
256
        $this->propertyMetadataFactoryProphecy->create('rootClass', 'dummyProperty', Argument::type('array'))->shouldBeCalled()->willReturn((new ApiProperty())->withWritableLink(true));
×
UNCOV
257
        $this->typeBuilderProphecy->getResourceObjectType($graphqlResourceMetadata, $operation, $propertyMetadata, ['input' => true, 'wrapped' => false, 'depth' => 1])->shouldBeCalled()->willReturn($expectedGraphqlType);
×
258

UNCOV
259
        $graphqlType = $this->typeConverter->convertType($type, true, $operation, 'dummy', 'rootClass', 'dummyProperty', 1);
×
UNCOV
260
        $this->assertSame($expectedGraphqlType, $graphqlType);
×
261
    }
262

263
    public function testConvertTypeInputResource(): void
264
    {
UNCOV
265
        $type = Type::object('dummy');
×
266
        /** @var Operation $operation */
UNCOV
267
        $operation = new Query();
×
268
        /** @var ApiProperty $propertyMetadata */
UNCOV
269
        $propertyMetadata = (new ApiProperty())->withWritableLink(true);
×
UNCOV
270
        $graphqlResourceMetadata = new ResourceMetadataCollection('dummy', [(new ApiResource())->withGraphQlOperations(['item_query' => $operation])]);
×
UNCOV
271
        $expectedGraphqlType = new ObjectType(['name' => 'resourceObjectType', 'fields' => []]);
×
272

UNCOV
273
        $this->resourceMetadataCollectionFactoryProphecy->create('dummy')->willReturn($graphqlResourceMetadata);
×
UNCOV
274
        $this->propertyMetadataFactoryProphecy->create('rootClass', 'dummyProperty', Argument::type('array'))->shouldBeCalled()->willReturn((new ApiProperty())->withWritableLink(true));
×
UNCOV
275
        $this->typeBuilderProphecy->getResourceObjectType($graphqlResourceMetadata, $operation, $propertyMetadata, ['input' => true, 'wrapped' => false, 'depth' => 1])->shouldBeCalled()->willReturn($expectedGraphqlType);
×
276

UNCOV
277
        $graphqlType = $this->typeConverter->convertPhpType($type, true, $operation, 'dummy', 'rootClass', 'dummyProperty', 1);
×
UNCOV
278
        $this->assertSame($expectedGraphqlType, $graphqlType);
×
279
    }
280

281
    #[DataProvider('legacyConvertTypeResourceProvider')]
282
    #[IgnoreDeprecations]
283
    public function testConvertTypeCollectionResourceLegacy(LegacyType $type, ObjectType $expectedGraphqlType): void
284
    {
UNCOV
285
        $collectionOperation = new QueryCollection();
×
UNCOV
286
        $graphqlResourceMetadata = new ResourceMetadataCollection('dummyValue', [
×
UNCOV
287
            (new ApiResource())->withShortName('DummyValue')->withGraphQlOperations(['collection_query' => $collectionOperation]),
×
UNCOV
288
        ]);
×
289

UNCOV
290
        $this->typeBuilderProphecy->isCollection($type)->shouldBeCalled()->willReturn(true);
×
UNCOV
291
        $this->resourceMetadataCollectionFactoryProphecy->create('dummyValue')->shouldBeCalled()->willReturn($graphqlResourceMetadata);
×
UNCOV
292
        $this->typeBuilderProphecy->getResourceObjectType($graphqlResourceMetadata, $collectionOperation, null, [
×
UNCOV
293
            'input' => false,
×
UNCOV
294
            'wrapped' => false,
×
UNCOV
295
            'depth' => 0,
×
UNCOV
296
        ])->shouldBeCalled()->willReturn($expectedGraphqlType);
×
297

298
        /** @var Operation $rootOperation */
UNCOV
299
        $rootOperation = (new Query())->withName('test');
×
UNCOV
300
        $graphqlType = $this->typeConverter->convertType($type, false, $rootOperation, 'resourceClass', 'rootClass', null, 0);
×
UNCOV
301
        $this->assertSame($expectedGraphqlType, $graphqlType);
×
302
    }
303

304
    public static function legacyConvertTypeResourceProvider(): array
305
    {
UNCOV
306
        return [
×
UNCOV
307
            [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, null, true, null, new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, 'dummyValue')), new ObjectType(['name' => 'resourceObjectType', 'fields' => []])],
×
UNCOV
308
            [new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, false, null, true, null, new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, 'dummyValue')), new ObjectType(['name' => 'resourceObjectType', 'fields' => []])],
×
UNCOV
309
        ];
×
310
    }
311

312
    #[DataProvider('convertTypeResourceProvider')]
313
    public function testConvertTypeCollectionResource(Type $type, ObjectType $expectedGraphqlType): void
314
    {
UNCOV
315
        $collectionOperation = new QueryCollection();
×
UNCOV
316
        $graphqlResourceMetadata = new ResourceMetadataCollection('dummyValue', [
×
UNCOV
317
            (new ApiResource())->withShortName('DummyValue')->withGraphQlOperations(['collection_query' => $collectionOperation]),
×
UNCOV
318
        ]);
×
319

UNCOV
320
        $this->resourceMetadataCollectionFactoryProphecy->create('dummyValue')->shouldBeCalled()->willReturn($graphqlResourceMetadata);
×
UNCOV
321
        $this->typeBuilderProphecy->getResourceObjectType($graphqlResourceMetadata, $collectionOperation, null, [
×
UNCOV
322
            'input' => false,
×
UNCOV
323
            'wrapped' => false,
×
UNCOV
324
            'depth' => 0,
×
UNCOV
325
        ])->shouldBeCalled()->willReturn($expectedGraphqlType);
×
326

327
        /** @var Operation $rootOperation */
UNCOV
328
        $rootOperation = (new Query())->withName('test');
×
UNCOV
329
        $graphqlType = $this->typeConverter->convertPhpType($type, false, $rootOperation, 'resourceClass', 'rootClass', null, 0);
×
UNCOV
330
        $this->assertSame($expectedGraphqlType, $graphqlType);
×
331
    }
332

333
    public static function convertTypeResourceProvider(): array
334
    {
UNCOV
335
        return [
×
UNCOV
336
            [Type::collection(Type::object('dummyValue'), Type::object('dummyValue')), new ObjectType(['name' => 'resourceObjectType', 'fields' => []])], // @phpstan-ignore-line
×
UNCOV
337
            [Type::array(Type::object('dummyValue')), new ObjectType(['name' => 'resourceObjectType', 'fields' => []])],
×
UNCOV
338
        ];
×
339
    }
340

341
    #[IgnoreDeprecations]
342
    public function testConvertTypeCollectionEnumLegacy(): void
343
    {
UNCOV
344
        $type = new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, false, null, true, null, new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, GenderTypeEnum::class));
×
UNCOV
345
        $expectedGraphqlType = new EnumType(['name' => 'GenderTypeEnum', 'values' => []]);
×
UNCOV
346
        $this->typeBuilderProphecy->isCollection($type)->shouldBeCalled()->willReturn(true);
×
UNCOV
347
        $this->resourceMetadataCollectionFactoryProphecy->create(GenderTypeEnum::class)->shouldBeCalled()->willReturn(new ResourceMetadataCollection(GenderTypeEnum::class, []));
×
UNCOV
348
        $this->typeBuilderProphecy->getEnumType(Argument::type(Operation::class))->willReturn($expectedGraphqlType);
×
349

350
        /** @var Operation $rootOperation */
UNCOV
351
        $rootOperation = (new Query())->withName('test');
×
UNCOV
352
        $graphqlType = $this->typeConverter->convertType($type, false, $rootOperation, 'resourceClass', 'rootClass', null, 0);
×
UNCOV
353
        $this->assertSame($expectedGraphqlType, $graphqlType);
×
354
    }
355

356
    public function testConvertTypeCollectionEnum(): void
357
    {
UNCOV
358
        $type = Type::array(Type::object(GenderTypeEnum::class));
×
UNCOV
359
        $expectedGraphqlType = new EnumType(['name' => 'GenderTypeEnum', 'values' => []]);
×
UNCOV
360
        $this->resourceMetadataCollectionFactoryProphecy->create(GenderTypeEnum::class)->shouldBeCalled()->willReturn(new ResourceMetadataCollection(GenderTypeEnum::class, []));
×
UNCOV
361
        $this->typeBuilderProphecy->getEnumType(Argument::type(Operation::class))->willReturn($expectedGraphqlType);
×
362

363
        /** @var Operation $rootOperation */
UNCOV
364
        $rootOperation = (new Query())->withName('test');
×
UNCOV
365
        $graphqlType = $this->typeConverter->convertPhpType($type, false, $rootOperation, 'resourceClass', 'rootClass', null, 0);
×
UNCOV
366
        $this->assertSame($expectedGraphqlType, $graphqlType);
×
367
    }
368

369
    #[DataProvider('resolveTypeProvider')]
370
    public function testResolveType(string $type, string|GraphQLType $expectedGraphqlType): void
371
    {
UNCOV
372
        $this->typesContainerProphecy->has(\DateTime::class)->willReturn(true);
×
UNCOV
373
        $this->typesContainerProphecy->get(\DateTime::class)->willReturn(new DateTimeType());
×
374

UNCOV
375
        $this->assertEquals($expectedGraphqlType, $this->typeConverter->resolveType($type));
×
376
    }
377

378
    public static function resolveTypeProvider(): array
379
    {
UNCOV
380
        return [
×
UNCOV
381
            ['String', GraphQLType::string()],
×
UNCOV
382
            ['String!', GraphQLType::nonNull(GraphQLType::string())],
×
UNCOV
383
            ['Boolean', GraphQLType::boolean()],
×
UNCOV
384
            ['[Boolean]', GraphQLType::listOf(GraphQLType::boolean())],
×
UNCOV
385
            ['Int!', GraphQLType::nonNull(GraphQLType::int())],
×
UNCOV
386
            ['[Int!]', GraphQLType::listOf(GraphQLType::nonNull(GraphQLType::int()))],
×
UNCOV
387
            ['Float', GraphQLType::float()],
×
UNCOV
388
            ['[Float]!', GraphQLType::nonNull(GraphQLType::listOf(GraphQLType::float()))],
×
UNCOV
389
            [\DateTime::class, new DateTimeType()],
×
UNCOV
390
            ['[DateTime!]!', GraphQLType::nonNull(GraphQLType::listOf(GraphQLType::nonNull(new DateTimeType())))],
×
UNCOV
391
        ];
×
392
    }
393

394
    #[DataProvider('resolveTypeInvalidProvider')]
395
    public function testResolveTypeInvalid(string $type, string $expectedExceptionMessage): void
396
    {
UNCOV
397
        $this->typesContainerProphecy->has('UnknownType')->willReturn(false);
×
398

UNCOV
399
        $this->expectExceptionMessage($expectedExceptionMessage);
×
400

UNCOV
401
        $this->typeConverter->resolveType($type);
×
402
    }
403

404
    public static function resolveTypeInvalidProvider(): array
405
    {
UNCOV
406
        return [
×
UNCOV
407
            ['float?', '"float?" is not a valid GraphQL type.'],
×
UNCOV
408
            ['UnknownType', 'The type "UnknownType" was not resolved.'],
×
UNCOV
409
            ['UnknownType!', 'The type "UnknownType!" was not resolved.'],
×
UNCOV
410
        ];
×
411
    }
412
}
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