octabevi's avatar

Execute php artisan test from Endpoint

Hello, i want to execute the command "php artisan test" in a endpoint of laravel, i created the Command:

<?php 
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class RunTests extends Command
{
   protected $signature = 'run:test';

   protected $description = 'Run tests and display output';

   public function handle()
   {
       Artisan::call('test');
   }
}

I added to the kernel.php:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\RunTests::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();
        // $schedule->command('cache:prune-stale-tags')->hourly();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

And later created the endpoint:

Route::get('/test', function(){
    Artisan::call('run:test');
});

But this don't work

0 likes
6 replies
octabevi's avatar

@shariff Yes i have the error: "Undefined array key "argv" in the "Artisan::call('test');" code

johndivam's avatar

remove this line

  public function handle()
  {
      Artisan::call('test'); //remove this line , write direct your code 
  }
octabevi's avatar

@jonhyknid I want to have PEST classes in the folder and call them by using a command. The problem it's that i can not execute artisan commands in production, i wanted to have an endpoint with the test y could do a seguiment

Snapey's avatar

why bother with a command to call another artisan command?

Just call test in the routes file?

octabevi's avatar

@Snapey I tryed to execute Artisan::call('test') for execute php artisan test but does not work

Please or to participate in this conversation.