|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace LastDragon_ru\LaraASP\GraphQL\Testing; |
| 4 | + |
| 5 | +use GraphQL\Type\Schema; |
| 6 | +use GraphQL\Utils\SchemaPrinter; |
| 7 | +use LastDragon_ru\LaraASP\Testing\Utils\Args; |
| 8 | +use Nuwave\Lighthouse\GraphQL; |
| 9 | +use Nuwave\Lighthouse\Schema\Source\SchemaSourceProvider; |
| 10 | +use Nuwave\Lighthouse\Testing\MocksResolvers; |
| 11 | +use Nuwave\Lighthouse\Testing\TestSchemaProvider; |
| 12 | +use SplFileInfo; |
| 13 | + |
| 14 | +/** |
| 15 | + * @mixin \PHPUnit\Framework\TestCase |
| 16 | + */ |
| 17 | +trait GraphQLAssertions { |
| 18 | + use MocksResolvers; |
| 19 | + |
| 20 | + /** |
| 21 | + * Compares two GraphQL schemas. |
| 22 | + */ |
| 23 | + public function assertGraphQLSchemaEquals( |
| 24 | + SplFileInfo|string $expected, |
| 25 | + SplFileInfo|string $schema, |
| 26 | + string $message = '', |
| 27 | + ): void { |
| 28 | + $this->assertEquals( |
| 29 | + Args::content($expected), |
| 30 | + $this->serializeGraphQLSchema($schema), |
| 31 | + $message, |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + protected function useGraphQLSchema(SplFileInfo|string $schema): static { |
| 36 | + $schema = Args::content($schema); |
| 37 | + |
| 38 | + $this->app->bind(SchemaSourceProvider::class, static function () use ($schema): SchemaSourceProvider { |
| 39 | + return new TestSchemaProvider($schema); |
| 40 | + }); |
| 41 | + |
| 42 | + return $this; |
| 43 | + } |
| 44 | + |
| 45 | + protected function getGraphQLSchema(SplFileInfo|string $schema): Schema { |
| 46 | + $this->useGraphQLSchema($schema); |
| 47 | + |
| 48 | + $graphql = $this->app->make(GraphQL::class); |
| 49 | + $schema = $graphql->prepSchema(); |
| 50 | + |
| 51 | + return $schema; |
| 52 | + } |
| 53 | + |
| 54 | + protected function serializeGraphQLSchema(SplFileInfo|string $schema): string { |
| 55 | + return SchemaPrinter::doPrint($this->getGraphQLSchema($schema)); |
| 56 | + } |
| 57 | +} |
0 commit comments