File: D:/HostingSpaces/SBogers10/stafa.komma.pro/app/Komma/Kms/QualityAssurance/DuskCommand.php
<?php
namespace App\Komma\Kms\QualityAssurance;
use Dotenv\Dotenv;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\RuntimeException;
class DuskCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dusk {type : kms, shop}
{--without-tty : Disable output to TTY}
';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run the Dusk tests for the application';
/**
* Indicates if the project has its own PHPUnit configuration.
*
* @var boolean
*/
protected $hasPhpUnitConfiguration = false;
/** @var string */
private $basePath;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->ignoreValidationErrors();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->configureType($this->argument('type'));
$this->purgeScreenshots();
$this->purgeConsoleLogs();
$options = array_slice($_SERVER['argv'], $this->option('without-tty') ? 3 : 2);
// $options = [];
//Remove type (kms / shop) from options
unset($options[0]);
$options = array_values($options);
return $this->withDuskEnvironment(function () use ($options) {
$process = (new Process(array_merge(
$this->binary(), $this->phpunitArguments($options)
)))->setTimeout(null);
try {
$process->setTty(! $this->option('without-tty'));
} catch (RuntimeException $e) {
$this->output->writeln('Warning: '.$e->getMessage());
}
return $process->run(function ($type, $line) {
$this->output->write($line);
});
});
}
/**
* @param string|null $type
*/
protected function configureType(string $type = null): void
{
switch ($type) {
default:
case 'kms':
$this->basePath = base_path('');
break;
case 'shop':
$this->basePath = base_path('app/Komma/Shop');
break;
}
}
/**
* Get the PHP binary to execute.
*
* @return array
*/
protected function binary()
{
return [PHP_BINARY, 'vendor/phpunit/phpunit/phpunit'];
}
/**
* Get the array of arguments for running PHPUnit.
*
* @return array
*/
protected function phpunitArguments($options)
{
$options = array_values(array_filter($options, function ($option) {
return ! Str::startsWith($option, '--env=');
}));
return array_merge(['-c',$this->basePath.DIRECTORY_SEPARATOR.'phpunit.dusk.xml'], $options);
}
/**
* Purge the failure screenshots
*
* @return void
*/
protected function purgeScreenshots()
{
$path = base_path('tests/Browser/screenshots');
if (! is_dir($path)) {
return;
}
$files = Finder::create()->files()
->in($path)
->name('failure-*');
foreach ($files as $file) {
@unlink($file->getRealPath());
}
}
/**
* Purge the console logs.
*
* @return void
*/
protected function purgeConsoleLogs()
{
$path = base_path('tests/Browser/console');
if (! is_dir($path)) {
return;
}
$files = Finder::create()->files()
->in($path)
->name('*.log');
foreach ($files as $file) {
@unlink($file->getRealPath());
}
}
/**
* Run the given callback with the Dusk configuration files.
*
* @param \Closure $callback
* @return mixed
*/
protected function withDuskEnvironment($callback)
{
if (file_exists(base_path($this->duskFile()))) {
if (file_get_contents(base_path('.env')) !== file_get_contents(base_path($this->duskFile()))) {
$this->backupEnvironment();
}
$this->refreshEnvironment();
}
$this->writeConfiguration();
return tap($callback(), function () {
$this->removeConfiguration();
if (file_exists(base_path($this->duskFile())) && file_exists(base_path('.env.backup'))) {
$this->restoreEnvironment();
}
});
}
/**
* Backup the current environment file.
*
* @return void
*/
protected function backupEnvironment()
{
copy(base_path('.env'), base_path('.env.backup'));
copy(base_path($this->duskFile()), base_path('.env'));
}
/**
* Restore the backed-up environment file.
*
* @return void
*/
protected function restoreEnvironment()
{
copy(base_path('.env.backup'), base_path('.env'));
unlink(base_path('.env.backup'));
}
/**
* Refresh the current environment variables.
*
* @return void
*/
protected function refreshEnvironment()
{
(Dotenv::create(base_path()))->overload();
}
/**
* Write the Dusk PHPUnit configuration.
*
* @return void
*/
protected function writeConfiguration()
{
if (! file_exists($file = base_path('phpunit.dusk.xml'))) {
copy(realpath(__DIR__.'/../../stubs/phpunit.xml'), $file);
} else {
$this->hasPhpUnitConfiguration = true;
}
}
/**
* Remove the Dusk PHPUnit configuration.
*
* @return void
*/
protected function removeConfiguration()
{
if (! $this->hasPhpUnitConfiguration) {
unlink(base_path('phpunit.dusk.xml'));
}
}
/**
* Get the name of the Dusk file for the environment.
*
* @return string
*/
protected function duskFile()
{
$file = '.env.testing';
if (!file_exists(base_path($file))) {
throw new \RuntimeException('Please create a .env.testing file in the root of your project. It will be used for dusk');
}
return $file;
}
}