Last active
April 27, 2024 15:04
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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