File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Shop/QualityAssurance/TestsController.php
<?php
namespace App\Komma\Shop\QualityAssurance;
/**
* Runs unit / feature / browser tests for the shop
*
* Class TestsController
*/
class TestsController implements TestsControllerInterface
{
/**
* Folder where the Unit, Feature, test folders and Reports folder is stored
* @var string
*/
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')
{
$horizontalBorderWidth = 55;
$horizontalBorderSymbol = '#';
$verticalBorderSymbol = '#';
$borderThickness = 1;
$horizontalBorder = str_repeat($horizontalBorderSymbol, $horizontalBorderWidth);
$decoratedText = str_repeat($verticalBorderSymbol, $borderThickness).
str_pad($text, $horizontalBorderWidth - ($borderThickness * 2), ' ', STR_PAD_BOTH).
str_repeat($verticalBorderSymbol, $borderThickness);
for ($index = 0; $index < $borderThickness; $index++) {
echo $horizontalBorder.PHP_EOL;
}
echo $decoratedText.PHP_EOL;
for ($index = 0; $index < $borderThickness; $index++) {
echo $horizontalBorder.PHP_EOL;
}
echo PHP_EOL;
}
/**
* @return bool
*/
public function checkIfShopExistsAndEnabled()
{
$isShop = \Config::get('app.isShop');
$shopPath = base_path('app'.DIRECTORY_SEPARATOR.'Komma'.DIRECTORY_SEPARATOR.'Shop');
$hasShopInstallation = file_exists($shopPath);
$shopEnabled = ($hasShopInstallation && $isShop);
if (! $shopEnabled) {
$this->echoResult([
'TestsController: The shop is not enabled or present. Please check if the IS_SHOP env value is true and check if the following directory exists: '.$shopPath,
]);
}
return $shopEnabled;
}
public function phpunit(string $filterOrGroup = null, string $filterOrGroupValue = null)
{
if (! $this->checkIfShopExistsAndEnabled()) {
return;
}
$this->showBanner('Starting shop 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[] = '';
}
$lines[] = 'Running command to test shop: '.$command;
exec($command, $lines);
$this->echoResult($lines);
}
public function clearBrowserScreenshots()
{
$this->showBanner('Clearing old shop 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)
{
if (! $this->checkIfShopExistsAndEnabled()) {
return;
}
$this->clearBrowserScreenshots();
$this->showBanner('Starting shop 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 = [];
$lines[] = 'Running command to test shop: '.$command;
exec($command, $lines);
$this->echoResult($lines);
}
public function phpbench(string $filterOrGroup = null, string $filterOrGroupValue = null)
{
if (! $this->checkIfShopExistsAndEnabled()) {
return;
}
$this->showBanner('Starting shop stress tests / benchmarks...');
$phpbench = base_path('vendor/bin/phpbench');
$parameters = [];
$parameters['--config'] = '.'.DIRECTORY_SEPARATOR.$this->baseDirectory.'phpbench_config.json';
$parameters['--report'] = 'shop';
if ($filterOrGroupValue != '' && ($filterOrGroup == '--filter' || $filterOrGroup == '--group')) {
$parameters[$filterOrGroup] = $filterOrGroupValue;
}
$parameterstring = ' ';
foreach ($parameters as $key => $parameter) {
$parameterstring .= $key.'='.$parameter.' ';
}
$command = $phpbench.' run'.$parameterstring.'app/Komma/Shop/tests/Benchmark';
$lines = [];
$lines[] = 'Running command to test shop: '.$command;
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;
}
}
}