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

编写 Lighthouse 5 的自动化测试用例时,将一个文件中的测试方法拆分为二个测试方法,报错:Error: Call to undefined method Illuminate\Support\Facades\Config::set()

运行测试,报错:Error: Call to undefined method Illuminate\Support\Facades\Config::set()
1、编写 Lighthouse 5 的自动化测试用例时,将一个测试方法拆分为二个测试方法。现阶段是一个方法


    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、拆分为二个方法
<pre class="wp-block-syntaxhighlighter-code">

<?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();
    }
}




</pre>
3、运行测试,报错:Error: Call to undefined method Illuminate\Support\Facades\Config::set()。如图1
运行测试,报错:Error: Call to undefined method Illuminate\Support\Facades\Config::set()
图1


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、此时,如果删除掉方法中的其中任意一个方法,皆可以测试通过。
<pre class="wp-block-syntaxhighlighter-code">

<?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>


</pre>
5、分别在二个方法的第一行打印 $this->app[‘config’] ,确认仅有第一个方法打印成功,第二个方法报错:Error: Call to undefined method Illuminate\Support\Facades\Config::set()。可以确认在第二个方法中,应用实例已经不复存在了的。 6、参考网址:https://phpunit.readthedocs.io/en/9.5/configuration.html 。processIsolation 属性。This attribute configures whether each test should be run in a separate PHP process for increased isolation。可能的值:true 或 false(默认值:false)。此属性配置每个测试是否应在单独的 PHP 进程中运行以增加隔离。 7、编辑文件 /phpunit.xml,设置 processIsolation=”true”。如图2
编辑文件 /phpunit.xml,设置 processIsolation="true"
图2
8、再次运行测试,测试通过。如图3
再次运行测试,测试通过
图3
9、如果不设置 processIsolation=”true” 的话,可以在运行测试时添加选项 –process-isolation。仍然可以测试通过。如图4
如果不设置 processIsolation="true" 的话,可以在运行测试时添加选项 --process-isolation。仍然可以测试通过
图4


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>


 

PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理