Skip to content

Commit ef6c97a

Browse files
committed
[graphql] Added GraphQLAssertions
1 parent 9ea862e commit ef6c97a

File tree

5 files changed

+72
-30
lines changed

5 files changed

+72
-30
lines changed

src/Helpers/EnumHelperTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ public function testGetType(): void {
2222
$registry->register($a);
2323
$registry->register($b);
2424

25-
$expected = $this->getTestData()->content('.graphql');
26-
$actual = $this->getSchema(
27-
/** @lang GraphQL */
25+
$expected = $this->getTestData()->file('.graphql');
26+
$actual = /** @lang GraphQL */
2827
<<<'GRAPHQL'
2928
type Query {
3029
test(a: EnumHelperTest__A, b: B): ID! @all
3130
}
32-
GRAPHQL,
33-
);
31+
GRAPHQL;
3432

35-
$this->assertEquals($expected, $actual);
33+
$this->assertGraphQLSchemaEquals($expected, $actual);
3634
}
3735
}
3836

src/SearchBy/DirectiveTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class DirectiveTest extends TestCase {
2323
* @dataProvider dataProviderManipulateArgDefinition
2424
*/
2525
public function testManipulateArgDefinition(string $expected, string $graphql): void {
26-
$expected = $this->getTestData()->content($expected);
27-
$actual = $this->getSchema($this->getTestData()->content($graphql));
28-
29-
$this->assertEquals($expected, $actual);
26+
$this->assertGraphQLSchemaEquals(
27+
$this->getTestData()->file($expected),
28+
$this->getTestData()->file($graphql),
29+
);
3030
}
3131

3232
/**

src/SortBy/DirectiveTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class DirectiveTest extends TestCase {
1717
* @dataProvider dataProviderManipulateArgDefinition
1818
*/
1919
public function testManipulateArgDefinition(string $expected, string $graphql): void {
20-
$expected = $this->getTestData()->content($expected);
21-
$actual = $this->getSchema($this->getTestData()->content($graphql));
22-
23-
$this->assertEquals($expected, $actual);
20+
$this->assertGraphQLSchemaEquals(
21+
$this->getTestData()->file($expected),
22+
$this->getTestData()->file($graphql),
23+
);
2424
}
2525
// </editor-fold>
2626

src/Testing/GraphQLAssertions.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

src/Testing/Package/TestCase.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
namespace LastDragon_ru\LaraASP\GraphQL\Testing\Package;
44

5-
use GraphQL\Utils\SchemaPrinter;
65
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
76
use Illuminate\Database\Query\Builder as QueryBuilder;
87
use LastDragon_ru\LaraASP\GraphQL\Provider;
8+
use LastDragon_ru\LaraASP\GraphQL\Testing\GraphQLAssertions;
99
use LastDragon_ru\LaraASP\Testing\Package\TestCase as PackageTestCase;
10-
use Nuwave\Lighthouse\GraphQL;
1110
use Nuwave\Lighthouse\LighthouseServiceProvider;
12-
use Nuwave\Lighthouse\Schema\Source\SchemaSourceProvider;
13-
use Nuwave\Lighthouse\Testing\TestSchemaProvider;
1411

1512
use function array_column;
1613
use function array_unique;
@@ -22,6 +19,8 @@
2219
use const PREG_SET_ORDER;
2320

2421
class TestCase extends PackageTestCase {
22+
use GraphQLAssertions;
23+
2524
/**
2625
* @inheritdoc
2726
*/
@@ -32,18 +31,6 @@ protected function getPackageProviders($app): array {
3231
];
3332
}
3433

35-
protected function getSchema(string $schema): string {
36-
$this->app->bind(SchemaSourceProvider::class, static function () use ($schema): SchemaSourceProvider {
37-
return new TestSchemaProvider($schema);
38-
});
39-
40-
$graphql = $this->app->make(GraphQL::class);
41-
$schema = $graphql->prepSchema();
42-
$schema = SchemaPrinter::doPrint($schema);
43-
44-
return $schema;
45-
}
46-
4734
/**
4835
* @return array{sql: string, bindings: array<mixed>}
4936
*/

0 commit comments

Comments
 (0)