Search code examples
phplaravellaravel-artisan

Unable to run php artisan test


I am currently trying to run the command php artisan test which should run the test suite however I get the following error

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

Given the error is internal to Laravel and the stacktrace all points to the Laravel vendor directory, how do you go about fixing something like this? I currently only have the 2 example tests that ship tests/Feature/ExampleTest.php and tests/Unit/ExampleTest.php.

I did recently upgrade to Laravel 9 but I couldn't see anything in the upgrade notes relating to this

My composer.json is as follows

{
  "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
}

Solution

  • The code seems not be of Laravel 9. Try deleting Vendor folder and removing composer.lock and then run composer install again.

    The below is code of execute() method in Laravel 9:

     /**
         * 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);
                }
            }
        }