File: D:/HostingSpaces/SBogers10/zipwire.komma.pro/app/KommaApp/Shop/TestsController.php
<?php
namespace App\KommaApp\Shop;
/**
* Runs unit / feature / browser tests for the shop
*
* Class TestsController
* @package App\KommaApp\Shop\
*/
class TestsController implements TestsControllerInterface
{
/**
* Folder where the Unit, Feature, test folders and Reports folder is stored
* @var string $testsFolder
*/
private $testsFolder;
/**
* Folder where the browser screenshots are stored
*/
private $screenshotFolder;
/**
* @var string The directory this file resides in relative to the root of this project.
*/
private $baseDirectory;
/**
* @var bool Wheter or not to show more debugging information about phpunit/dusk
*/
private $debug;
public function __construct()
{
$this->baseDirectory = lcfirst(str_replace('\\', DIRECTORY_SEPARATOR,__NAMESPACE__)).DIRECTORY_SEPARATOR;
$this->testsFolder = $this->baseDirectory.'Tests'.DIRECTORY_SEPARATOR;
$this->screenshotFolder = $this->baseDirectory.'Tests'.DIRECTORY_SEPARATOR.'Browser'.DIRECTORY_SEPARATOR.'screenshots'.DIRECTORY_SEPARATOR;
$this->debug = true;
}
public function showBanner($text = 'testing')
{
echo '######################################################'.PHP_EOL;
echo '## '.$text.PHP_EOL;
echo '######################################################'.PHP_EOL.PHP_EOL;
}
public function phpunit(string $filterOrGroup = null, string $filterOrGroupValue = null)
{
$this->showBanner('Starting unit test...');
if($this->debug)
{
$phpUnit = 'php -d display_errors=On -d memory_limit=768M '.base_path('vendor/bin/phpunit');
} else {
$phpUnit = base_path('vendor/bin/phpunit');
}
$parameters = [];
$parameters['-c'] = base_path($this->baseDirectory.'phpunit.xml');
$parameters['--bootstrap'] = base_path('bootstrap/autoload.php');
// $parameters['--coverage-clover'] = $this->testsFolder.'reports'.DIRECTORY_SEPARATOR.'clover';
// $parameters['--coverage-html'] = $this->testsFolder.'reports'.DIRECTORY_SEPARATOR.'html';
if($filterOrGroupValue != "" && ($filterOrGroup == "--filter" || $filterOrGroup == "--group")) $parameters[$filterOrGroup] = $filterOrGroupValue;
$parameterstring = "";
foreach($parameters as $key => $parameter) $parameterstring .= " ".$key." ".$parameter;
if($this->debug) {
$command = $phpUnit.' --debug '.$parameterstring;
} else {
$command = $phpUnit.$parameterstring;
}
$lines = [];
if($this->debug)
{
$lines[] = $command;
$lines[] = "";
}
exec($command, $lines);
$this->echoResult($lines);
}
public function clearBrowserScreenshots()
{
$this->showBanner('Clearing old browser test screenshots');
if(!file_exists($this->screenshotFolder)) return;
$images = glob($this->screenshotFolder."*.png");
foreach ($images as $image)
{
unlink($image);
}
}
public function dusk(string $filterOrGroup = null, string $filterOrGroupValue = null)
{
$this->clearBrowserScreenshots();
$this->showBanner('Starting browser test...');
if($this->debug)
{
$phpUnit = 'php -d display_errors=On -d memory_limit=768M '.base_path('vendor/bin/phpunit');
} else {
$phpUnit = base_path('vendor/bin/phpunit');
}
$parameters = [];
$parameters['-c'] = base_path($this->baseDirectory.'phpunit.dusk.xml');
$parameters['--bootstrap'] = base_path('bootstrap/autoload.php');
// $parameters['--coverage-clover'] = $this->testsFolder.'reports'.DIRECTORY_SEPARATOR.'clover';
// $parameters['--coverage-html'] = $this->testsFolder.'reports'.DIRECTORY_SEPARATOR.'html';
if($filterOrGroupValue != "" && ($filterOrGroup == "--filter" || $filterOrGroup == "--group")) $parameters[$filterOrGroup] = $filterOrGroupValue;
$parameterstring = "";
foreach($parameters as $key => $parameter) $parameterstring .= " ".$key." ".$parameter;
if($this->debug) {
$command = $phpUnit.' --debug '.$parameterstring;
} else {
$command = $phpUnit.$parameterstring;
}
$lines = [];
exec($command, $lines);
$this->echoResult($lines);
}
/**
* Echos exec command result
*
* @param array $result
*/
private function echoResult(array $result)
{
foreach($result as $line) echo $line.PHP_EOL;
}
}