Skip to content

Commit

Permalink
feat(graphql): Added assertGraphQLSchemaNoDangerousChanges() assert…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
LastDragon-ru committed Mar 13, 2024
1 parent ed9bfc1 commit ee41b8c
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,12 @@ Checks that no breaking changes in the default internal schema (with all directi

[Read more](<docs/Assertions/AssertGraphQLSchemaNoBreakingChanges.md>).

## `assertGraphQLSchemaNoDangerousChanges`

Checks that no dangerous changes in the default internal schema (with all directives).

[Read more](<docs/Assertions/AssertGraphQLSchemaNoDangerousChanges.md>).

## `assertGraphQLSchemaValid`

Validates default internal schema (with all directives). Faster than `lighthouse:validate-schema` command because loads only used directives.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# `assertGraphQLSchemaNoDangerousChanges`

Checks that no dangerous changes in the default internal schema (with all directives).

[include:example]: ./AssertGraphQLSchemaNoDangerousChangesTest.php
[//]: # (start: e89808347215c25407fece361d856f4087c43ac0599c7f65bba42fc4afc4fd51)
[//]: # (warning: Generated automatically. Do not edit.)

```php
<?php declare(strict_types = 1);

namespace Assertions;

use Illuminate\Container\Container;
use LastDragon_ru\LaraASP\GraphQL\Provider;
use LastDragon_ru\LaraASP\GraphQL\Testing\GraphQLAssertions;
use LastDragon_ru\LaraASP\GraphQL\Testing\Package\Directives\TestDirective;
use LastDragon_ru\LaraASP\GraphQL\Testing\Package\Provider as TestProvider;
use LastDragon_ru\LaraASP\Testing\Package\TestCase;
use Nuwave\Lighthouse\LighthouseServiceProvider;
use Nuwave\Lighthouse\Schema\DirectiveLocator;
use Override;
use PHPUnit\Framework\Attributes\CoversNothing;

/**
* @internal
*/
#[CoversNothing]
final class AssertGraphQLSchemaNoDangerousChangesTest extends TestCase {
/**
* Trait where assertion defined.
*/
use GraphQLAssertions;

/**
* Preparation for test.
*
* @inheritDoc
*/
#[Override]
protected function getPackageProviders(mixed $app): array {
return [
Provider::class,
TestProvider::class,
LighthouseServiceProvider::class,
];
}

/**
* Assertion test.
*/
public function testAssertion(): void {
// Prepare
Container::getInstance()->make(DirectiveLocator::class)
->setResolved('test', TestDirective::class);

$this->useGraphQLSchema(
<<<'GRAPHQL'
type Query {
a(a: Int = 123): String @test
}
GRAPHQL,
);

// Test
$this->assertGraphQLSchemaNoDangerousChanges(
<<<'GRAPHQL'
directive @test on FIELD_DEFINITION

type Query {
a(a: Int = 123): String @test
}
GRAPHQL,
);
}
}
```

[//]: # (end: e89808347215c25407fece361d856f4087c43ac0599c7f65bba42fc4afc4fd51)
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types = 1);

namespace Assertions;

use Illuminate\Container\Container;
use LastDragon_ru\LaraASP\GraphQL\Provider;
use LastDragon_ru\LaraASP\GraphQL\Testing\GraphQLAssertions;
use LastDragon_ru\LaraASP\GraphQL\Testing\Package\Directives\TestDirective;
use LastDragon_ru\LaraASP\GraphQL\Testing\Package\Provider as TestProvider;
use LastDragon_ru\LaraASP\Testing\Package\TestCase;
use Nuwave\Lighthouse\LighthouseServiceProvider;
use Nuwave\Lighthouse\Schema\DirectiveLocator;
use Override;
use PHPUnit\Framework\Attributes\CoversNothing;

/**
* @internal
*/
#[CoversNothing]
final class AssertGraphQLSchemaNoDangerousChangesTest extends TestCase {
/**
* Trait where assertion defined.
*/
use GraphQLAssertions;

/**
* Preparation for test.
*
* @inheritDoc
*/
#[Override]
protected function getPackageProviders(mixed $app): array {
return [
Provider::class,
TestProvider::class,
LighthouseServiceProvider::class,
];
}

/**
* Assertion test.
*/
public function testAssertion(): void {
// Prepare
Container::getInstance()->make(DirectiveLocator::class)
->setResolved('test', TestDirective::class);

$this->useGraphQLSchema(
<<<'GRAPHQL'
type Query {
a(a: Int = 123): String @test
}
GRAPHQL,
);

// Test
$this->assertGraphQLSchemaNoDangerousChanges(
<<<'GRAPHQL'
directive @test on FIELD_DEFINITION
type Query {
a(a: Int = 123): String @test
}
GRAPHQL,
);
}
}
16 changes: 16 additions & 0 deletions packages/graphql/src/Testing/GraphQLAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ public function assertGraphQLSchemaNoBreakingChanges(
self::assertTrue($changes === '', $message);
}

/**
* Checks the current (application) schema has no dangerous changes.
*/
public function assertGraphQLSchemaNoDangerousChanges(
SplFileInfo|string $expected,
string $message = '',
): void {
$oldSchema = BuildSchema::build(Args::content($expected));
$newSchema = BuildSchema::build($this->getGraphQLSchemaString());
$changes = BreakingChangesFinder::findDangerousChanges($oldSchema, $newSchema);
$changes = $this->getGraphQLChanges($changes);
$message = ($message ?: 'The dangerous changes found!')."\n\n{$changes}\n";

self::assertTrue($changes === '', $message);
}

/**
* @param Closure(Printer, Node|Type|Directive|FieldDefinition|Argument|EnumValueDefinition|InputObjectField|Schema): Result $print
*/
Expand Down
55 changes: 55 additions & 0 deletions packages/graphql/src/Testing/GraphQLAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,59 @@ public function testAssertGraphQLSchemaNoBreakingChanges(): void {
$message,
);
}

public function testAssertGraphQLSchemaNoDangerousChanges(): void {
// Prepare
Container::getInstance()->make(DirectiveLocator::class)
->setResolved('a', TestDirective::class)
->setResolved('test', TestDirective::class);

$this->useGraphQLSchema(
<<<'GRAPHQL'
directive @a(a: Boolean = true) on OBJECT
type Query @a {
a(a: Int = 321): Int! @test
}
GRAPHQL,
);

// Test
$valid = true;
$message = null;

try {
$this->assertGraphQLSchemaNoDangerousChanges(
<<<'GRAPHQL'
directive @a(a: Boolean = false) on OBJECT
directive @test on FIELD_DEFINITION
type Query @a(a: "value") {
a(a: Int = 123): Int! @test
}
GRAPHQL,
);
} catch (ExpectationFailedException $exception) {
$valid = false;
$message = $exception->getMessage();
}

self::assertFalse($valid);

// todo(graphql-php): Strange breaking changes:
// - `@a(a)` default value was changed but not detected

self::assertEquals(
<<<'STRING'
The dangerous changes found!
ARG_DEFAULT_VALUE_CHANGE:
* Query.a arg a has changed defaultValue
Failed asserting that false is true.
STRING,
$message,
);
}
}

0 comments on commit ee41b8c

Please sign in to comment.