diff --git a/Jyxo/Beholder/Executor.php b/Jyxo/Beholder/Executor.php index 650fe81..519a39e 100644 --- a/Jyxo/Beholder/Executor.php +++ b/Jyxo/Beholder/Executor.php @@ -13,6 +13,13 @@ namespace Jyxo\Beholder; +use Jyxo\Beholder\Output\HtmlOutput; +use Jyxo\Beholder\Output\JsonOutput; +use Jyxo\Beholder\Output\NoOutput; +use Jyxo\Beholder\Output\Output; +use Jyxo\Beholder\Output\TextOutput; +use Jyxo\Beholder\Result\TestSuiteResult; + /** * Beholder test executor. * @@ -179,9 +186,11 @@ public function setParams(array $params) /** * Performs chosen tests and outputs results according to the selected output type. * - * @return boolean Returns if all tests were successful + * @param bool $print + * + * @return \Jyxo\Beholder\Output\Output */ - public function run(): bool + public function run($print = true): Output { // Filters tests foreach (array_keys($this->tests) as $ident) { @@ -216,10 +225,6 @@ public function run(): bool } array_multisort($idents, SORT_ASC, $this->testsData); - if ($this->output === self::OUTPUT_NOTHING) { - return $allSucceeded; - } - // Outputs the header if ($allSucceeded) { header('HTTP/1.1 200 OK'); @@ -227,24 +232,35 @@ public function run(): bool header('HTTP/1.1 500 Internal Server Error'); } + $result = new TestSuiteResult($this->project, $allSucceeded, $this->testsData, $this->includeFilter, $this->excludeFilter); + // Outputs the output :) switch ($this->output) { + // No output + case self::OUTPUT_NOTHING: + $output = new NoOutput($result); + break; // Plaintext case self::OUTPUT_TEXT: - $this->writeText($allSucceeded); + $output = new TextOutput($result); break; // JSON case self::OUTPUT_JSON: - $this->writeJson($allSucceeded); + $output = new JsonOutput($result); break; // HTML case self::OUTPUT_HTML: default: - $this->writeHtml($allSucceeded); + $output = new HtmlOutput($result); break; } - return $allSucceeded; + if ($print) { + header(sprintf('Content-type: %s', $output->getContentType())); + echo (string) $output; + } + + return $output; } /** @@ -339,167 +355,4 @@ private function patternMatch(string $pattern, string $string): bool return fnmatch($pattern, $string); } - /** - * Outputs results in HTML form. - * - * @param boolean $allSucceeded Have all tests been successful - */ - private function writeHtml(bool $allSucceeded) - { - header('Content-Type: text/html; charset=utf-8'); - echo '
' . "\n"; - echo '' . "\n"; - echo 'Tests included: ' . $this->includeFilter . "\n";
- echo '
Tests excluded: ' . $this->excludeFilter . "\n";
- echo '
Run order | Duration | Ident | Status | Test name | Comment |
---|---|---|---|---|---|
%d | -%.2fs | -%s | -%s | -%s | -%s | -
Tests are included, then excluded.
- - - ' . "\n"; - } - - /** - * Outputs results in plaintext. - * - * @param boolean $allSucceeded Have all tests been successful - */ - private function writeText(bool $allSucceeded) - { - // HTML is sent on purpose - header('Content-Type: text/html; charset=utf-8'); - echo 'This is Beholder for project ' . $this->project . "\n"; - echo 'Tests included: ' . $this->includeFilter . "\n"; - echo 'Tests excluded: ' . $this->excludeFilter . "\n\n"; - echo 'Html version\n\n"; - echo 'JSON version\n\n"; - - echo sprintf("%-9s %10s %-10s %-7s %-35s %s\n", - 'Run Order', 'Duration', 'Ident', 'Status', 'Test Name', 'Description'); - foreach ($this->testsData as $data) { - echo sprintf("%9d %9.2fs %-10s %-7s %-35s %s\n", - $data['order'], - $data['duration'], - $data['ident'], - $data['result']->getStatusMessage(), - $data['test']->getDescription(), - $data['result']->getDescription()); - } - - if ($allSucceeded) { - echo "\nJust a little prayer so we know we are allright.\n\n"; - - echo $this->getPrayer(); - } - } - - /** - * Outputs results as a JSON string - * - * @param boolean $allSucceeded Have all tests been successful - */ - private function writeJson(bool $allSucceeded) - { - header('Content-Type: application/json; charset=utf-8'); - - $tests = []; - foreach ($this->testsData as $data) { - $tests[] = [ - 'order' => $data['order'], - 'duration' => sprintf("%.6f s", $data['duration']), - 'ident' => $data['ident'], - 'result' => $data['result']->getStatusMessage(), - 'test_description' => $data['test']->getDescription(), - 'result_description' => $data['result']->getDescription(), - ]; - } - - $data = [ - 'included' => $this->includeFilter, - 'excluded' => $this->excludeFilter, - 'tests' => $tests, - 'urls' => [ - 'text' => '?' . self::PARAM_INCLUDE . '=' . $this->includeFilter - . '&' . self::PARAM_EXCLUDE . '=' . $this->excludeFilter - . '&' . self::PARAM_OUTPUT . '=' . self::OUTPUT_TEXT, - 'html' => '?' . self::PARAM_INCLUDE . '=' . $this->includeFilter - . '&' . self::PARAM_EXCLUDE . '=' . $this->excludeFilter - . '&' . self::PARAM_OUTPUT . '=' . self::OUTPUT_HTML, - ] - ]; - - if ($allSucceeded) { - $data['prayer'] = $this->getPrayer(); - } - - echo json_encode($data); - } - - private function getPrayer(): string - { - $return = ''; - for ($i = 0; $i < 5; $i++) { - $return .= 'Our Father in heaven,' . "\n"; - $return .= 'hallowed be your name,' . "\n"; - $return .= 'your kingdom come,' . "\n"; - $return .= 'your will be done' . "\n"; - $return .= 'on earth as it is in heaven.' . "\n"; - $return .= 'Give us today our daily bread,' . "\n"; - $return .= 'and forgive us the wrong we have done' . "\n"; - $return .= 'as we forgive those who wrong us.' . "\n"; - $return .= 'Subject us not to the trial' . "\n"; - $return .= 'but deliver us from the evil one.' . "\n"; - $return .= 'And make the ' . $this->project . " project work.\n"; - $return .= 'Amen.' . "\n\n"; - } - - return $return; - } - } diff --git a/Jyxo/Beholder/Output/HtmlOutput.php b/Jyxo/Beholder/Output/HtmlOutput.php new file mode 100644 index 0000000..f4fab0f --- /dev/null +++ b/Jyxo/Beholder/Output/HtmlOutput.php @@ -0,0 +1,92 @@ +' . "\n"; + $return .= '' . "\n"; + $return .= 'Beholder for project ' . $this->result->getProject() . ' ' . "\n"; + $return .= '' . "\n"; + $return .= '' . "\n"; + $return .= '' . "\n"; + $return .= 'Beholder for project ' . $this->result->getProject() . "
\n"; + $return .= 'Tests included: ' . $this->result->getIncludeFilter() . "\n"; + $return .= '
' . "\n"; + $return .= '
Tests excluded: ' . $this->result->getExcludeFilter() . "\n"; + $return .= '
Run order | Duration | Ident | Status | Test name | Comment |
---|---|---|---|---|---|
%d | +%.2fs | +%s | +%s | +%s | +%s | +
Tests are included, then excluded.
+ + + ' . "\n"; + + return $return; + } +} diff --git a/Jyxo/Beholder/Output/JsonOutput.php b/Jyxo/Beholder/Output/JsonOutput.php new file mode 100644 index 0000000..a4a526b --- /dev/null +++ b/Jyxo/Beholder/Output/JsonOutput.php @@ -0,0 +1,64 @@ +result->getTestsData() as $data) { + $tests[] = [ + 'order' => $data['order'], + 'duration' => sprintf("%.6f s", $data['duration']), + 'ident' => $data['ident'], + 'result' => $data['result']->getStatusMessage(), + 'test_description' => $data['test']->getDescription(), + 'result_description' => $data['result']->getDescription(), + ]; + } + + $data = [ + 'included' => $this->result->getIncludeFilter(), + 'excluded' => $this->result->getExcludeFilter(), + 'tests' => $tests, + 'urls' => [ + 'text' => '?' . Executor::PARAM_INCLUDE . '=' . $this->result->getIncludeFilter() + . '&' . Executor::PARAM_EXCLUDE . '=' . $this->result->getExcludeFilter() + . '&' . Executor::PARAM_OUTPUT . '=' . Executor::OUTPUT_TEXT, + 'html' => '?' . Executor::PARAM_INCLUDE . '=' . $this->result->getIncludeFilter() + . '&' . Executor::PARAM_EXCLUDE . '=' . $this->result->getExcludeFilter() + . '&' . Executor::PARAM_OUTPUT . '=' . Executor::OUTPUT_HTML, + ] + ]; + + return json_encode($data); + } +} diff --git a/Jyxo/Beholder/Output/NoOutput.php b/Jyxo/Beholder/Output/NoOutput.php new file mode 100644 index 0000000..7fa30da --- /dev/null +++ b/Jyxo/Beholder/Output/NoOutput.php @@ -0,0 +1,28 @@ +result = $result; + } + + public abstract function getContentType(): string; + + public abstract function __toString(): string; + + /** + * @return \Jyxo\Beholder\Result\TestSuiteResult + */ + public function getResult(): TestSuiteResult + { + return $this->result; + } + +} diff --git a/Jyxo/Beholder/Output/TextOutput.php b/Jyxo/Beholder/Output/TextOutput.php new file mode 100644 index 0000000..2342fda --- /dev/null +++ b/Jyxo/Beholder/Output/TextOutput.php @@ -0,0 +1,88 @@ +This is Beholder for project ' . $this->result->getProject() . "\n"; + $return .= 'Tests included: ' . $this->result->getIncludeFilter() . "\n"; + $return .= 'Tests excluded: ' . $this->result->getExcludeFilter() . "\n\n"; + $return .= 'Html version\n\n"; + $return .= 'JSON version\n\n"; + + $return .= sprintf("%-9s %10s %-10s %-7s %-35s %s\n", + 'Run Order', 'Duration', 'Ident', 'Status', 'Test Name', 'Description'); + foreach ($this->testsData as $data) { + $return .= sprintf("%9d %9.2fs %-10s %-7s %-35s %s\n", + $data['order'], + $data['duration'], + $data['ident'], + $data['result']->getStatusMessage(), + $data['test']->getDescription(), + $data['result']->getDescription()); + } + + if ($this->result->hasAllSucceeded()) { + $return .= "\nJust a little prayer so we know we are allright.\n\n"; + $return .= $this->getPrayer(); + } + + return $return; + } + + private function getPrayer(): string + { + $return = ''; + for ($i = 0; $i < 5; $i++) { + $return .= 'Our Father in heaven,' . "\n"; + $return .= 'hallowed be your name,' . "\n"; + $return .= 'your kingdom come,' . "\n"; + $return .= 'your will be done' . "\n"; + $return .= 'on earth as it is in heaven.' . "\n"; + $return .= 'Give us today our daily bread,' . "\n"; + $return .= 'and forgive us the wrong we have done' . "\n"; + $return .= 'as we forgive those who wrong us.' . "\n"; + $return .= 'Subject us not to the trial' . "\n"; + $return .= 'but deliver us from the evil one.' . "\n"; + $return .= 'And make the ' . $this->result->getProject() . " project work.\n"; + $return .= 'Amen.' . "\n\n"; + } + + return $return; + } +} diff --git a/Jyxo/Beholder/Result/TestSuiteResult.php b/Jyxo/Beholder/Result/TestSuiteResult.php new file mode 100644 index 0000000..d20cb88 --- /dev/null +++ b/Jyxo/Beholder/Result/TestSuiteResult.php @@ -0,0 +1,107 @@ +project = $project; + $this->allSucceeded = $allSucceeded; + $this->testsData = $testsData; + $this->includeFilter = $includeFilter; + $this->excludeFilter = $excludeFilter; + } + + /** + * @return string + */ + public function getProject(): string + { + return $this->project; + } + + /** + * @return bool + */ + public function hasAllSucceeded(): bool + { + return $this->allSucceeded; + } + + /** + * @return array + */ + public function getTestsData(): array + { + return $this->testsData; + } + + /** + * @return string + */ + public function getIncludeFilter(): string + { + return $this->includeFilter; + } + + /** + * @return string + */ + public function getExcludeFilter(): string + { + return $this->excludeFilter; + } + +}