Skip to content

Instantly share code, notes, and snippets.

@deleugpn
Last active April 27, 2024 15:04
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deleugpn/6114f475e09c71f7fa6fa8290b222ccb to your computer and use it in GitHub Desktop.
Save deleugpn/6114f475e09c71f7fa6fa8290b222ccb to your computer and use it in GitHub Desktop.
Run php artisan serve before running php artisan dusk in a single console command.
<?php
namespace App\Console\Commands;
use RuntimeException;
use Laravel\Dusk\Console\DuskCommand;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;
class DuskServeCommand extends DuskCommand {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dusk:serve';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Serve the application and run Dusk tests';
/**
* @var Process
*/
protected $serve;
/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
// Snippets copied from DuskCommand::handle()
$this->purgeScreenshots();
$this->purgeConsoleLogs();
return $this->withDuskEnvironment(function () {
// Start the Web Server AFTER Dusk handled the environment, but before running PHPUnit
$serve = $this->serve();
// Run PHP Unit
return $this->runPhpunit();
});
}
/**
* Snippet copied from DuskCommand::handle() to actually run PHP Unit
*
* @return int
*/
protected function runPhpunit() {
$options = array_slice($_SERVER['argv'], 2);
$process = (new ProcessBuilder())
->setTimeout(null)
->setPrefix($this->binary())
->setArguments($this->phpunitArguments($options))
->getProcess();
try {
$process->setTty(true);
} catch (RuntimeException $e) {
$this->output->writeln('Warning: ' . $e->getMessage());
}
return $process->run(function ($type, $line) {
$this->output->write($line);
});
}
/**
* Build a process to run php artisan serve
*
* @return Process
*/
protected function serve() {
// Compatibility with Windows and Linux environment
$arguments = [PHP_BINARY, 'artisan', 'serve'];
// Build the process
$serve = (new ProcessBuilder($arguments))
->setTimeout(null)
->getProcess();
return tap($serve, function (Process $serve) {
$serve->start(function ($type, $line) {
$this->output->writeln($line);
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment