From 6d3965f02d6cd9d33ad220d2ad3be6c6838dfb93 Mon Sep 17 00:00:00 2001 From: Philipp Melab Date: Mon, 1 Feb 2021 08:23:40 +0100 Subject: [PATCH 1/6] fix: adjust example module folder name to match info and namespaces --- .../graphql/example.graphqls | 0 .../graphql/example_extension.base.graphqls | 0 .../graphql/example_extension.extension.graphqls | 0 .../graphql_examples.info.yml | 0 .../src/Plugin/GraphQL/DataProducer/QueryArticles.php | 0 .../src/Plugin/GraphQL/Schema/ExampleSchema.php | 0 .../src/Plugin/GraphQL/SchemaExtension/ExampleSchemaExtension.php | 0 .../src/Wrappers/QueryConnection.php | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename examples/{graphql_example => graphql_examples}/graphql/example.graphqls (100%) rename examples/{graphql_example => graphql_examples}/graphql/example_extension.base.graphqls (100%) rename examples/{graphql_example => graphql_examples}/graphql/example_extension.extension.graphqls (100%) rename examples/{graphql_example => graphql_examples}/graphql_examples.info.yml (100%) rename examples/{graphql_example => graphql_examples}/src/Plugin/GraphQL/DataProducer/QueryArticles.php (100%) rename examples/{graphql_example => graphql_examples}/src/Plugin/GraphQL/Schema/ExampleSchema.php (100%) rename examples/{graphql_example => graphql_examples}/src/Plugin/GraphQL/SchemaExtension/ExampleSchemaExtension.php (100%) rename examples/{graphql_example => graphql_examples}/src/Wrappers/QueryConnection.php (100%) diff --git a/examples/graphql_example/graphql/example.graphqls b/examples/graphql_examples/graphql/example.graphqls similarity index 100% rename from examples/graphql_example/graphql/example.graphqls rename to examples/graphql_examples/graphql/example.graphqls diff --git a/examples/graphql_example/graphql/example_extension.base.graphqls b/examples/graphql_examples/graphql/example_extension.base.graphqls similarity index 100% rename from examples/graphql_example/graphql/example_extension.base.graphqls rename to examples/graphql_examples/graphql/example_extension.base.graphqls diff --git a/examples/graphql_example/graphql/example_extension.extension.graphqls b/examples/graphql_examples/graphql/example_extension.extension.graphqls similarity index 100% rename from examples/graphql_example/graphql/example_extension.extension.graphqls rename to examples/graphql_examples/graphql/example_extension.extension.graphqls diff --git a/examples/graphql_example/graphql_examples.info.yml b/examples/graphql_examples/graphql_examples.info.yml similarity index 100% rename from examples/graphql_example/graphql_examples.info.yml rename to examples/graphql_examples/graphql_examples.info.yml diff --git a/examples/graphql_example/src/Plugin/GraphQL/DataProducer/QueryArticles.php b/examples/graphql_examples/src/Plugin/GraphQL/DataProducer/QueryArticles.php similarity index 100% rename from examples/graphql_example/src/Plugin/GraphQL/DataProducer/QueryArticles.php rename to examples/graphql_examples/src/Plugin/GraphQL/DataProducer/QueryArticles.php diff --git a/examples/graphql_example/src/Plugin/GraphQL/Schema/ExampleSchema.php b/examples/graphql_examples/src/Plugin/GraphQL/Schema/ExampleSchema.php similarity index 100% rename from examples/graphql_example/src/Plugin/GraphQL/Schema/ExampleSchema.php rename to examples/graphql_examples/src/Plugin/GraphQL/Schema/ExampleSchema.php diff --git a/examples/graphql_example/src/Plugin/GraphQL/SchemaExtension/ExampleSchemaExtension.php b/examples/graphql_examples/src/Plugin/GraphQL/SchemaExtension/ExampleSchemaExtension.php similarity index 100% rename from examples/graphql_example/src/Plugin/GraphQL/SchemaExtension/ExampleSchemaExtension.php rename to examples/graphql_examples/src/Plugin/GraphQL/SchemaExtension/ExampleSchemaExtension.php diff --git a/examples/graphql_example/src/Wrappers/QueryConnection.php b/examples/graphql_examples/src/Wrappers/QueryConnection.php similarity index 100% rename from examples/graphql_example/src/Wrappers/QueryConnection.php rename to examples/graphql_examples/src/Wrappers/QueryConnection.php From 5d4771c8c624bb51fc574ef5fe3abcf0f13dbce3 Mon Sep 17 00:00:00 2001 From: Philipp Melab Date: Mon, 1 Feb 2021 08:25:50 +0100 Subject: [PATCH 2/6] test: add example for unit-testing a full schema plugin --- .../src/Kernel/Schema/ExampleSchemaTest.php | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php diff --git a/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php b/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php new file mode 100644 index 000000000..34e7fc005 --- /dev/null +++ b/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php @@ -0,0 +1,82 @@ + 'article', + 'name' => 'Article', + ]); + + // Create a test-server that uses the schema plugin defined in this module. + $this->createTestServer('example', '/graphql'); + } + + /** + * Test the example schema for article listing. + */ + public function testExampleSchema() { + // Create two authors. + $userA = User::create([ + 'name' => 'A' + ]); + $userA->save(); + + $userB = User::create([ + 'name' => 'B' + ]); + $userB->save(); + + // Create three articles. + Node::create([ + 'type' => 'article', + 'title' => 'One', + 'uid' => $userA->id(), + ])->save(); + + Node::create([ + 'type' => 'article', + 'title' => 'Two', + 'uid' => $userB->id(), + ])->save(); + + Node::create([ + 'type' => 'article', + 'title' => 'Three', + 'uid' => $userA->id(), + ])->save(); + + // Execute the query and run assertions against its response content. + $response = $this->query('{ articles { total, items { title, author } } }'); + $content = json_decode($response->getContent(), TRUE); + $this->assertEquals([ + 'data' => [ + 'articles' => [ + 'total' => 3, + 'items' => [ + ['title' => 'ONE', 'author' => 'A'], + ['title' => 'TWO', 'author' => 'B'], + ['title' => 'THREE', 'author' => 'A'], + ], + ], + ], + ], $content); + } +} From 4bee230f565833096987928790e46bebbb4478c2 Mon Sep 17 00:00:00 2001 From: Philipp Melab Date: Mon, 1 Feb 2021 08:53:27 +0100 Subject: [PATCH 3/6] fix: add return type hint to please PHPStan --- .../tests/src/Kernel/Schema/ExampleSchemaTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php b/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php index 34e7fc005..e44b5b2c6 100644 --- a/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php +++ b/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php @@ -32,7 +32,7 @@ protected function setUp() :void { /** * Test the example schema for article listing. */ - public function testExampleSchema() { + public function testExampleSchema() : void { // Create two authors. $userA = User::create([ 'name' => 'A' From 6806b4e865cec63f57516049c6cde7880faeaca5 Mon Sep 17 00:00:00 2001 From: Philipp Melab Date: Tue, 2 Feb 2021 10:59:59 +0100 Subject: [PATCH 4/6] style: address PHPStan complaints --- .../tests/src/Kernel/Schema/ExampleSchemaTest.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php b/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php index e44b5b2c6..6a444b87d 100644 --- a/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php +++ b/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php @@ -7,12 +7,17 @@ use Drupal\Tests\graphql\Kernel\GraphQLTestBase; use Drupal\user\Entity\User; +/** + * Runs unit tests agains the `example` schema defined in `graphql_examples`. + * + * @group graphql + */ class ExampleSchemaTest extends GraphQLTestBase { /** * {@inheritdoc} */ - static $modules = ['graphql_examples']; + public static $modules = ['graphql_examples']; /** * {@inheritdoc} @@ -35,12 +40,12 @@ protected function setUp() :void { public function testExampleSchema() : void { // Create two authors. $userA = User::create([ - 'name' => 'A' + 'name' => 'A', ]); $userA->save(); $userB = User::create([ - 'name' => 'B' + 'name' => 'B', ]); $userB->save(); @@ -69,7 +74,7 @@ public function testExampleSchema() : void { $this->assertEquals([ 'data' => [ 'articles' => [ - 'total' => 3, + 'total' => 3, 'items' => [ ['title' => 'ONE', 'author' => 'A'], ['title' => 'TWO', 'author' => 'B'], @@ -79,4 +84,5 @@ public function testExampleSchema() : void { ], ], $content); } + } From 4b2948ac63a3bc4da1ff8f7b4e9bc9b5f09e79d3 Mon Sep 17 00:00:00 2001 From: Philipp Melab Date: Tue, 2 Feb 2021 11:03:46 +0100 Subject: [PATCH 5/6] ci: adjusted phpcs exclude paths to match `graphql_examples` directory name --- phpcs.xml.dist | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index c5fbcb917..c8182d5cd 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -123,10 +123,10 @@ src/Plugin/GraphQL/Schema/SdlSchemaPluginBase.php src/Plugin/DataProducerPluginManager.php src/Controller/RequestController.php - examples/graphql_example/src/Wrappers/QueryConnection.php - examples/graphql_example/src/Plugin/GraphQL/DataProducer/QueryArticles.php - examples/graphql_example/src/Plugin/GraphQL/Schema/ExampleSchema.php - examples/graphql_example/src/Plugin/GraphQL/SchemaExtension/ExampleSchemaExtension.php + examples/graphql_examples/src/Wrappers/QueryConnection.php + examples/graphql_examples/src/Plugin/GraphQL/DataProducer/QueryArticles.php + examples/graphql_examples/src/Plugin/GraphQL/Schema/ExampleSchema.php + examples/graphql_examples/src/Plugin/GraphQL/SchemaExtension/ExampleSchemaExtension.php tests/src/Traits/MockingTrait.php tests/src/Traits/DataProducerExecutionTrait.php tests/src/Kernel/ResolverBuilderTest.php @@ -193,8 +193,8 @@ src/Plugin/DataProducerPluginManager.php src/Plugin/DataProducerPluginCachingInterface.php src/Controller/RequestController.php - examples/graphql_example/src/Wrappers/QueryConnection.php - examples/graphql_example/src/Plugin/GraphQL/DataProducer/QueryArticles.php + examples/graphql_examples/src/Wrappers/QueryConnection.php + examples/graphql_examples/src/Plugin/GraphQL/DataProducer/QueryArticles.php tests/src/Traits/DataProducerExecutionTrait.php tests/src/Kernel/ResolverBuilderTest.php tests/src/Kernel/DataProducer/XML/XMLTestBase.php From 1c03feae1d22900f810a177ff7c720ea5d2e0cc7 Mon Sep 17 00:00:00 2001 From: Philipp Melab Date: Wed, 3 Feb 2021 13:26:12 +0100 Subject: [PATCH 6/6] refactor: use `assertResults` with cache metadata checks instead --- .../src/Kernel/Schema/ExampleSchemaTest.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php b/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php index 6a444b87d..0f260192e 100644 --- a/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php +++ b/examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\graphql_example\Kernel; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; use Drupal\Tests\graphql\Kernel\GraphQLTestBase; @@ -69,20 +70,19 @@ public function testExampleSchema() : void { ])->save(); // Execute the query and run assertions against its response content. - $response = $this->query('{ articles { total, items { title, author } } }'); - $content = json_decode($response->getContent(), TRUE); - $this->assertEquals([ - 'data' => [ - 'articles' => [ - 'total' => 3, - 'items' => [ - ['title' => 'ONE', 'author' => 'A'], - ['title' => 'TWO', 'author' => 'B'], - ['title' => 'THREE', 'author' => 'A'], - ], + $this->assertResults('{ articles { total, items { title, author } } }', [], [ + 'articles' => [ + 'total' => 3, + 'items' => [ + ['title' => 'ONE', 'author' => 'A'], + ['title' => 'TWO', 'author' => 'B'], + ['title' => 'THREE', 'author' => 'A'], ], ], - ], $content); + ], $this->defaultCacheMetaData() + ->addCacheContexts(['user.node_grants:view']) + ->addCacheTags(['node:1', 'node:2', 'node:3', 'node_list', 'user:3', 'user:4']) + ); } }