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 6 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\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.
$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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of $this->query I think we should use $this->assertResults.

The benefits are that it bypasses the HTTP stack which is slightly faster. It also handles the response decoding for you and allows you to test for caching metadata.

Suggested change
$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);
$this->assertResults(
'{ articles { total, items { title, author } } }',
[],
[
'data' => [
'articles' => [
'total' => 3,
'items' => [
['title' => 'ONE', 'author' => 'A'],
['title' => 'TWO', 'author' => 'B'],
['title' => 'THREE', 'author' => 'A'],
],
],
],
]
);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right @Kingdutch ! 🤦 Thanks for the input.

}

}
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