-
Notifications
You must be signed in to change notification settings - Fork 201
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
pmelab
wants to merge
7
commits into
8.x-4.x
Choose a base branch
from
schema-unit-test-example
base: 8.x-4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6d3965f
fix: adjust example module folder name to match info and namespaces
pmelab 5d4771c
test: add example for unit-testing a full schema plugin
pmelab 4bee230
fix: add return type hint to please PHPStan
pmelab 105fe96
Merge branch '8.x-4.x' into schema-unit-test-example
klausi 6806b4e
style: address PHPStan complaints
pmelab 4b2948a
ci: adjusted phpcs exclude paths to match `graphql_examples` director…
pmelab 1c03fea
refactor: use `assertResults` with cache metadata checks instead
pmelab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
88 changes: 88 additions & 0 deletions
88
examples/graphql_examples/tests/src/Kernel/Schema/ExampleSchemaTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
/** | ||
* {@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']) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.