You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行php artisan test报错:JsonResponse无法转换为int

解决Laravel 9执行php artisan test时的类型转换错误

问题场景

执行php artisan test -vvv运行测试套件时触发类型转换错误,当前仅保留默认的tests/Feature/ExampleTest.php和tests/Unit/ExampleTest.php示例测试,且刚完成Laravel 9版本升级。

错误信息

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()

项目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
}

解决方案

  • 检查自定义Console命令返回值:Laravel要求Console命令的handle方法必须返回整数(0表示执行成功,非0表示执行失败)。如果自定义命令返回了JsonResponse或其他非整数类型,会触发该类型转换错误。
  • 排查命令注册:打开app/Console/Kernel.php,查看commands方法中注册的所有命令,重点检查自定义命令的返回值是否符合要求。
  • 临时定位问题:先注释掉自定义命令的注册代码,再运行php artisan test。若错误消失,即可确定是该自定义命令的问题,修改其返回值为整数即可。
  • 检查Helper文件:确认app/Helpers/helpers.php中没有修改Laravel核心命令逻辑,也没有定义干扰Console命令执行的全局函数。
  • 清理缓存:执行以下命令清理缓存,避免旧代码影响:
    php artisan cache:clear
    composer dump-autoload
    

内容的提问来源于stack exchange,提问作者Ian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 07:55:00