Inheritance: implements GetOptionKit\OptionPrinter\OptionPrinter
 public function test()
 {
     $options = new OptionCollection();
     $options->add('f|foo:', 'option requires a value.')->isa('String');
     $options->add('b|bar+', 'option with multiple value.')->isa('Number');
     $options->add('z|zoo?', 'option with optional value.')->isa('Boolean');
     $options->add('n', 'n flag');
     $options->add('verbose', 'verbose');
     $options->add('o|output?', 'option with optional value.')->isa('File')->defaultValue('output.txt');
     $printer = new ConsoleOptionPrinter();
     $output = $printer->render($options);
 }
Example #2
0
 /**
  * 
  * @param array $argsList
  * @param type $aliveObject
  * @param string $action
  */
 private function getArgs(array &$argsList, $aliveObject, $action)
 {
     $listOptions = array();
     if (isset($aliveObject->options)) {
         $listOptions = $aliveObject->options;
     }
     $specs = new OptionCollection();
     foreach ($listOptions as $option => $spec) {
         if ($spec['type'] != 'boolean') {
             if ($spec['multiple']) {
                 $option .= '+';
             } else {
                 if ($spec['required']) {
                     $option .= ':';
                 } else {
                     $option .= '?';
                 }
             }
         }
         $specs->add($option, $spec['help'])->isa($spec['type']);
     }
     $parser = new OptionParser($specs);
     $parsedOptions = self::parseOptions($this->arguments, $parser);
     if (isset($aliveObject->objectName)) {
         $events = Di::getDefault()->get('events');
         $manageCommandOptionsEvent = new ManageCommandOptionsEvent($aliveObject->objectName, $action, $listOptions, $parsedOptions);
         $events->emit('core.manage.command.options', array($manageCommandOptionsEvent));
         $listOptions = $manageCommandOptionsEvent->getOptions();
         $aliveObject->options = $listOptions;
     }
     $listOptions = array_merge($listOptions, array('h|help' => array('help' => 'help', 'type' => 'boolean', 'functionParams' => '', "toTransform" => '', 'required' => false, 'defaultValue' => false)));
     $specs = new OptionCollection();
     foreach ($listOptions as $option => $spec) {
         if ($spec['type'] != 'boolean') {
             if ($spec['multiple']) {
                 $option .= '+';
             } else {
                 if ($spec['required']) {
                     $option .= ':';
                 } else {
                     $option .= '?';
                 }
             }
         }
         $specs->add($option, $spec['help'])->isa($spec['type']);
     }
     try {
         $parser = new OptionParser($specs);
         $optionsParsed = $parser->parse($this->arguments);
     } catch (RequireValueException $ex) {
         echo $ex->getMessage();
     }
     if ($optionsParsed->help) {
         $printer = new ConsoleOptionPrinter();
         echo $printer->render($specs);
         die;
     }
     unset($listOptions['h|help']);
     $this->manageConsoleParams($listOptions, $optionsParsed, $argsList);
 }
Example #3
0
 /**
  * Invoke scripts
  * @return integer Returns indicate how the script exited.
  * Normal exit is generally represented by a 0 return.
  */
 public function invoke()
 {
     echo 'Hephaestus v' . Hephaestus::VERSION . PHP_EOL;
     $arguments = $this->arguments;
     $subCommandOptions = $this->subCommandOptions;
     if ($arguments->help) {
         echo 'Help:' . PHP_EOL;
         $printer = new ConsoleOptionPrinter();
         echo $printer->render(static::getArgumentSpecifications());
         return 0;
     }
     if (isset($subCommandOptions->generate)) {
         echo 'generate' . PHP_EOL;
         return new Generate($subCommandOptions->generate);
         //var_dump($subCommandOptions->generate);
         //foreach ($subCommandOptions->generate as $key => $spec) {
         //    echo $spec . PHP_EOL;
         //}
     }
 }
Example #4
0
 /**
  * Print option specs.
  *
  * @since 150424 Initial release.
  *
  * @return string Specs as a string.
  */
 public function specs() : string
 {
     $Printer = new ConsoleOptionPrinter();
     return $Printer->render($this->OptionCollection);
 }
Example #5
0
use GetOptionKit\OptionParser;
use GetOptionKit\OptionPrinter\ConsoleOptionPrinter;
$specs = new OptionCollection();
$specs->add('f|foo:', 'option requires a value.')->isa('String');
$specs->add('b|bar+', 'option with multiple value.')->isa('Number');
$specs->add('z|zoo?', 'option with optional value.')->isa('Boolean');
$specs->add('o|output?', 'option with optional value.')->isa('File')->defaultValue('output.txt');
// works for -vvv  => verbose = 3
$specs->add('v|verbose', 'verbose')->isa('Number')->incremental();
$specs->add('file:', 'option value should be a file.')->trigger(function ($value) {
    echo "Set value to :";
    var_dump($value);
})->isa('File');
$specs->add('r|regex:', 'with custom regex type value')->isa('Regex', '/^([a-z]+)$/');
$specs->add('d|debug', 'debug message.');
$specs->add('long', 'long option name only.');
$specs->add('s', 'short option name only.');
$specs->add('m', 'short option m');
$specs->add('4', 'short option with digit');
$printer = new ConsoleOptionPrinter();
echo $printer->render($specs);
$parser = new OptionParser($specs);
echo "Enabled options: \n";
try {
    $result = $parser->parse($argv);
    foreach ($result as $key => $spec) {
        echo $spec . "\n";
    }
} catch (Exception $e) {
    echo $e->getMessage();
}
Example #6
0
 /**
  * Invoke scripts
  * @return integer Returns indicate how the script exited.
  * Normal exit is generally represented by a 0 return.
  */
 public function invoke()
 {
     $arguments = $this->arguments;
     if (($serverHost = $arguments->{'server-host'}) !== null) {
         $this->server = new Server($serverHost, $arguments->{'server-root'});
         $this->server->start();
     }
     //Include bootstrap file if set
     if ($bootstrapFile = $arguments->bootstrap) {
         require $bootstrapFile;
     }
     echo 'testphase v' . Testphase::getVersion() . PHP_EOL;
     if ($arguments->help) {
         echo 'Help:' . PHP_EOL;
         $printer = new ConsoleOptionPrinter();
         echo $printer->render(static::getArgumentSpecifications());
         return 0;
     } elseif ($arguments->debug) {
         echo 'Enabled options: ' . PHP_EOL;
         foreach ($arguments as $key => $spec) {
             echo $spec;
         }
     }
     try {
         $testParserCollection = $this->getTestParserCollection();
     } catch (\Exception $e) {
         echo $e->getMessage();
         return $this->stop(1);
     }
     //Statistics object
     $stats = (object) ['tests' => count($testParserCollection), 'success' => 0, 'error' => 0, 'failure' => 0, 'ignore' => 0, 'incomplete' => 0, 'errors' => []];
     $testIndex = 0;
     foreach ($testParserCollection as $test) {
         //Check if subdir argument is set
         if (isset($arguments->subdir) && $arguments->subdir !== null) {
             //If so check if file name passes the given pattern
             //Remove base dir from filename
             $cleanFilename = trim(str_replace($arguments->dir, '', $test->getFilename()), '/');
             $match = false;
             //Check if file name matches any of the subdir patterns
             foreach ($arguments->subdir as $pattern) {
                 $pattern = '@' . $pattern . '@';
                 if (!!preg_match($pattern, $cleanFilename)) {
                     $match = $match || true;
                     break;
                 }
             }
             if (!$match) {
                 //Ignore
                 $stats->ignore += 1;
                 if ($arguments->verbose) {
                     echo sprintf('I %s', $test->getFilename()) . PHP_EOL;
                 } else {
                     echo 'I';
                 }
                 continue;
             }
         }
         $meta = $test->getMeta();
         if (isset($meta->ignore) && $meta->ignore) {
             if ($arguments->verbose) {
                 echo sprintf('I %s', $test->getFilename()) . PHP_EOL;
             } else {
                 echo 'I';
             }
             $stats->ignore += 1;
             continue;
         }
         if (isset($meta->incomplete) && $meta->incomplete !== false) {
             $stats->incomplete += 1;
         }
         try {
             //Complete test's testphase collection
             $test->createTest();
         } catch (\Exception $e) {
             echo sprintf('Unable to create test from file "%s" %s With message: "%s"', $test->getFilename(), PHP_EOL, $e->getMessage()) . PHP_EOL;
             return $this->stop(1);
         }
         $testphaseCollection = $test->getTest();
         //Include number of additional testphase collections
         $stats->tests += count($testphaseCollection) - 1;
         //Iterate though test parser's testphase collection
         foreach ($testphaseCollection as $testphase) {
             try {
                 $testphase->run(function ($responseStatusCode, $responseHeaders, $responseBody, $responseBodyObject = null) use($test, $arguments) {
                     //todo move to TestParser
                     $export = $test->getExport();
                     //Fetch all test exports and add them as globals
                     foreach ($export as $key => $value) {
                         $path = explode('.', $value);
                         $pathValue = $responseBodyObject;
                         foreach ($path as $p) {
                             //@todo implement array index
                             $arrayIndex = 0;
                             if (is_array($pathValue)) {
                                 $pathValue = $pathValue[$arrayIndex]->{$p};
                             } else {
                                 $pathValue = $pathValue->{$p};
                             }
                         }
                         Globals::set($key, $pathValue);
                     }
                     if ($arguments->debug) {
                         echo 'Response Status Code:' . PHP_EOL;
                         echo $responseStatusCode . PHP_EOL;
                         echo 'Response Headers:' . PHP_EOL;
                         print_r($responseHeaders);
                         echo PHP_EOL;
                         echo 'Response Body:' . PHP_EOL;
                         echo json_encode($responseBodyObject, JSON_PRETTY_PRINT) . PHP_EOL;
                     }
                 });
                 //Echo successful char
                 if ($arguments->verbose) {
                     echo sprintf('. %s', $test->getFilename()) . PHP_EOL;
                 } else {
                     echo '.';
                 }
                 $stats->success += 1;
             } catch (UnsetGlobalException $e) {
                 //Error message
                 $message = $e->getMessage();
                 $message = sprintf(self::colored('Test "%s" failed with message', 'red') . PHP_EOL . ' %s' . PHP_EOL, $test->getFilename(), $message);
                 //push message to error message
                 $stats->errors[] = $message;
                 //Echo unsuccessful char
                 if ($arguments->verbose) {
                     echo sprintf('F %s', $test->getFilename()) . PHP_EOL;
                 } else {
                     echo 'F';
                 }
                 //print if immediate
                 if ($arguments->immediate) {
                     echo PHP_EOL . $message . PHP_EOL;
                 }
                 //Error message
                 $stats->error += 1;
             } catch (\Exception $e) {
                 //Error message
                 $message = $e->getMessage();
                 if ($arguments->debug) {
                     $message .= PHP_EOL . $testphase->getResponseBody();
                 }
                 $message = sprintf(self::colored('Test "%s" failed with message', 'red') . PHP_EOL . ' %s' . PHP_EOL, $test->getFilename(), $message);
                 if (get_class($e) == IncorrectParametersException::class) {
                     $message .= 'Incorrect:' . PHP_EOL . json_encode($e->getParameters(), JSON_PRETTY_PRINT) . PHP_EOL;
                 } elseif (get_class($e) == MissingParametersException::class) {
                     $message .= 'Missing:' . PHP_EOL . json_encode($e->getParameters(), JSON_PRETTY_PRINT) . PHP_EOL;
                 }
                 //push message to error message
                 $stats->errors[] = $message;
                 //Echo unsuccessful char
                 if ($arguments->verbose) {
                     echo sprintf('F %s', $test->getFilename()) . PHP_EOL;
                 } else {
                     echo 'F';
                 }
                 //print if immediate
                 if ($arguments->immediate) {
                     echo PHP_EOL . $message . PHP_EOL;
                 }
                 $stats->failure += 1;
             }
             ++$testIndex;
             //Show only 80 characters per line
             if (!($testIndex % 79)) {
                 echo PHP_EOL;
             }
         }
     }
     echo PHP_EOL;
     if ($arguments->{'show-globals'}) {
         echo 'Globals:' . PHP_EOL;
         echo Globals::toString() . PHP_EOL;
     }
     //don't print if immediate is true
     if (!$arguments->immediate && !empty($stats->errors)) {
         echo 'Errors:' . PHP_EOL;
         foreach ($stats->errors as $e) {
             echo $e . PHP_EOL;
         }
     }
     echo 'Complete!' . PHP_EOL;
     echo 'Tests:' . $stats->tests . ', ';
     Binary::output('Successful: ' . $stats->success, 'green');
     echo ', ';
     Binary::output('Ignored: ' . $stats->ignore, 'yellow');
     echo ', ';
     Binary::output('Incomplete: ' . $stats->incomplete, 'yellow');
     echo ', ';
     Binary::output('Error: ' . $stats->error, 'red');
     echo ', ';
     Binary::output('Failure: ' . $stats->failure . PHP_EOL, 'red');
     echo 'Memory usage: ' . (int) (memory_get_usage(true) / 1048576) . ' MB' . PHP_EOL;
     echo 'Elapsed time: ' . (time() - $_SERVER['REQUEST_TIME']) . ' s' . PHP_EOL;
     if ($stats->error > 0) {
         return $this->stop(1);
     }
     if ($stats->failure > 0) {
         return $this->stop(2);
     }
     return $this->stop(0);
 }