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

Splitting a Test Method Into Two Causes an Error When Writing Automated Tests for Lighthouse 5

运行测试,报错:Error: Call to undefined method Illuminate\Support\Facades\Config::set()

1. When writing automated test cases for Lighthouse 5, I split one test method into two. At this stage, it is a single method.

PHP

    public function testGetThemeById(): void
    {
        $response = $this->graphQL('
            query GetThemeById($id: ID!) {
                onlineStoreTheme(themeId: $id) {
                    id
                    editable
                    createdAt
                    name
                    themeAssets {
                        id
                        themeId
                        version
                        key
                        mimeType
                        category
                        schema
                        createdAt
                        updatedAt
                    }
                }
            }
            ',
            [
                'id' => 'vogue',
            ]
        );

        $response->assertJson(
            [
                'data' => [
                    'onlineStoreTheme' => [
                        'id' => 'vogue'
                    ],
                ],
            ]
        )->assertJsonStructure([
                'data' => [
                    'onlineStoreTheme' => [
                        'themeAssets' => [
                            [
                                'id',
                                'themeId',
                                'version',
                                'key',
                                'mimeType',
                                'category',
                                'schema',
                                'createdAt',
                                'updatedAt',
                            ]
                        ]
                    ]
                ],
            ]
        );
    }


2. Split into two methods.

PHP
<?php
 
namespace Modules\ThemeStore\Tests\Functional\GraphQl;
 
use Nuwave\Lighthouse\Testing\MakesGraphQLRequests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Nuwave\Lighthouse\Testing\ClearsSchemaCache;
use Tests\CreatesApplication;
 
class OnlineStoreThemeGraphQlApiTest extends BaseTestCase
{
    use CreatesApplication,
        ClearsSchemaCache,
        MakesGraphQLRequests;
 
    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'
                    ],
                ],
            ]
        );
    }
 
    public function testGetThemeAssetsById(): void
    {
        $response = $this->graphQL('
            query GetThemeAssetsById($id: ID!) {
                onlineStoreTheme(themeId: $id) {
                    themeAssets {
                        id
                        themeId
                        version
                        key
                        mimeType
                        category
                        schema
                        createdAt
                        updatedAt
                    }
                }
            }
            ',
            [
                'id' => 'vogue',
            ]
        );
 
        $response->assertJsonStructure(
            [
                'data' => [
                    'onlineStoreTheme' => [
                        'themeAssets' => [
                            [
                                'id',
                                'themeId',
                                'version',
                                'key',
                                'mimeType',
                                'category',
                                'schema',
                                'createdAt',
                                'updatedAt',
                            ]
                        ]
                    ]
                ],
            ]
        );
    }
 
    protected function setUp(): void
    {
        parent::setUp();
 
        $this->bootClearsSchemaCache();
    }
}
 

3. Run the test, and an error is reported: Error: Call to undefined method Illuminate\Support\Facades\Config::set(). As shown in Figure 1.

Run the test, and an error is reported: Error: Call to undefined method Illuminate\Support\Facades\Config::set()
Figure 1
Plaintext

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

.E                                                                  2 / 2 (100%)

Time: 1.02 seconds, Memory: 70.00 MB

There was 1 error:

1) Modules\ThemeStore\Tests\Functional\GraphQl\OnlineStoreThemeGraphQlApiTest::testGetThemeAssetsById
Error: Call to undefined method Illuminate\Support\Facades\Config::set()

E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Support\ServiceProvider.php:63
E:\wwwroot\object\Modules\ThemeSetting\Providers\ThemeServiceProvider.php:103
E:\wwwroot\object\Modules\ThemeSetting\Providers\ThemeServiceProvider.php:37
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:616
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php:75
E:\wwwroot\object\vendor\nwidart\laravel-modules\src\Laravel\Module.php:33
E:\wwwroot\object\vendor\nwidart\laravel-modules\src\Module.php:264
E:\wwwroot\object\vendor\nwidart\laravel-modules\src\FileRepository.php:321
E:\wwwroot\object\vendor\nwidart\laravel-modules\src\Providers\BootstrapServiceProvider.php:23
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:616
E:\wwwroot\object\vendor\nwidart\laravel-modules\src\ModulesServiceProvider.php:31
E:\wwwroot\object\vendor\nwidart\laravel-modules\src\LaravelModulesServiceProvider.php:17
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:36
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\Util.php:37
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:93
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:37
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\Container.php:590
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:856
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:839
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:840
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\BootProviders.php:17
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:219
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php:320
E:\wwwroot\object\tests\CreatesApplication.php:18
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestCase.php:102
E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestCase.php:81
E:\wwwroot\object\Modules\ThemeStore\Tests\Functional\GraphQl\OnlineStoreThemeGraphQlApiTest.php:74
phpvfscomposer://E:\wwwroot\object\vendor\phpunit\phpunit\phpunit:60

ERRORS!
Tests: 2, Assertions: 1, Errors: 1.
PS E:\wwwroot\object>

4. At this point, if I remove either of the two methods, the test passes.

XML
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="true"
         stopOnFailure="false">
</phpunit>





5. I printed $this->app[‘config’] on the first line of both methods respectively. I confirmed that only the first method prints successfully, while the second method throws an error: Error: Call to undefined method Illuminate\Support\Facades\Config::set(). This confirms that in the second method, the application instance no longer exists.

6. Reference URL: https://phpunit.readthedocs.io/en/9.5/configuration.html . The processIsolation attribute. This attribute configures whether each test should be run in a separate PHP process for increased isolation. Possible values: true or false (default: false). This attribute configures whether each test should be run in a separate PHP process for increased isolation.

7. Edit the file /phpunit.xml and set processIsolation=”true”. As shown in Figure 2.

Edit the file /phpunit.xml and set processIsolation="true"
Figure 2

8. Run the test again, and it passes. As shown in Figure 3.

Run the test again, and it passes
Figure 3

9. If processIsolation=”true” is not set, you can add the –process-isolation option when running the test. The test will still pass. As shown in Figure 4.

If processIsolation="true" is not set, you can add the --process-isolation option when running the test. The test will still pass
Figure 4
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.

..                                                                  2 / 2 (100%)

Time: 2.56 seconds, Memory: 16.00 MB

OK (2 tests, 14 assertions)
PS E:\wwwroot\object>

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

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

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

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

关于我  ·  GitHub  ·  邮件联系