Skip to content

Commit

Permalink
Reorganize Binary
Browse files Browse the repository at this point in the history
Update codestyle
  • Loading branch information
nohponex committed Jan 19, 2016
1 parent affd270 commit 8961395
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ npm-debug.log
localsettings.php
build/
/bin/*.symbolic.php
.idea/**
215 changes: 131 additions & 84 deletions src/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @version 1.0.0
* @since 1.0.0
* @todo Add time and memory statistics
*/
class Binary
{
/**
* @var GetOptionKit\OptionResult
* Parsed arguments passed to script
* @var \GetOptionKit\OptionResult
*/
protected $arguments;

/**
* @param array $argv Array of arguments passed to script
* Get argument specifications
* @return OptionCollection
*/
public function __construct($argv)
public static function getArgumentSpecifications()
{
$specs = new OptionCollection;
$specs->add('d|dir:', 'Tests directory path')
Expand All @@ -66,7 +69,25 @@ public function __construct($argv)
$specs->add('no-colors', 'No colors')->defaultValue(false);
$specs->add('i|immediate', 'Show error output immediately as it appears')->defaultValue(false);

$parser = new OptionParser($specs);
return $specs;
}

/**
* @param array $argv Array of arguments passed to script
* @example
* ```php
* $binary = new Binary([
* __FILE__,
* '-d',
* './tests/'
* ];
*
* $binary->invoke();
* ```
*/
public function __construct($argv)
{
$parser = new OptionParser(static::getArgumentSpecifications());

$this->arguments = $parser->parse($argv);

Expand All @@ -87,77 +108,31 @@ public function invoke()
if ($arguments->help) {
echo 'Help:' . PHP_EOL;
$printer = new ConsoleOptionPrinter;
echo $printer->render($specs);
echo $printer->render(static::getArgumentSpecifications());
return 0;
} elseif ($arguments->debug) {
echo 'Enabled options: ' . PHP_EOL;

foreach ($arguments as $key => $spec) {
echo $spec . PHP_EOL;
echo $spec;
}
}

$dir = $arguments->dir;

$bootstrapFile = $arguments->bootstrap;

if ($bootstrapFile) {
//Include bootstrap file if set
if (($bootstrapFile = $arguments->bootstrap)) {
require $bootstrapFile;
}

//Get all .json files in directory
$testFiles = array_map(
function ($f) {
return str_replace('//', '/', $f);
},
Util::directoryToArray(
$dir,
true,
false,
true,
'/^\.|\.\.$/',
['json'], //Only .json files
false
)
);

/**
* @var TestParser
*/
$tests = [];

foreach ($testFiles as $filename) {
try {
$testParser = new TestParser($filename);
} catch (\Exception $e) {
$message = sprintf(
'Failed to parse file "%s" %s With message: "%s"',
$filename,
PHP_EOL,
$e->getMessage()
) . PHP_EOL;

if (get_class($e) == IncorrectParametersException::class) {
$message .= PHP_EOL . 'Incorrect:' . PHP_EOL
. json_encode($e->getParameters(), JSON_PRETTY_PRINT) . PHP_EOL;
} elseif (get_class($e) == MissingParametersException::class) {
$message .= PHP_EOL . 'Missing:' . PHP_EOL
. json_encode($e->getParameters(), JSON_PRETTY_PRINT) . PHP_EOL;
}

echo $message;

return 1;
}
$tests[] = $testParser;
try {
$testParserCollection = $this->getTestParserCollection();
} catch(\Exception $e) {
echo $e->getMessage();
return 1;
}

//Sort tests by order
uasort($tests, [self::class, 'sortTests']);

//Statistics object
$stats = (object)[
'tests' => count($tests),
'tests' => count($testParserCollection),
'success' => 0,
'error' => 0,
'ignore' => 0,
Expand All @@ -166,8 +141,7 @@ function ($f) {

$testIndex = 0;

//Execute tests
foreach ($tests as $test) {
foreach ($testParserCollection as $test) {

//Check if subdir argument is set
if (isset($arguments->subdir) && $arguments->subdir !== null) {
Expand All @@ -185,7 +159,7 @@ function ($f) {

$match = false;

//Check if any of the patterns ar matching
//Check if file name matches any of the subdir patterns
foreach ($arguments->subdir as $pattern) {
$pattern = '@' . $pattern . '@';
if (!!preg_match($pattern, $cleanFilename)) {
Expand All @@ -195,6 +169,7 @@ function ($f) {
}

if (!$match) {
//Ignore
$stats->ignore += 1;
if ($arguments->verbose) {
echo sprintf(
Expand Down Expand Up @@ -241,10 +216,11 @@ function ($f) {

//Include number of additional testphase collections
$stats->tests += count($testphaseCollection) - 1;


//Iterate though test parser's testphase collection
foreach ($testphaseCollection as $testphase) {
try {
$ok = $testphase->run(function (
$testphase->run(function (
$responseStatusCode,
$responseHeaders,
$responseBody,
Expand All @@ -253,19 +229,17 @@ function ($f) {
$test,
$arguments
) {
//global $arguments;
//global $test;
//todo move to TestParser
$export = $test->getExport();

//Fetch all teest exports and add them as globals
//Fetch all test exports and add them as globals
foreach ($export as $key => $value) {
$path = explode('.', $value);

$pathValue = $responseBodyObject;

foreach ($path as $p) {
//@todo array index
//@todo implement array index
$arrayIndex = 0;

if (is_array($pathValue)) {
Expand Down Expand Up @@ -301,7 +275,7 @@ function ($f) {
$stats->success += 1;
} catch (\Exception $e) {

//@todo if verbose show more details (trace)
//Error message
$message = $e->getMessage();

if ($arguments->debug) {
Expand Down Expand Up @@ -342,7 +316,7 @@ function ($f) {
$stats->error += 1;
}
++$testIndex;
//Allow only 80 characters per line
//Show only 80 characters per line
if (!($testIndex % 79)) {
echo PHP_EOL;
}
Expand All @@ -356,7 +330,7 @@ function ($f) {
echo Globals::toString() . PHP_EOL;
}

//dont print if immediate is true
//don't print if immediate is true
if (!$arguments->immediate && !empty($stats->errors)) {
echo 'Errors:' . PHP_EOL;
foreach ($stats->errors as $e) {
Expand All @@ -373,13 +347,78 @@ function ($f) {
Binary::output('Unsuccessful: ' . $stats->error . PHP_EOL, 'red');

if ($stats->error > 0) {
return (1);
return 1;
}

return 0;
}

protected static function sortTests($a, $b)
/**
* @return TestParser[]
* @throws \Exception
*/
protected function getTestParserCollection()
{
$dir = $this->arguments->dir;

//Get all .json files in given directory
$testFiles = array_map(
function ($f) {
return str_replace('//', '/', $f);
},
Util::directoryToArray(
$dir,
true,
false,
true,
'/^\.|\.\.$/',
['json'], //Only .json files
false
)
);

/**
* @var TestParser[]
*/
$testParserCollection = [];

foreach ($testFiles as $filename) {
try {
$testParser = new TestParser($filename);
} catch (\Exception $e) {
$message = sprintf(
'Failed to parse file "%s" %s With message: "%s"',
$filename,
PHP_EOL,
$e->getMessage()
) . PHP_EOL;

if (get_class($e) == IncorrectParametersException::class) {
$message .= PHP_EOL . 'Incorrect:' . PHP_EOL
. json_encode($e->getParameters(), JSON_PRETTY_PRINT) . PHP_EOL;
} elseif (get_class($e) == MissingParametersException::class) {
$message .= PHP_EOL . 'Missing:' . PHP_EOL
. json_encode($e->getParameters(), JSON_PRETTY_PRINT) . PHP_EOL;
}

throw new \Exception($message);
}
$testParserCollection[] = $testParser;
}

//Sort tests by order
uasort($testParserCollection, [self::class, 'sortTestParser']);

return $testParserCollection;
}

/**
* Sort TestParsers ascending
* @param \Phramework\Testphase\TestParser $a
* @param \Phramework\Testphase\TestParser $b
* @return int Returns 1 if order of first TestParser is larger
*/
public static function sortTestParser(TestParser $a, TestParser $b)
{
return (
$a->getMeta()->order < $b->getMeta()->order
Expand All @@ -389,15 +428,18 @@ protected static function sortTests($a, $b)
}

/**
* @todo add no-colors
* Returned colored text
* @param string $text
* @param string $color
* @return string
*/
public static function colored($text, $color)
public function colored($text, $color)
{
$colors = [
'black' => '0;30',
'red' => '0;31',
'green' => '0;32',
'blue' => '1;34',
'black' => '0;30',
'red' => '0;31',
'green' => '0;32',
'blue' => '1;34',
'yellow' => '1;33'
];

Expand All @@ -407,15 +449,20 @@ public static function colored($text, $color)
: $colors['black']
);

if (false && $arguments['no-colors']->value) {
if ($this->arguments->{'no-colors'}) {
return $text;
} else {
return "\033[". $c . "m" . $text . "\033[0m";
return "\033[". $c . 'm' . $text . "\033[0m";
}
}

public static function output($text, $color)
/**
* Print colored text
* @param string $text
* @param string $color
*/
public function output($text, $color)
{
echo Binary::colored($text, $color);
echo $this->colored($text, $color);
}
}
14 changes: 13 additions & 1 deletion src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use \Phramework\Testphase\Util;

/**
* Expression methods and constants
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @version 1.0.0
Expand All @@ -41,8 +42,19 @@ class Expression
*/
const EXPRESSION_TYPE_INLINE_REPLACE = 'inline_replace';

/**
* Regular expression pattern of keys
*/
const PATTERN_KEY = '[a-zA-Z][a-zA-Z0-9\-_]{1,}';
const PATTERN_FUNCTION_PARAMETER = self::PATTERN_KEY . '|([\'\"]?)' . '[a-zA-Z0-9\-_]{1,}' . '\5';

/**
* Regular expression pattern of function parameters
*/
const PATTERN_FUNCTION_PARAMETER = '[a-zA-Z][a-zA-Z0-9\-_]{1,}|([\'\"]?)[a-zA-Z0-9\-_]{1,}\5';

/**
* Regular expression pattern of array indices
*/
const PATTERN_ARRAY_INDEX = '[1-9]*[0-9]';

/**
Expand Down
Loading

0 comments on commit 8961395

Please sign in to comment.