没有不值得去解决的问题,也没有不值得去学习的技术!

Fix PhpStorm Autocompletion When Writing Automated Test Cases for Lighthouse 5

编写 Lighthouse 5 的自动化测试用例时,PhpStorm 不能够自动完成。点击 $response->assertJson 时,无法找到要转到的声明。方法 'assertJson' 在 \Illuminate\Testing\TestResponse 中未找到

1. When writing automated test cases for Lighthouse 5, PhpStorm cannot autocomplete. Clicking $response->assertJson fails to find the declaration to go to. Method ‘assertJson’ not found in \Illuminate\Testing\TestResponse. See Figure 1.

When writing automated test cases for Lighthouse 5, PhpStorm cannot autocomplete. Clicking $response->assertJson fails to find the declaration to go to. Method 'assertJson' not found in \Illuminate\Testing\TestResponse
Figure 1

2. Printing the $response object reveals its actual path is: \Illuminate\Foundation\Testing\TestResponse. See Figure 2.

Printing the $response object reveals its actual path is: \Illuminate\Foundation\Testing\TestResponse
Figure 2
Plaintext

PS E:\wwwroot\object> ./vendor/bin/phpunit --process-isolation .\Modules\ThemeStore\Tests\Functional\GraphQl\OnlineStoreThemeGraphQlApiTest.php
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

E..                                                                 3 / 3 (100%)

Time: 11.89 seconds, Memory: 16.00 MB

There was 1 error:

1) Modules\ThemeStore\Tests\Functional\GraphQl\OnlineStoreThemeGraphQlApiTest::testGetThemeById
PHPUnit\Framework\Exception: Illuminate\Foundation\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, 17 Feb 2022 06:16:07 GMT
                                )

                            [content-type] => Array
                                (
                                    [0] => application/json
                                )

                        )

                    [cacheControl:protected] => Array
                        (
                        )

                )

            [content:protected] => {"data":{"onlineStoreTheme":{"id":"vogue","editable":false,"createdAt":"2022-02-17 14:16:07","name":"vogue"}}}
            [version:protected] => 1.1
            [statusCode:protected] => 200
            [statusText:protected] => OK
            [charset:protected] =>
            [original] => Array
                (
                    [data] => Array
                        (
                            [onlineStoreTheme] => Array
                                (
                                    [id] => vogue
                                    [editable] =>
                                    [createdAt] => 2022-02-17 14:16:07
                                    [name] => vogue
                                )

                        )

                )

            [exception] =>
        )

    [streamedContent:protected] =>
)

phpvfscomposer://E:\wwwroot\object\vendor\phpunit\phpunit\phpunit:60


3. In another project, PhpStorm can autocomplete. See Figure 3.

In another project, PhpStorm can autocomplete
Figure 3

4. In the other project, printing the $response object reveals its actual path is: \Illuminate\Testing\TestResponse. Compared to the project where the method cannot be located, it is missing \Foundation. See Figure 4.

In the other project, printing the $response object reveals its actual path is: \Illuminate\Testing\TestResponse. Compared to the project where the method cannot be located, it is missing \Foundation
Figure 4
Plaintext

PS E:\wwwroot\lighthouse-tutorial> ./vendor/bin/phpunit --process-isolation .\tests\Feature\PostsGraphQlApiTest.php
PHPUnit 9.5.11 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 00:07.704, Memory: 12.00 MB

There was 1 error:

1) Tests\Feature\PostsGraphQlApiTest::testQueriesPosts
PHPUnit\Framework\Exception: 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, 17 Feb 2022 06:25:24 GMT
                                )

                            [content-type] => Array
                                (
                                    [0] => application/json
                                )

                        )

                    [cacheControl:protected] => Array
                        (
                        )

                )


5. Reference URL: https://www.shuijingwanwq.com/2022/01/26/5827/ . I suspect it is similar to this issue. In Laravel Framework 6.20.44 and nuwave/lighthouse v5.36.0, this is caused by a version mismatch. In the other project, the versions are Laravel Framework 8.78.1 and nuwave/lighthouse v5.35.0, which are matching.

6. I plan to roll back nuwave/lighthouse v5.36.0 to ~4.10.1 in the current project. Run the composer remove nuwave/lighthouse command to uninstall nuwave/lighthouse. Run the composer require nuwave/lighthouse:~4.10.1 command to install nuwave/lighthouse. After rolling back to nuwave/lighthouse v4.10.2, autocompletion works. See Figure 5.

I plan to roll back nuwave/lighthouse v5.36.0 to ~4.10.1 in the current project. Run the composer remove nuwave/lighthouse command to uninstall nuwave/lighthouse. Run the composer require nuwave/lighthouse:~4.10.1 command to install nuwave/lighthouse. After rolling back to nuwave/lighthouse v4.10.2, autocompletion works
Figure 5

7. Clicking to jump to it shows the file path is: \Illuminate\Foundation\Testing\TestResponse. See Figure 6.

Clicking to jump to it shows the file path is: \Illuminate\Foundation\Testing\TestResponse
Figure 6

8. Searching for \Illuminate\Testing\TestResponse in the Issues confirms that someone else has encountered a similar problem: Refine test helper return types for static analysis. See Figure 7.

Searching for \Illuminate\Testing\TestResponse in the Issues confirms that someone else has encountered a similar problem: Refine test helper return types for static analysis.
Figure 7

9. Because Laravel moved TestResponse in v7. The official recommendation is to use: https://github.com/nuwave/lighthouse/releases/tag/v4.12.2 . It works perfectly with Laravel 7 and is still compatible with Laravel 6. See Figure 8.

Because Laravel moved TestResponse in v7. The official recommendation is to use: https://github.com/nuwave/lighthouse/releases/tag/v4.12.2 . It works perfectly with Laravel 7 and is still compatible with Laravel 6
Figure 8

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系