File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/QualityAssurance/TestsController.php
<?php
namespace App\Komma\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 = base_path().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;
}
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'] = $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 kms: '.$command;
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'] = $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 kms: '.$command;
exec($command, $lines);
$this->echoResult($lines);
}
public function phpbench(string $filterOrGroup = null, string $filterOrGroupValue = null)
{
$this->showBanner('Starting shop stress tests / benchmarks...');
$phpbench = base_path('vendor/bin/phpbench');
$parameters = [];
$parameters['--config'] = $this->baseDirectory.'phpbench_config.json';
$parameters['--report'] = 'kms';
if ($filterOrGroupValue != '' && ($filterOrGroup == '--filter' || $filterOrGroup == '--group')) {
$parameters[$filterOrGroup] = $filterOrGroupValue;
}
$parameterstring = ' ';
foreach ($parameters as $key => $parameter) {
$parameterstring .= $key.'='.$parameter.' ';
}
$command = $phpbench.' run'.$parameterstring.'tests/Benchmark';
$lines = [];
$lines[] = 'Running command to test kms: '.$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;
}
}
}