Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for doing a full schema unit test #1154

Open
wants to merge 7 commits into
base: 8.x-4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

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;
use Drupal\user\Entity\User;

/**
* Runs unit tests agains the `example` schema defined in `graphql_examples`.
*
* @group graphql
*/
class ExampleSchemaTest extends GraphQLTestBase {

This comment was marked as resolved.


/**
* {@inheritdoc}
*/
public static $modules = ['graphql_examples'];

/**
* {@inheritdoc}
*/
protected function setUp() :void {
parent::setUp();
// Create the "article" node type since the schema relies on it.
NodeType::create([
'type' => '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() : void {
// 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.
$this->assertResults('{ articles { total, items { title, author } } }', [], [
'articles' => [
'total' => 3,
'items' => [
['title' => 'ONE', 'author' => 'A'],
['title' => 'TWO', 'author' => 'B'],
['title' => 'THREE', 'author' => 'A'],
],
],
], $this->defaultCacheMetaData()
->addCacheContexts(['user.node_grants:view'])
->addCacheTags(['node:1', 'node:2', 'node:3', 'node_list', 'user:3', 'user:4'])
);
}

}
12 changes: 6 additions & 6 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@
<exclude-pattern>src/Plugin/GraphQL/Schema/SdlSchemaPluginBase.php</exclude-pattern>
<exclude-pattern>src/Plugin/DataProducerPluginManager.php</exclude-pattern>
<exclude-pattern>src/Controller/RequestController.php</exclude-pattern>
<exclude-pattern>examples/graphql_example/src/Wrappers/QueryConnection.php</exclude-pattern>
<exclude-pattern>examples/graphql_example/src/Plugin/GraphQL/DataProducer/QueryArticles.php</exclude-pattern>
<exclude-pattern>examples/graphql_example/src/Plugin/GraphQL/Schema/ExampleSchema.php</exclude-pattern>
<exclude-pattern>examples/graphql_example/src/Plugin/GraphQL/SchemaExtension/ExampleSchemaExtension.php</exclude-pattern>
<exclude-pattern>examples/graphql_examples/src/Wrappers/QueryConnection.php</exclude-pattern>
<exclude-pattern>examples/graphql_examples/src/Plugin/GraphQL/DataProducer/QueryArticles.php</exclude-pattern>
<exclude-pattern>examples/graphql_examples/src/Plugin/GraphQL/Schema/ExampleSchema.php</exclude-pattern>
<exclude-pattern>examples/graphql_examples/src/Plugin/GraphQL/SchemaExtension/ExampleSchemaExtension.php</exclude-pattern>
<exclude-pattern>tests/src/Traits/MockingTrait.php</exclude-pattern>
<exclude-pattern>tests/src/Traits/DataProducerExecutionTrait.php</exclude-pattern>
<exclude-pattern>tests/src/Kernel/ResolverBuilderTest.php</exclude-pattern>
Expand Down Expand Up @@ -193,8 +193,8 @@
<exclude-pattern>src/Plugin/DataProducerPluginManager.php</exclude-pattern>
<exclude-pattern>src/Plugin/DataProducerPluginCachingInterface.php</exclude-pattern>
<exclude-pattern>src/Controller/RequestController.php</exclude-pattern>
<exclude-pattern>examples/graphql_example/src/Wrappers/QueryConnection.php</exclude-pattern>
<exclude-pattern>examples/graphql_example/src/Plugin/GraphQL/DataProducer/QueryArticles.php</exclude-pattern>
<exclude-pattern>examples/graphql_examples/src/Wrappers/QueryConnection.php</exclude-pattern>
<exclude-pattern>examples/graphql_examples/src/Plugin/GraphQL/DataProducer/QueryArticles.php</exclude-pattern>
<exclude-pattern>tests/src/Traits/DataProducerExecutionTrait.php</exclude-pattern>
<exclude-pattern>tests/src/Kernel/ResolverBuilderTest.php</exclude-pattern>
<exclude-pattern>tests/src/Kernel/DataProducer/XML/XMLTestBase.php</exclude-pattern>
Expand Down