Execute PHP artisan test from Controller/Endpoint

166 views Asked by At

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, i tryed to create a command, execute with "exec" function and nothing works, i want to execute all the test that i have calling one endpoint of my routing.

1

There are 1 answers

3
Ali Raza On

Why don't you call php artisan test directly from the route file and print output ?

Route::get('/test', function(){
    // here call test directly and return or dd output
    Artisan::call('test');
    // dd(Artisan::output())
    return Artisan::output();
});