我目前正在尝试运行命令php artisan test
,该命令应该运行测试套件,但出现以下错误
user@earth:/var/www/project/api$ art test -vvv
ErrorException
Object of class Illuminate\Http\JsonResponse could not be converted to int
at vendor/laravel/framework/src/Illuminate/Console/Command.php:136
132▕ protected function execute(InputInterface $input, OutputInterface $output)
133▕ {
134▕ $method = method_exists($this, 'handle') ? 'handle' : '__invoke';
135▕
➜ 136▕ return (int) $this->laravel->call([$this, $method]);
137▕ }
138▕
139▕ /**
140▕ * Resolve the console command instance for the given command.
1 vendor/laravel/framework/src/Illuminate/Console/Command.php:136
Illuminate\Foundation\Bootstrap\HandleExceptions::Illuminate\Foundation\Bootstrap\{closure}()
2 vendor/symfony/console/Command/Command.php:312
Illuminate\Console\Command::execute()
3 vendor/laravel/framework/src/Illuminate/Console/Command.php:120
Symfony\Component\Console\Command\Command::run()
4 vendor/symfony/console/Application.php:1022
Illuminate\Console\Command::run()
5 vendor/symfony/console/Application.php:314
Symfony\Component\Console\Application::doRunCommand()
6 vendor/symfony/console/Application.php:168
Symfony\Component\Console\Application::doRun()
7 vendor/laravel/framework/src/Illuminate/Console/Application.php:102
Symfony\Component\Console\Application::run()
8 vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:129
Illuminate\Console\Application::run()
9 artisan:35
Illuminate\Foundation\Console\Kernel::handle()
鉴于错误是 Laravel 内部的,并且堆栈跟踪全部指向 Laravel 供应商目录,您如何修复此类问题?我目前只有两个示例测试,分别是 tests/Feature/ExampleTest.php
和 tests/Unit/ExampleTest.php
。
我最近升级到了 Laravel 9,但我在升级说明中看不到与此相关的任何内容
我的composer.json
如下
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"type": "project",
"require": {
"php": "^8.1.4",
"aws/aws-sdk-php": "^3.258",
"cmgmyr/messenger": "^2.16",
"creativeorange/gravatar": "~1.0",
"doctrine/dbal": "^2.9",
"fruitcake/laravel-cors": "^3.0",
"guzzlehttp/guzzle": "^7.4",
"intervention/image": "^2.7",
"laracasts/flash": "^3.0",
"laravel/framework": "v9.9.0",
"laravel/helpers": "^1.5",
"laravel/tinker": "^2.7.2",
"laravel/ui": "^3.4",
"laravelcollective/html": "^6.3.0",
"mews/purifier": "^3.3.7",
"nopjmp/discord-webhooks": "^0.3.1",
"predis/predis": "^1.1",
"sentry/sentry-laravel": "^2.12.0",
"spatie/laravel-permission": "^5.5",
"spatie/laravel-sluggable": "^3.4",
"tymon/jwt-auth": "1.*"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.6",
"barryvdh/laravel-ide-helper": "^2.5",
"fakerphp/faker": "^1.9.1",
"laravel/sail": "^1.0.1",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^6.1",
"pestphp/pest-plugin-laravel": "^1.2",
"phpunit/phpunit": "^9.5.10",
"spatie/laravel-ignition": "^1.0"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/Helpers/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": [
]
}
},
"scripts": {
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan ide-helper:generate",
"php artisan ide-helper:meta"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"php-http/discovery": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
最佳答案
代码似乎不是 Laravel 9 的。尝试删除 Vendor 文件夹并删除composer.lock,然后再次运行composer install。
以下是Laravel 9中execute()方法的代码:
/**
* Execute the console command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($this instanceof Isolatable && $this->option('isolated') !== false &&
! $this->commandIsolationMutex()->create($this)) {
$this->comment(sprintf(
'The [%s] command is already running.', $this->getName()
));
return (int) (is_numeric($this->option('isolated'))
? $this->option('isolated')
: self::SUCCESS);
}
$method = method_exists($this, 'handle') ? 'handle' : '__invoke';
try {
return (int) $this->laravel->call([$this, $method]);
} finally {
if ($this instanceof Isolatable && $this->option('isolated') !== false) {
$this->commandIsolationMutex()->forget($this);
}
}
}
关于php - 无法运行 php artisan 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76158542/