Conversation
| use Drupal\Tests\graphql\Kernel\GraphQLTestBase; | ||
| use Drupal\user\Entity\User; | ||
|
|
||
| class ExampleSchemaTest extends GraphQLTestBase { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
|
We have a couple of exceptions in phpcs.xml.dist for the example module, so you also need to fix the paths there. The other coding standard fixes should be straightforward. |
| $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); |
There was a problem hiding this comment.
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.
| $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'], | |
| ], | |
| ], | |
| ], | |
| ] | |
| ); |
There was a problem hiding this comment.
You are right @Kingdutch ! 🤦 Thanks for the input.
|
Another thought: should we be excluding this test from the test runs? If we're going to provide an example, it'd be good to just run the test, this way we also know when the documentation/example breaks :D Otherwise we may end up with someone copying a test that doesn't work in the first place. |
|
Yep, we absolutely should execute this test! I think that is done with how we run phpunit on github actions (by just pointing at the graphql module), but I did not double-check. |
Since I was asked how to do it and realised we have no canonical example or documentation.