编写 Lighthouse 5 的自动化测试用例时,PhpStorm 不能够自动完成(无法找到要转到的声明)的解决

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

图1

2、打印 $response 对象,其实际路径为:\Illuminate\Foundation\Testing\TestResponse。如图2

图2

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、在另一个项目中,PhpStorm 能够自动完成。如图3

图3

4、在另一个项目中,打印 $response 对象,其实际路径为:\Illuminate\Testing\TestResponse。相对于无法定位方法的项目,缺少了 \Foundation。如图4

图4

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、参考网址:https://www.shuijingwanwq.com/2022/01/26/5827/ ,怀疑与此问题类似。在 Laravel Framework 6.20.44、nuwave/lighthouse v5.36.0 中,版本不匹配所导致。因为在另一个项目中,版本分别为:Laravel Framework 8.78.1、nuwave/lighthouse v5.35.0,版本是匹配的。

6、计划在当前项目中,将 nuwave/lighthouse v5.36.0 回退至 ~4.10.1。执行 composer remove nuwave/lighthouse 命令,卸载 nuwave/lighthouse。执行 composer require nuwave/lighthouse:~4.10.1 命令,安装 nuwave/lighthouse。回退至 nuwave/lighthouse v4.10.2 后,已经能够自动完成。如图5

图5

7、点击跳转,其文件路径为:\Illuminate\Foundation\Testing\TestResponse。如图6

图6

8、在 Issues 中搜索:\Illuminate\Testing\TestResponse,确认已经有人遇见过类似的问题:Refine test helper return types for static analysis。如图7

图7

9、因为 Laravel 在 v7 中移动了 TestResponse。官方建议使用:https://github.com/nuwave/lighthouse/releases/tag/v4.12.2 。可与 Laravel 7 完美配合。仍然与 Laravel 6 兼容。如图8

图8

永夜