When writing the automated test case of Lighthouse 5, when you want to use a variable in the query, the error is reported: syntax error: expected $, and then failed Asserting that an array has the subset array
1. Reference URL:https://lighthouse-php.com/4/testing/phpunit.html#setup, test with phpunit . In Lighthouse, it is easy to add automated tests with phpunit.
2. Run the GraphQL Query API and respond 200. The main test field: The responses of TheMeAssets. as shown in Figure 1
3. Edit /modules/themestore/tests/functional/graphql/onlineStoreEthemeGraphQLapTest.php, the code is as follows
graphQL('
{
query GetThemeById($id: ID!) {
onlineStoreTheme(themeId: $id) {
id
editable
createdAt
name
}
}
}
',
[
'id' => 'vogue',
]
);
$response->assertJson([
'data' => [
'onlineStoreTheme' => [
'id' => 'vogue'
],
],
]);
}
protected function setUp(): void
{
parent::setUp();
$this->bootClearsSchemaCache();
}
}
4. Execute the command: ./vendor/bin/phpunit .\modules\themestore\tests\functional\graphql\OnlineStoreEthemeGraphQLapTest.php . Error: Failed asserting that an array has the subset array . as shown in Figure 2
PS E:\wwwroot\object> ./vendor/bin/phpunit .\Modules\ThemeStore\Tests\Functional\GraphQl\OnlineStoreThemeGraphQlApiTest.php
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 919 ms, Memory: 56.00 MB
There was 1 failure:
1) Modules\ThemeStore\Tests\Functional\GraphQl\OnlineStoreThemeGraphQlApiTest::testGetThemeById
Unable to find JSON:
[{
"data": {
"onlineStoreTheme": {
"id": "vogue"
}
}
}]
within response JSON:
[{
"errors": [
{
"message": "Syntax Error: Expected Name, found $",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 3,
"column": 36
}
]
}
]
}].
Failed asserting that an array has the subset Array &0 (
'data' => Array &1 (
'onlineStoreTheme' => Array &2 (
'id' => 'vogue'
)
)
).
--- Expected
+++ Actual
@@ @@
),
),
),
- 'data' =>
- array (
- 'onlineStoreTheme' =>
- array (
- 'id' => 'vogue',
- ),
- ),
)
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Assert.php:108
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:479
E:\wwwroot\object\Modules\ThemeStore\Tests\Functional\GraphQl\OnlineStoreThemeGraphQlApiTest.php:38
phpvfscomposer://E:\wwwroot\object\vendor\phpunit\phpunit\phpunit:60
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
PS E:\wwwroot\object>
5. The root cause is that the response is inconsistent with the response to manually sending the request. Decided to write a test case without parameters. /tests/feature/PostsGraphQLapTest.php
create();
$response = $this->graphQL('
{
posts {
id
title
content
}
}
');
$response->assertJson([
'data' => [
'posts' => [
[
'id' => $post->id,
'title' => $post->title,
'content' => $post->content,
]
],
],
]);
}
protected function setUp(): void
{
parent::setUp();
$this->bootClearsSchemaCache();
}
}
6. Run the test: ./vendor/bin/phpunit .\tests\feature\postsgraphqlapiTest.php, report error: failed asserting that an array has The Subset Array. But the response is determined to be a JSON structure, but the first element is not matched. as shown in Figure 3
{
"id": "104",
"title": "Tenetur id labore dolores aperiam.",
it vel ut corrupti repellat quaerat. Et eos exercitationem voluptatem ut."
},
{
"id": "105",
"title": "Incidunt nulla libero officia aliquam dolor ut.",
"content": "Molestiae harum velit numquam atque qui consequatur quam. Et sequi voluptatem sunt. Nemo et quo quod consequuntur. Quis eos consequatur recusandae in doloribus rerum odit odio."
}
]
}
}].
Failed asserting that an array has the subset Array &0 (
'data' => Array &1 (
'posts' => Array &2 (
0 => Array &3 (
'id' => 105
'title' => 'Incidunt nulla libero officia aliquam dolor ut.'
'content' => 'Molestiae harum velit numquam atque qui consequatur quam. Et sequi voluptatem sunt. Nemo et quo quod consequuntur. Quis eos consequatur recusandae in doloribus rerum odit odio.'
)
)
)
).
--- Expected
+++ Actual
array (
0 =>
array (
- 'id' => 105,
- 'title' => 'Incidunt nulla libero officia aliquam dolor ut.',
- 'content' => 'Molestiae harum velit numquam atque qui consequatur quam. Et sequi voluptatem sunt. Nemo et quo quod consequuntur. Quis eos consequatur recusandae in doloribus rerum odit odio.',
+ 'id' => '1',
+ 'title' => 'Optio earum deserunt fuga qui quia hic.',
+ 'content' => 'Fuga molestiae quis sit quis maiores et. Amet eveniet sint quidem deleniti voluptas necessitatibus. Aut aut voluptatibus ut maiores doloremque eos totam.',
),
1 =>
array (
E:\wwwroot\lighthouse-tutorial\vendor\laravel\framework\src\Illuminate\Testing\Constraints\ArraySubset.php:85
E:\wwwroot\lighthouse-tutorial\vendor\laravel\framework\src\Illuminate\Testing\Assert.php:40
E:\wwwroot\lighthouse-tutorial\vendor\laravel\framework\src\Illuminate\Testing\AssertableJsonString.php:273
E:\wwwroot\lighthouse-tutorial\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php:695
E:\wwwroot\lighthouse-tutorial\tests\Feature\PostsGraphQlApiTest.php:36
phpvfscomposer://E:\wwwroot\lighthouse-tutorial\vendor\phpunit\phpunit\phpunit:97
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
7. Get the last key value of the $posts array, and then match successfully. The running test passes. as shown in Figure 4
create();
$response = $this->graphQL('
{
posts {
id
title
content
}
}
');
$responsePosts = $response->json("data.posts");
$lastKey = array_key_last($responsePosts);
$response->assertJson([
'data' => [
'posts' => [
$lastKey => [
'id' => $post->id,
'title' => $post->title,
'content' => $post->content,
]
],
],
]);
}
protected function setUp(): void
{
parent::setUp();
$this->bootClearsSchemaCache();
}
}
PS E:\wwwroot\lighthouse-tutorial> ./vendor/bin/phpunit .\tests\Feature\PostsGraphQlApiTest.php
PHPUnit 9.5.11 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.434, Memory: 38.00 MB
OK (1 test, 1 assertion)
PS E:\wwwroot\lighthouse-tutorial>
8. Print the response object.
PS E:\wwwroot\lighthouse-tutorial> ./vendor/bin/phpunit .\tests\Feature\UsersGraphQlApiTest.php
PHPUnit 9.5.11 by Sebastian Bergmann and contributors.
Illuminate\Testing\TestResponse Object
(
[baseResponse] => Illuminate\Http\Response Object
(
[headers] => Symfony\Component\HttpFoundation\ResponseHeaderBag Object
(
[computedCacheControl:protected] => Array
(
[no-cache] => 1
[private] => 1
)
[cookies:protected] => Array
(
)
[headerNames:protected] => Array
(
[cache-control] => Cache-Control
[date] => Date
[content-type] => Content-Type
)
[headers:protected] => Array
(
[cache-control] => Array
(
[0] => no-cache, private
)
[date] => Array
(
[0] => Thu, 20 Jan 2022 06:33:46 GMT
)
[content-type] => Array
(
[0] => application/json
)
)
[cacheControl:protected] => Array
(
)
)
[content:protected] => {"errors":[{"message":"Syntax Error: Expected Name, found $","extensions":{"category":"graphql"},"locations":[{"line":3,"column":36}]}]}
[version:protected] => 1.1
[statusCode:protected] => 200
[statusText:protected] => OK
[charset:protected] =>
[original] => Array
(
[errors] => Array
(
[0] => Array
(
[message] => Syntax Error: Expected Name, found $
[extensions] => Array
(
[category] => graphql
)
[locations] => Array
(
[0] => Array
(
[line] => 3
[column] => 36
)
)
)
)
)
[exception] =>
)
[exceptions:protected] => Illuminate\Testing\LoggedExceptionCollection Object
(
[items:protected] => Array
(
)
[escapeWhenCastingToString:protected] =>
)
[streamedContent:protected] =>
)
PS E:\wwwroot\lighthouse-tutorial>
9. The final source is that the curly braces are more than one layer. as shown in Figure 5
public function testGetThemeById(): void
{
$response = $this->graphQL('
query GetThemeById($id: ID!) {
onlineStoreTheme(themeId: $id) {
id
editable
createdAt
name
}
}
',
[
'id' => 'vogue',
]
);
$response->assertJson([
'data' => [
'onlineStoreTheme' => [
'id' => 'vogue'
],
],
]);
}
10. Run the test and pass the test. as shown in Figure 6
PS E:\wwwroot\object> ./vendor/bin/phpunit .\Modules\ThemeStore\Tests\Functional\GraphQl\OnlineStoreThemeGraphQlApiTest.php
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 1.69 seconds, Memory: 60.00 MB
OK (1 test, 1 assertion)
PS E:\wwwroot\object>





