|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Ruudk\GraphQLCodeGenerator\ListScalar; |
| 6 | + |
| 7 | +use Override; |
| 8 | +use Ruudk\GraphQLCodeGenerator\Config\Config; |
| 9 | +use Ruudk\GraphQLCodeGenerator\GraphQLTestCase; |
| 10 | +use Ruudk\GraphQLCodeGenerator\ListScalar\Generated\Query\Test\TestQuery; |
| 11 | +use Ruudk\GraphQLCodeGenerator\ListScalar\ValueObjects\Currency; |
| 12 | +use Symfony\Component\TypeInfo\Type; |
| 13 | + |
| 14 | +/** |
| 15 | + * Test that lists of custom scalars are correctly typed in the generated code. |
| 16 | + * |
| 17 | + * This tests the fix for a bug where the $builtInOnly parameter was not correctly |
| 18 | + * passed when processing ListOfType in TypeMapper::mapGraphQLTypeToPHPType(). |
| 19 | + * |
| 20 | + * The bug caused the $data parameter docblock to incorrectly use the PHP mapped type |
| 21 | + * (e.g., Currency) instead of the primitive JSON type (e.g., string) for list items. |
| 22 | + */ |
| 23 | +final class ListScalarTest extends GraphQLTestCase |
| 24 | +{ |
| 25 | + #[Override] |
| 26 | + public function getConfig() : Config |
| 27 | + { |
| 28 | + return parent::getConfig() |
| 29 | + ->withScalar('Currency', Type::string(), Type::object(Currency::class)); |
| 30 | + } |
| 31 | + |
| 32 | + public function testGenerate() : void |
| 33 | + { |
| 34 | + $this->assertActualMatchesExpected(); |
| 35 | + } |
| 36 | + |
| 37 | + public function testQueryWithListOfScalars() : void |
| 38 | + { |
| 39 | + $result = new TestQuery($this->getClient([ |
| 40 | + 'data' => [ |
| 41 | + 'supportedCurrencies' => ['EUR', 'USD', 'GBP'], |
| 42 | + 'wallet' => [ |
| 43 | + 'name' => 'My Wallet', |
| 44 | + 'currencies' => ['EUR', 'USD'], |
| 45 | + ], |
| 46 | + ], |
| 47 | + ]))->execute(); |
| 48 | + |
| 49 | + // Verify the list of currencies is correctly converted to Currency objects |
| 50 | + self::assertCount(3, $result->supportedCurrencies); |
| 51 | + self::assertSame('EUR', $result->supportedCurrencies[0]->code); |
| 52 | + self::assertSame('USD', $result->supportedCurrencies[1]->code); |
| 53 | + self::assertSame('GBP', $result->supportedCurrencies[2]->code); |
| 54 | + |
| 55 | + // Verify nested list of currencies |
| 56 | + self::assertSame('My Wallet', $result->wallet->name); |
| 57 | + self::assertCount(2, $result->wallet->currencies); |
| 58 | + self::assertSame('EUR', $result->wallet->currencies[0]->code); |
| 59 | + self::assertSame('USD', $result->wallet->currencies[1]->code); |
| 60 | + } |
| 61 | +} |
0 commit comments