Created
May 11, 2018 18:35
-
-
Save josecanhelp/d3858b79e0f98e8d229ceb843df7661d to your computer and use it in GitHub Desktop.
Duskunit 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 Illuminate\Console\Command; | |
use Symfony\Component\Finder\Finder; | |
class DuskunitCommand extends Command | |
{ | |
protected $signature = 'duskunit {--down}'; | |
protected $description = 'Toggles the application in and out of Dusk mode without running dusk tests.'; | |
public function handle() | |
{ | |
$this->purgeScreenshots(); | |
$this->purgeConsoleLogs(); | |
if ($this->option('down')) { | |
return $this->tearDownDusk(); | |
} | |
return $this->setUpDusk(); | |
} | |
protected function setUpDusk() | |
{ | |
if ($this->backupFileDoesNotExist() && $this->duskFileExists() && $this->duskEnvironmentIsNotSet()) { | |
$this->backupEnvironment(); | |
} | |
$this->setConfigurationFile(); | |
$this->info('Application is in DUSK MODE.'); | |
} | |
protected function tearDownDusk() | |
{ | |
if ($this->duskFileExists() && $this->backupFileExists()) { | |
$this->restoreEnvironment(); | |
} | |
$this->info('Application is in NORMAL MODE.'); | |
} | |
protected function purgeScreenshots() | |
{ | |
$files = Finder::create()->files() | |
->in(base_path('tests/Browser/screenshots')) | |
->name('failure-*'); | |
foreach ($files as $file) { | |
@unlink($file->getRealPath()); | |
} | |
} | |
protected function purgeConsoleLogs() | |
{ | |
$files = Finder::create()->files() | |
->in(base_path('tests/Browser/console')) | |
->name('*.log'); | |
foreach ($files as $file) { | |
@unlink($file->getRealPath()); | |
} | |
} | |
protected function backupEnvironment() | |
{ | |
copy(base_path('.env'), base_path('.env.backup')); | |
copy(base_path($this->duskFile()), base_path('.env')); | |
} | |
protected function restoreEnvironment() | |
{ | |
copy(base_path('.env.backup'), base_path('.env')); | |
unlink(base_path('.env.backup')); | |
} | |
protected function setConfigurationFile() | |
{ | |
if ($this->duskConfigurationFileDoesNotExist()) { | |
if ($this->phpunitConfigurationFileDoesNotExist()) { | |
$this->warn('Dusk requires an xml config file.'); | |
} else { | |
copy(realpath(__DIR__ . '/phpunit.xml'), base_path('phpunit.dusk.xml')); | |
} | |
} | |
} | |
protected function backupFileExists() | |
{ | |
return file_exists(base_path('.env.backup')); | |
} | |
protected function backupFileDoesNotExist() | |
{ | |
return !$this->backupFileExists(); | |
} | |
protected function duskFileExists() | |
{ | |
return file_exists(base_path($this->duskFile())); | |
} | |
protected function duskEnvironmentIsSet() | |
{ | |
return file_get_contents(base_path('.env')) === file_get_contents(base_path($this->duskFile())); | |
} | |
protected function duskEnvironmentIsNotSet() | |
{ | |
return !$this->duskEnvironmentIsSet(); | |
} | |
protected function duskConfigurationFileExists() | |
{ | |
return file_exists(base_path('phpunit.dusk.xml')); | |
} | |
protected function duskConfigurationFileDoesNotExist() | |
{ | |
return !$this->duskConfigurationFileExists(); | |
} | |
protected function phpunitConfigurationFileExists() | |
{ | |
return file_exists(base_path('phpunit.xml')); | |
} | |
protected function phpunitConfigurationFileDoesNotExist() | |
{ | |
return !$this->phpunitConfigurationFileExists(); | |
} | |
protected function duskFile() | |
{ | |
if (file_exists(base_path($file = '.env.dusk.' . $this->laravel->environment()))) { | |
return $file; | |
} | |
return '.env.dusk'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment