Laravel包测试报错:str_ends_with参数类型不符问题排查
模块包测试报错排查求助
背景
我基于改造后的Spatie/laravel-package-skeleton搭建系统,自定义了ModuleServiceProvider和Module类。因模块系统相关资源不足,实现可能存在问题。
通常开发包需保证抽象独立,但我的包需要直接关联项目的app与src目录下的类,因此在包的composer.json中配置了PSR-4自动加载,内容如下:
"autoload": { "psr-4": { "App\\": "../../../app/", "Src\\": "../../../src/", "Src\\core\\": "../../../src/core/", "Src\\core\\database\\": "../../../src/core/database/", "Src\\core\\database\\factories\\": "../../../src/core/database/factories/", "Src\\core\\database\\migrations\\": "../../../src/core/database/migrations/", "Src\\core\\action\\": "../../../src/core/action/", "Src\\core\\component\\": "../../../src/core/component/", "Src\\core\\hook\\": "../../../src/core/hook/", "Src\\core\\module\\": "../../../src/core/module/", "AlexisVS\\MultipassTestingModule\\": "src/", "AlexisVS\\MultipassTestingModule\\Database\\Factories\\": "database/factories/", "AlexisVS\\MultipassTestingModule\\Database\\Migrations\\": "database/migrations/", "AlexisVS\\MultipassTestingModule\\Tests\\": "tests/" } },
虽然配置看起来奇怪,但能正常加载类。
测试报错
使用Pest和Orchestra/testbench运行测试时出现如下错误:
FAILED Tests\ArchTest > it will not use debugging functions TypeError str_ends_with(): Argument #1 ($haystack) must be of type string, array given at vendor/laravel/framework/src/Illuminate/Support/Str.php:301 297▕ $needles = (array) $needles; 298▕ } 299▕ 300▕ foreach ($needles as $needle) { ➜ 301▕ if ((string) $needle !== '' && str_ends_with($haystack, $needle)) { 302▕ return true; 303▕ } 304▕ } 305▕ 1 vendor/laravel/framework/src/Illuminate/Support/Str.php:301 2 vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:124 3 vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:92 4 vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1820 5 vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1786 6 vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1578 7 vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1497 8 vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1533 9 vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1486 10 vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2335 11 vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2347 12 /var/www/html/src/core/module/AbstractModuleClass.php:67 13 /var/www/html/src/core/module/ModuleServiceProvider.php:42 14 vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36 15 vendor/laravel/framework/src/Illuminate/Container/Util.php:41 16 vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93 17 vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:35 18 vendor/laravel/framework/src/Illuminate/Container/Container.php:661 19 vendor/laravel/framework/src/Illuminate/Foundation/Application.php:1006 20 vendor/laravel/framework/src/Illuminate/Foundation/Application.php:789 21 vendor/laravel/framework/src/Illuminate/Foundation/Application.php:898 22 vendor/laravel/framework/src/Illuminate/Foundation/Application.php:878 23 vendor/laravel/framework/src/Illuminate/Foundation/Application.php:854 24 vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:431 25 vendor/orchestra/testbench-core/src/Concerns/CreatesApplication.php:369 26 vendor/orchestra/testbench-core/src/Concerns/CreatesApplication.php:230 27 vendor/orchestra/testbench-core/src/TestCase.php:85 28 vendor/orchestra/testbench-core/src/Concerns/Testing.php:88 29 vendor/orchestra/testbench-core/src/TestCase.php:52 30 tests/TestCase.php:32 31 vendor/pestphp/pest/src/Concerns/Testable.php:182 32 vendor/pestphp/pest/src/Kernel.php:86 33 vendor/pestphp/pest/bin/pest:91 34 vendor/pestphp/pest/bin/pest:99
环境与困惑
我的环境为Laravel 10、PHP 8.2、Laravel Sail,测试时进入包目录安装依赖后执行composer run test。我认为问题根源不在报错栈表面,不知如何进一步调试。
内容的提问来源于stack exchange,提问作者Alexis Van San
相关产品推荐
相关产品推荐

