Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Serve the laravel application and run Dusk tests
<?php
namespace App\Console\Commands;
use RuntimeException;
use Laravel\Dusk\Console\DuskCommand;
use Symfony\Component\Process\Process;
class DuskServeCommand extends DuskCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dusk:serve {--host=} {--port=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Serve the application and run Dusk tests';
/**
* @var Process
*/
protected $serve;
/**
* @var string
*/
protected $defaultHost = '127.0.0.1';
/**
* @var string
*/
protected $defaultPort = '8000';
/**
* 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 dusk testing
$this->serve();
// Run dusk testing
return $this->runDusk();
});
}
/**
* Build a process to run php artisan serve
*
* @return Process
*/
protected function serve()
{
// Compatibility with Windows and Linux environment
$arguments = [
PHP_BINARY,
'artisan',
'serve',
'--host=' . ($this->option('host') ?? $this->defaultHost),
'--port=' . ($this->option('port') ?? $this->defaultPort),
];
// Build the process
$serve = (new Process($arguments))->setTimeout(null);
return tap($serve, function (Process $serve) {
$serve->start(function ($type, $line) {
$this->output->writeln($line);
});
});
}
/**
* Snippet copied from DuskCommand::handle() to actually run PHP Unit
*
* @return int
*/
protected function runDusk()
{
$process = (new Process([PHP_BINARY, 'artisan', 'dusk']))->setTimeout(null);
try {
$process->setTty(true);
} catch (RuntimeException $e) {
$this->output->writeln('Warning: '.$e->getMessage());
}
return $process->run(function ($type, $line) {
$this->output->write($line);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.