From f3bcf07bf38e895cfa7e301ee1e61d4e86ca9aa0 Mon Sep 17 00:00:00 2001 From: Neal Vanmeert Date: Fri, 20 Mar 2015 14:20:21 +0100 Subject: [PATCH] Added optional file name parameter. --- README.md | 3 + src/BehatHTMLFormatterExtension.php | 117 ++- src/Formatter/BehatHTMLFormatter.php | 1025 +++++++++++++------------- src/Printer/FileOutputPrinter.php | 124 ++-- src/Renderer/TwigRenderer.php | 85 ++- 5 files changed, 674 insertions(+), 680 deletions(-) diff --git a/README.md b/README.md index 6b034de..764c376 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,15 @@ formatters: emuse\BehatHTMLFormatter\BehatHTMLFormatterExtension: name: html renderer: Twig,Behat2 + file_name: Index The *output* parameter is relative to %paths.base% and, when omitted, will default to that same path. The *renderer* is the renderer engine and the report format that you want to be generated. +The *file_name* is optional. When it is added, the report name will be fixed instead fo generated, and this file will be overwritten with every build. + Actually, there is 3 formats : - **Twig** : new report format based on Twig, **requires Twig installed** diff --git a/src/BehatHTMLFormatterExtension.php b/src/BehatHTMLFormatterExtension.php index 9e6a627..7e044d3 100644 --- a/src/BehatHTMLFormatterExtension.php +++ b/src/BehatHTMLFormatterExtension.php @@ -12,68 +12,65 @@ * Class BehatHTMLFormatterExtension * @package Features\Formatter */ -class BehatHTMLFormatterExtension implements ExtensionInterface -{ - /** - * You can modify the container here before it is dumped to PHP code. - * - * @param ContainerBuilder $container - * - * @api - */ - public function process(ContainerBuilder $container) - { - } +class BehatHTMLFormatterExtension implements ExtensionInterface { + /** + * You can modify the container here before it is dumped to PHP code. + * + * @param ContainerBuilder $container + * + * @api + */ + public function process(ContainerBuilder $container) { + } - /** - * Returns the extension config key. - * - * @return string - */ - public function getConfigKey() - { - return "emusehtml"; - } + /** + * Returns the extension config key. + * + * @return string + */ + public function getConfigKey() { + return "emusehtml"; + } - /** - * Initializes other extensions. - * - * This method is called immediately after all extensions are activated but - * before any extension `configure()` method is called. This allows extensions - * to hook into the configuration of other extensions providing such an - * extension point. - * - * @param ExtensionManager $extensionManager - */ - public function initialize(ExtensionManager $extensionManager) - { - } + /** + * Initializes other extensions. + * + * This method is called immediately after all extensions are activated but + * before any extension `configure()` method is called. This allows extensions + * to hook into the configuration of other extensions providing such an + * extension point. + * + * @param ExtensionManager $extensionManager + */ + public function initialize(ExtensionManager $extensionManager) { + } - /** - * Setups configuration for the extension. - * - * @param ArrayNodeDefinition $builder - */ - public function configure(ArrayNodeDefinition $builder) - { - $builder->children()->scalarNode("name")->defaultValue("emusehtml"); - $builder->children()->scalarNode("renderer")->defaultValue("behat2"); - $builder->children()->scalarNode('output')->defaultValue('.'); - } + /** + * Setups configuration for the extension. + * + * @param ArrayNodeDefinition $builder + */ + public function configure(ArrayNodeDefinition $builder) { + $builder->children()->scalarNode("name")->defaultValue("emusehtml"); + $builder->children()->scalarNode("renderer")->defaultValue("behat2"); + $builder->children()->scalarNode("file_name")->defaultValue("generated"); + $builder->children()->scalarNode('output')->defaultValue('.'); + } - /** - * Loads extension services into temporary container. - * - * @param ContainerBuilder $container - * @param array $config - */ - public function load(ContainerBuilder $container, array $config) - { - $definition = new Definition("emuse\\BehatHTMLFormatter\\Formatter\\BehatHTMLFormatter"); - $definition->addArgument($config['name']); - $definition->addArgument($config['renderer']); - $definition->addArgument('%paths.base%'); - $container->setDefinition("html.formatter", $definition) - ->addTag("output.formatter"); - } + /** + * Loads extension services into temporary container. + * + * @param ContainerBuilder $container + * @param array $config + */ + public function load(ContainerBuilder $container, array $config) { + $definition = new Definition("emuse\\BehatHTMLFormatter\\Formatter\\BehatHTMLFormatter"); + $definition->addArgument($config['name']); + $definition->addArgument($config['renderer']); + $definition->addArgument($config['file_name']); + + $definition->addArgument('%paths.base%'); + $container->setDefinition("html.formatter", $definition) + ->addTag("output.formatter"); + } } diff --git a/src/Formatter/BehatHTMLFormatter.php b/src/Formatter/BehatHTMLFormatter.php index 94fc992..629e080 100644 --- a/src/Formatter/BehatHTMLFormatter.php +++ b/src/Formatter/BehatHTMLFormatter.php @@ -2,11 +2,11 @@ namespace emuse\BehatHTMLFormatter\Formatter; +use Behat\Behat\EventDispatcher\Event\AfterFeatureTested; use Behat\Behat\EventDispatcher\Event\AfterOutlineTested; use Behat\Behat\EventDispatcher\Event\AfterScenarioTested; use Behat\Behat\EventDispatcher\Event\AfterStepTested; use Behat\Behat\EventDispatcher\Event\BeforeFeatureTested; -use Behat\Behat\EventDispatcher\Event\AfterFeatureTested; use Behat\Behat\EventDispatcher\Event\BeforeOutlineTested; use Behat\Behat\EventDispatcher\Event\BeforeScenarioTested; use Behat\Behat\Tester\Result\ExecutedStepResult; @@ -23,538 +23,525 @@ use emuse\BehatHTMLFormatter\Classes\Scenario; use emuse\BehatHTMLFormatter\Classes\Step; use emuse\BehatHTMLFormatter\Classes\Suite; -use emuse\BehatHTMLFormatter\Renderer\BaseRenderer; use emuse\BehatHTMLFormatter\Printer\FileOutputPrinter; +use emuse\BehatHTMLFormatter\Renderer\BaseRenderer; /** * Class BehatHTMLFormatter * @package tests\features\formatter */ -class BehatHTMLFormatter implements Formatter -{ - - // - /** - * @var array - */ - private $parameters; - - /** - * @var - */ - private $name; - - /** - * @var - */ - private $timer; - - /** - * @var - */ - private $memory; - - - /** - * @param String $outputPath where to save the generated report file - */ - private $outputPath; - - /** - * @param String $base_path Behat base path - */ - private $base_path; - - /** - * Printer used by this Formatter - * @param $printer OutputPrinter - */ - private $printer; - - /** - * Renderer used by this Formatter - * @param $renderer BaseRenderer - */ - private $renderer; - - /** - * @var Array - */ - private $suites; - - /** - * @var Suite - */ - private $currentSuite; - - /** - * @var int - */ - private $featureCounter = 1; - /** - * @var Feature - */ - private $currentFeature; - - /** - * @var Scenario - */ - private $currentScenario; - - /** - * @var Scenario[] - */ - private $failedScenarios; - - /** - * @var Scenario[] - */ - private $passedScenarios; - - /** - * @var Feature[] - */ - private $failedFeatures; - - /** - * @var Feature[] - */ - private $passedFeatures; - - /** - * @var Step[] - */ - private $failedSteps; - - /** - * @var Step[] - */ - private $passedSteps; - - /** - * @var Step[] - */ - private $pendingSteps; - - /** - * @var Step[] - */ - private $skippedSteps; - // - - // - /** - * @param $name - * @param $base_path - */ - function __construct($name, $renderer, $base_path) - { - $this->name = $name; - $this->renderer = new BaseRenderer($renderer, $base_path); - $this->printer = new FileOutputPrinter($this->renderer->getNameList(), $base_path); - $this->timer = new Timer(); - $this->memory = new Memory(); - - } - - /** - * Returns an array of event names this subscriber wants to listen to. - * - * @return array The event names to listen to - */ - public static function getSubscribedEvents() - { - return array( - 'tester.exercise_completed.before' => 'onBeforeExercise', - 'tester.exercise_completed.after' => 'onAfterExercise', - 'tester.suite_tested.before' => 'onBeforeSuiteTested', - 'tester.suite_tested.after' => 'onAfterSuiteTested', - 'tester.feature_tested.before' => 'onBeforeFeatureTested', - 'tester.feature_tested.after' => 'onAfterFeatureTested', - 'tester.scenario_tested.before' => 'onBeforeScenarioTested', - 'tester.scenario_tested.after' => 'onAfterScenarioTested', - 'tester.outline_tested.before' => 'onBeforeOutlineTested', - 'tester.outline_tested.after' => 'onAfterOutlineTested', - 'tester.step_tested.after' => 'onAfterStepTested', +class BehatHTMLFormatter implements Formatter { + + // + /** + * @var array + */ + private $parameters; + + /** + * @var + */ + private $name; + + /** + * @var + */ + private $timer; + + /** + * @var + */ + private $memory; + + + /** + * @param String $outputPath where to save the generated report file + */ + private $outputPath; + + /** + * @param String $base_path Behat base path + */ + private $base_path; + + /** + * Printer used by this Formatter + * @param $printer OutputPrinter + */ + private $printer; + + /** + * Renderer used by this Formatter + * @param $renderer BaseRenderer + */ + private $renderer; + + /** + * @var Array + */ + private $suites; + + /** + * @var Suite + */ + private $currentSuite; + + /** + * @var int + */ + private $featureCounter = 1; + /** + * @var Feature + */ + private $currentFeature; + + /** + * @var Scenario + */ + private $currentScenario; + + /** + * @var Scenario[] + */ + private $failedScenarios; + + /** + * @var Scenario[] + */ + private $passedScenarios; + + /** + * @var Feature[] + */ + private $failedFeatures; + + /** + * @var Feature[] + */ + private $passedFeatures; + + /** + * @var Step[] + */ + private $failedSteps; + + /** + * @var Step[] + */ + private $passedSteps; + + /** + * @var Step[] + */ + private $pendingSteps; + + /** + * @var Step[] + */ + private $skippedSteps; + // + + // + /** + * @param $name + * @param $base_path + */ + function __construct($name, $renderer, $filename, $base_path) { + $this->name = $name; + $this->renderer = new BaseRenderer($renderer, $base_path); + $this->printer = new FileOutputPrinter($this->renderer->getNameList(), $filename, $base_path); + $this->timer = new Timer(); + $this->memory = new Memory(); + + } + + /** + * Returns an array of event names this subscriber wants to listen to. + * + * @return array The event names to listen to + */ + public static function getSubscribedEvents() { + return array( + 'tester.exercise_completed.before' => 'onBeforeExercise', + 'tester.exercise_completed.after' => 'onAfterExercise', + 'tester.suite_tested.before' => 'onBeforeSuiteTested', + 'tester.suite_tested.after' => 'onAfterSuiteTested', + 'tester.feature_tested.before' => 'onBeforeFeatureTested', + 'tester.feature_tested.after' => 'onAfterFeatureTested', + 'tester.scenario_tested.before' => 'onBeforeScenarioTested', + 'tester.scenario_tested.after' => 'onAfterScenarioTested', + 'tester.outline_tested.before' => 'onBeforeOutlineTested', + 'tester.outline_tested.after' => 'onAfterOutlineTested', + 'tester.step_tested.after' => 'onAfterStepTested', + ); + } + + /** + * Returns formatter name. + * + * @return string + */ + public function getName() { + return $this->name; + } + + /** + * Returns formatter description. + * + * @return string + */ + public function getDescription() { + return "Formatter for teamcity"; + } + + /** + * Returns formatter output printer. + * + * @return OutputPrinter + */ + public function getOutputPrinter() { + return $this->printer; + } + + /** + * Sets formatter parameter. + * + * @param string $name + * @param mixed $value + */ + public function setParameter($name, $value) { + $this->parameters[$name] = $value; + } + + /** + * Returns parameter name. + * + * @param string $name + * @return mixed + */ + public function getParameter($name) { + return $this->parameters[$name]; + } + + /** + * Verify that the specified output path exists or can be created, + * then sets the output path. + * + * @param String $path Output path relative to %paths.base% + * @throws BadOutputPathException + */ + public function setOutputPath($path) { + $outpath = realpath($this->base_path . DIRECTORY_SEPARATOR . $path); + if (!file_exists($outpath)) { + if (!mkdir($outpath, 0755, TRUE)) { + throw new BadOutputPathException( + sprintf( + 'Output path %s does not exist and could not be created!', + $outpath + ), + $outpath ); + } + } + else { + if (!is_dir($outpath)) { + throw new BadOutputPathException( + sprintf( + 'The argument to `output` is expected to the a directory, but got %s!', + $outpath + ), + $outpath + ); + } } - - /** - * Returns formatter name. - * - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * Returns formatter description. - * - * @return string - */ - public function getDescription() - { - return "Formatter for teamcity"; - } - - /** - * Returns formatter output printer. - * - * @return OutputPrinter - */ - public function getOutputPrinter() - { - return $this->printer; - } - - /** - * Sets formatter parameter. - * - * @param string $name - * @param mixed $value - */ - public function setParameter($name, $value) - { - $this->parameters[$name] = $value; - } - - /** - * Returns parameter name. - * - * @param string $name - * @return mixed - */ - public function getParameter($name) - { - return $this->parameters[$name]; - } - - /** - * Verify that the specified output path exists or can be created, - * then sets the output path. - * - * @param String $path Output path relative to %paths.base% - * @throws BadOutputPathException - */ - public function setOutputPath($path) - { - $outpath = realpath($this->base_path . DIRECTORY_SEPARATOR . $path); - if (!file_exists($outpath)) { - if (!mkdir($outpath, 0755, true)) - throw new BadOutputPathException( - sprintf( - 'Output path %s does not exist and could not be created!', - $outpath - ), - $outpath - ); - } else { - if (!is_dir($outpath)) { - throw new BadOutputPathException( - sprintf( - 'The argument to `output` is expected to the a directory, but got %s!', - $outpath - ), - $outpath - ); - } - } - $this->outputPath = $outpath; - } - - /** - * Returns output path - * - * @return String output path - */ - public function getOutputPath() - { - return $this->outputPath; - } - - public function getTimer(){ - return $this->timer; - } - - public function getMemory(){ - return $this->memory; - } - - public function getSuites(){ - return $this->suites; - } - - public function getCurrentSuite(){ - return $this->currentSuite; - } - - public function getFeatureCounter(){ - return $this->featureCounter; - } - - public function getCurrentFeature(){ - return $this->currentFeature; - } - - public function getCurrentScenario(){ - return $this->currentScenario; - } - - public function getFailedScenarios(){ - return $this->failedScenarios; - } - - public function getPassedScenarios(){ - return $this->passedScenarios; - } - - public function getFailedFeatures(){ - return $this->failedFeatures; - } - - public function getPassedFeatures(){ - return $this->passedFeatures; - } - - public function getFailedSteps(){ - return $this->failedSteps; - } - - public function getPassedSteps(){ - return $this->passedSteps; - } - - public function getPendingSteps(){ - return $this->pendingSteps; - } - - public function getSkippedSteps(){ - return $this->skippedSteps; - } - - - // - - // - /** - * @param BeforeExerciseCompleted $event - */ - public function onBeforeExercise(BeforeExerciseCompleted $event) - { - $this->timer->start(); - - $print = $this->renderer->renderBeforeExercise($this) ; - $this->printer->write($print) ; - } - - /** - * @param AfterExerciseCompleted $event - */ - public function onAfterExercise(AfterExerciseCompleted $event) - { - - $this->timer->stop(); - - $print = $this->renderer->renderAfterExercise($this) ; - $this->printer->writeln($print) ; - } - - /** - * @param BeforeSuiteTested $event - */ - public function onBeforeSuiteTested(BeforeSuiteTested $event) - { - $this->currentSuite = new Suite(); - $this->currentSuite->setName($event->getSuite()->getName()); - - $print = $this->renderer->renderBeforeSuite($this) ; - $this->printer->writeln($print) ; - } - - /** - * @param AfterSuiteTested $event - */ - public function onAfterSuiteTested(AfterSuiteTested $event) - { - $this->suites[] = $this->currentSuite; - - $print = $this->renderer->renderAfterSuite($this) ; - $this->printer->writeln($print); - } - - /** - * @param BeforeFeatureTested $event - */ - public function onBeforeFeatureTested(BeforeFeatureTested $event) - { - $feature = new Feature(); - $feature->setId($this->featureCounter); - $this->featureCounter++; - $feature->setName($event->getFeature()->getTitle()); - $feature->setDescription($event->getFeature()->getDescription()); - $feature->setTags($event->getFeature()->getTags()); - $feature->setFile($event->getFeature()->getFile()); - $this->currentFeature = $feature; - - $print = $this->renderer->renderBeforeFeature($this) ; - $this->printer->writeln($print); - } - - /** - * @param AfterFeatureTested $event - */ - public function onAfterFeatureTested(AfterFeatureTested $event) - { - $this->currentSuite->addFeature($this->currentFeature); - if ($this->currentFeature->allPassed()) { - $this->passedFeatures[] = $this->currentFeature; - } else { - $this->failedFeatures[] = $this->currentFeature; - } - - $print = $this->renderer->renderAfterFeature($this) ; - $this->printer->writeln($print); - } - - /** - * @param BeforeScenarioTested $event - */ - public function onBeforeScenarioTested(BeforeScenarioTested $event) - { - $scenario = new Scenario(); - $scenario->setName($event->getScenario()->getTitle()); - $scenario->setTags($event->getScenario()->getTags()); - $scenario->setLine($event->getScenario()->getLine()); - $this->currentScenario = $scenario; - - $print = $this->renderer->renderBeforeScenario($this) ; - $this->printer->writeln($print); - } - - /** - * @param AfterScenarioTested $event - */ - public function onAfterScenarioTested(AfterScenarioTested $event) - { - $scenarioPassed = $event->getTestResult()->isPassed(); - - if ($scenarioPassed) { - $this->passedScenarios[] = $this->currentScenario; - $this->currentFeature->addPassedScenario(); - } else { - $this->failedScenarios[] = $this->currentScenario; - $this->currentFeature->addFailedScenario(); + $this->outputPath = $outpath; + } + + /** + * Returns output path + * + * @return String output path + */ + public function getOutputPath() { + return $this->outputPath; + } + + public function getTimer() { + return $this->timer; + } + + public function getMemory() { + return $this->memory; + } + + public function getSuites() { + return $this->suites; + } + + public function getCurrentSuite() { + return $this->currentSuite; + } + + public function getFeatureCounter() { + return $this->featureCounter; + } + + public function getCurrentFeature() { + return $this->currentFeature; + } + + public function getCurrentScenario() { + return $this->currentScenario; + } + + public function getFailedScenarios() { + return $this->failedScenarios; + } + + public function getPassedScenarios() { + return $this->passedScenarios; + } + + public function getFailedFeatures() { + return $this->failedFeatures; + } + + public function getPassedFeatures() { + return $this->passedFeatures; + } + + public function getFailedSteps() { + return $this->failedSteps; + } + + public function getPassedSteps() { + return $this->passedSteps; + } + + public function getPendingSteps() { + return $this->pendingSteps; + } + + public function getSkippedSteps() { + return $this->skippedSteps; + } + + + // + + // + /** + * @param BeforeExerciseCompleted $event + */ + public function onBeforeExercise(BeforeExerciseCompleted $event) { + $this->timer->start(); + + $print = $this->renderer->renderBeforeExercise($this); + $this->printer->write($print); + } + + /** + * @param AfterExerciseCompleted $event + */ + public function onAfterExercise(AfterExerciseCompleted $event) { + + $this->timer->stop(); + + $print = $this->renderer->renderAfterExercise($this); + $this->printer->writeln($print); + } + + /** + * @param BeforeSuiteTested $event + */ + public function onBeforeSuiteTested(BeforeSuiteTested $event) { + $this->currentSuite = new Suite(); + $this->currentSuite->setName($event->getSuite()->getName()); + + $print = $this->renderer->renderBeforeSuite($this); + $this->printer->writeln($print); + } + + /** + * @param AfterSuiteTested $event + */ + public function onAfterSuiteTested(AfterSuiteTested $event) { + $this->suites[] = $this->currentSuite; + + $print = $this->renderer->renderAfterSuite($this); + $this->printer->writeln($print); + } + + /** + * @param BeforeFeatureTested $event + */ + public function onBeforeFeatureTested(BeforeFeatureTested $event) { + $feature = new Feature(); + $feature->setId($this->featureCounter); + $this->featureCounter++; + $feature->setName($event->getFeature()->getTitle()); + $feature->setDescription($event->getFeature()->getDescription()); + $feature->setTags($event->getFeature()->getTags()); + $feature->setFile($event->getFeature()->getFile()); + $this->currentFeature = $feature; + + $print = $this->renderer->renderBeforeFeature($this); + $this->printer->writeln($print); + } + + /** + * @param AfterFeatureTested $event + */ + public function onAfterFeatureTested(AfterFeatureTested $event) { + $this->currentSuite->addFeature($this->currentFeature); + if ($this->currentFeature->allPassed()) { + $this->passedFeatures[] = $this->currentFeature; + } + else { + $this->failedFeatures[] = $this->currentFeature; + } + + $print = $this->renderer->renderAfterFeature($this); + $this->printer->writeln($print); + } + + /** + * @param BeforeScenarioTested $event + */ + public function onBeforeScenarioTested(BeforeScenarioTested $event) { + $scenario = new Scenario(); + $scenario->setName($event->getScenario()->getTitle()); + $scenario->setTags($event->getScenario()->getTags()); + $scenario->setLine($event->getScenario()->getLine()); + $this->currentScenario = $scenario; + + $print = $this->renderer->renderBeforeScenario($this); + $this->printer->writeln($print); + } + + /** + * @param AfterScenarioTested $event + */ + public function onAfterScenarioTested(AfterScenarioTested $event) { + $scenarioPassed = $event->getTestResult()->isPassed(); + + if ($scenarioPassed) { + $this->passedScenarios[] = $this->currentScenario; + $this->currentFeature->addPassedScenario(); + } + else { + $this->failedScenarios[] = $this->currentScenario; + $this->currentFeature->addFailedScenario(); + } + + $this->currentScenario->setPassed($event->getTestResult()->isPassed()); + $this->currentFeature->addScenario($this->currentScenario); + + + $print = $this->renderer->renderAfterScenario($this); + $this->printer->writeln($print); + } + + /** + * @param BeforeOutlineTested $event + */ + public function onBeforeOutlineTested(BeforeOutlineTested $event) { + $scenario = new Scenario(); + $scenario->setName($event->getOutline()->getTitle()); + $scenario->setTags($event->getOutline()->getTags()); + $scenario->setLine($event->getOutline()->getLine()); + $this->currentScenario = $scenario; + + $print = $this->renderer->renderBeforeOutline($this); + $this->printer->writeln($print); + } + + /** + * @param AfterOutlineTested $event + */ + public function onAfterOutlineTested(AfterOutlineTested $event) { + $scenarioPassed = $event->getTestResult()->isPassed(); + + if ($scenarioPassed) { + $this->passedScenarios[] = $this->currentScenario; + $this->currentFeature->addPassedScenario(); + } + else { + $this->failedScenarios[] = $this->currentScenario; + $this->currentFeature->addFailedScenario(); + } + + $this->currentScenario->setPassed($event->getTestResult()->isPassed()); + $this->currentFeature->addScenario($this->currentScenario); + + $print = $this->renderer->renderAfterOutline($this); + $this->printer->writeln($print); + } + + /** + * @param BeforeStepTested $event + */ + public function onBeforeStepTested(BeforeStepTested $event) { + $print = $this->renderer->renderBeforeStep($this); + $this->printer->writeln($print); + } + + + /** + * @param AfterStepTested $event + */ + public function onAfterStepTested(AfterStepTested $event) { + $result = $event->getTestResult(); + + /** @var Step $step */ + $step = new Step(); + $step->setKeyword($event->getStep()->getKeyword()); + $step->setText($event->getStep()->getText()); + $step->setLine($event->getStep()->getLine()); + $step->setArguments($event->getStep()->getArguments()); + $step->setResult($result); + $step->setResultCode($result->getResultCode()); + + //What is the result of this step ? + if (is_a($result, 'Behat\Behat\Tester\Result\UndefinedStepResult')) { + //pending step -> no definition to load + $this->pendingSteps[] = $step; + + } + else { + if (is_a($result, 'Behat\Behat\Tester\Result\SkippedStepResult')) { + //skipped step + $step->setDefinition($result->getStepDefinition()); + $this->skippedSteps[] = $step; + } + else { + //failed or passed + if ($result instanceof ExecutedStepResult) { + $step->setDefinition($result->getStepDefinition()); + $exception = $result->getException(); + if ($exception) { + $step->setException($exception->getMessage()); + $this->failedSteps[] = $step; + } + else { + $this->passedSteps[] = $step; + } } - - $this->currentScenario->setPassed($event->getTestResult()->isPassed()); - $this->currentFeature->addScenario($this->currentScenario); - - - $print = $this->renderer->renderAfterScenario($this) ; - $this->printer->writeln($print); + } } - /** - * @param BeforeOutlineTested $event - */ - public function onBeforeOutlineTested(BeforeOutlineTested $event) - { - $scenario = new Scenario(); - $scenario->setName($event->getOutline()->getTitle()); - $scenario->setTags($event->getOutline()->getTags()); - $scenario->setLine($event->getOutline()->getLine()); - $this->currentScenario = $scenario; - - $print = $this->renderer->renderBeforeOutline($this) ; - $this->printer->writeln($print); - } + $this->currentScenario->addStep($step); - /** - * @param AfterOutlineTested $event - */ - public function onAfterOutlineTested(AfterOutlineTested $event) - { - $scenarioPassed = $event->getTestResult()->isPassed(); - - if ($scenarioPassed) { - $this->passedScenarios[] = $this->currentScenario; - $this->currentFeature->addPassedScenario(); - } else { - $this->failedScenarios[] = $this->currentScenario; - $this->currentFeature->addFailedScenario(); - } - - $this->currentScenario->setPassed($event->getTestResult()->isPassed()); - $this->currentFeature->addScenario($this->currentScenario); - - $print = $this->renderer->renderAfterOutline($this) ; - $this->printer->writeln($print); - } + $print = $this->renderer->renderAfterStep($this); + $this->printer->writeln($print); + } - /** - * @param BeforeStepTested $event - */ - public function onBeforeStepTested(BeforeStepTested $event) - { - $print = $this->renderer->renderBeforeStep($this) ; - $this->printer->writeln($print); - } - - - /** - * @param AfterStepTested $event - */ - public function onAfterStepTested(AfterStepTested $event) - { - $result = $event->getTestResult(); - - /** @var Step $step */ - $step = new Step(); - $step->setKeyword($event->getStep()->getKeyword()); - $step->setText($event->getStep()->getText()); - $step->setLine($event->getStep()->getLine()); - $step->setArguments($event->getStep()->getArguments()); - $step->setResult($result); - $step->setResultCode($result->getResultCode()); - - //What is the result of this step ? - if (is_a($result, 'Behat\Behat\Tester\Result\UndefinedStepResult')) { - //pending step -> no definition to load - $this->pendingSteps[] = $step; - - } else if (is_a($result, 'Behat\Behat\Tester\Result\SkippedStepResult')){ - //skipped step - $step->setDefinition($result->getStepDefinition()); - $this->skippedSteps[] = $step; - } else { - //failed or passed - if ($result instanceof ExecutedStepResult) { - $step->setDefinition($result->getStepDefinition()); - $exception = $result->getException(); - if ($exception) { - $step->setException($exception->getMessage()); - $this->failedSteps[] = $step; - } else { - $this->passedSteps[] = $step; - } - } - } - - $this->currentScenario->addStep($step); - - $print = $this->renderer->renderAfterStep($this) ; - $this->printer->writeln($print); + /** + * @param $text + */ + public function printText($text) { + file_put_contents('php://stdout', $text); } - /** - * @param $text - */ - public function printText($text) - { - file_put_contents('php://stdout', $text); - } - } diff --git a/src/Printer/FileOutputPrinter.php b/src/Printer/FileOutputPrinter.php index 42fe316..e8eb704 100644 --- a/src/Printer/FileOutputPrinter.php +++ b/src/Printer/FileOutputPrinter.php @@ -21,20 +21,30 @@ class FileOutputPrinter implements PrinterInterface { * @param $base_path Behat base path */ private $base_path; - + /** * @param $rendererFiles List of the filenames for the renderers */ - private $rendererFiles; + private $rendererFiles; - public function __construct($rendererList, $base_path) { + /** + * @param $rendererList + * @param $filename + * @param $base_path + */ + public function __construct($rendererList, $filename, $base_path) { //let's generate the filenames for the renderers - $this->rendererFiles = array() ; - foreach($rendererList as $renderer) { - $date = date('YmdHis') ; - $this->rendererFiles[$renderer] = $renderer . '_' . $date ; + $this->rendererFiles = array(); + foreach ($rendererList as $renderer) { + if ($filename == 'generated') { + $date = date('YmdHis'); + $this->rendererFiles[$renderer] = $renderer . '_' . $date; + } + else{ + $this->rendererFiles[$renderer] = $filename; + } } - + $this->base_path = $base_path; } @@ -132,53 +142,51 @@ public function setOutputVerbosity($level) { * @return integer */ public function getOutputVerbosity() { - + } - /** - * Writes message(s) to output console. - * - * @param string|array $messages message or array of messages - */ - public function write($messages = array()) - { - //Write it for each message = each renderer - foreach($messages as $key => $message) { - $file = $this->outputPath . DIRECTORY_SEPARATOR . $this->rendererFiles[$key]. '.html'; - file_put_contents($file, $message); - $this->copyAssets($key) ; - } + /** + * Writes message(s) to output console. + * + * @param string|array $messages message or array of messages + */ + public function write($messages = array()) { + //Write it for each message = each renderer + foreach ($messages as $key => $message) { + $file = $this->outputPath . DIRECTORY_SEPARATOR . $this->rendererFiles[$key] . '.html'; + file_put_contents($file, $message); + $this->copyAssets($key); } - - - /** - * Writes newlined message(s) to output console. - * - * @param string|array $messages message or array of messages - */ - public function writeln($messages = array()) - { - //Write it for each message = each renderer - foreach($messages as $key => $message) { - $file = $this->outputPath . DIRECTORY_SEPARATOR . $this->rendererFiles[$key]. '.html'; - file_put_contents($file, $message, FILE_APPEND); - } + } + + + /** + * Writes newlined message(s) to output console. + * + * @param string|array $messages message or array of messages + */ + public function writeln($messages = array()) { + //Write it for each message = each renderer + foreach ($messages as $key => $message) { + $file = $this->outputPath . DIRECTORY_SEPARATOR . $this->rendererFiles[$key] . '.html'; + file_put_contents($file, $message, FILE_APPEND); } - - /** - * Writes message(s) at start of the output console. - * - * @param string|array $messages message or array of messages - */ - public function writeBeginning($messages = array()) { - - //Write it for each message = each renderer - foreach($messages as $key => $message) { - $file = $this->outputPath . DIRECTORY_SEPARATOR . $this->rendererFiles[$key]. '.html'; - $fileContents = file_get_contents($file); - file_put_contents($file, $message . $fileContents); - } + } + + /** + * Writes message(s) at start of the output console. + * + * @param string|array $messages message or array of messages + */ + public function writeBeginning($messages = array()) { + + //Write it for each message = each renderer + foreach ($messages as $key => $message) { + $file = $this->outputPath . DIRECTORY_SEPARATOR . $this->rendererFiles[$key] . '.html'; + $fileContents = file_get_contents($file); + file_put_contents($file, $message . $fileContents); } + } /** * Copies the assets folder to the report destination. @@ -189,15 +197,15 @@ public function copyAssets($renderer) { // If the assets folder doesn' exist in the output path for this renderer, copy it $source = realpath(dirname(__FILE__)); $assets_source = realpath($source . '/../../assets/' . $renderer); - if ($assets_source === false) { - //There is no assets to copy for this renderer - return ; - } - + if ($assets_source === FALSE) { + //There is no assets to copy for this renderer + return; + } + //first create the assets dir - $destination = $this->outputPath . DIRECTORY_SEPARATOR . 'assets' ; - @mkdir($destination) ; - + $destination = $this->outputPath . DIRECTORY_SEPARATOR . 'assets'; + @mkdir($destination); + $this->recurse_copy($assets_source, $destination . DIRECTORY_SEPARATOR . $renderer); } diff --git a/src/Renderer/TwigRenderer.php b/src/Renderer/TwigRenderer.php index 8f57375..c9c8c75 100644 --- a/src/Renderer/TwigRenderer.php +++ b/src/Renderer/TwigRenderer.php @@ -13,28 +13,27 @@ class TwigRenderer public function __construct() { - + } - /** * Renders before an exercice. * * @param object : BehatHTMLFormatter object * @return string : HTML generated - */ + */ public function renderBeforeExercise($obj) { return '' ; } - + /** * Renders after an exercice. * * @param object : BehatHTMLFormatter object * @return string : HTML generated - */ + */ public function renderAfterExercise($obj) { - + $templatePath = dirname(__FILE__) . '/../../templates'; $loader = new Twig_Loader_Filesystem($templatePath); $twig = new Twig_Environment($loader, array()); @@ -48,132 +47,132 @@ public function renderAfterExercise($obj) { 'failedFeatures' => $obj->getFailedFeatures(), 'passedFeatures' => $obj->getPassedFeatures(), ) - ); - + ); + return $print ; - + } - - + + /** * Renders before a suite. * * @param object : BehatHTMLFormatter object * @return string : HTML generated - */ + */ public function renderBeforeSuite($obj) { return '' ; - } + } /** * Renders after a suite. * * @param object : BehatHTMLFormatter object * @return string : HTML generated - */ + */ public function renderAfterSuite($obj) { return '' ; - } - + } + /** * Renders before a feature. * * @param object : BehatHTMLFormatter object * @return string : HTML generated - */ + */ public function renderBeforeFeature($obj) { return '' ; - } + } /** * Renders after a feature. * * @param object : BehatHTMLFormatter object * @return string : HTML generated - */ + */ public function renderAfterFeature($obj) { return '' ; - } + } /** * Renders before a scenario. * * @param object : BehatHTMLFormatter object * @return string : HTML generated - */ + */ public function renderBeforeScenario($obj) { return '' ; - } + } /** * Renders after a scenario. * * @param object : BehatHTMLFormatter object * @return string : HTML generated - */ + */ public function renderAfterScenario($obj) { return '' ; - } - + } + /** * Renders before an outline. * * @param object : BehatHTMLFormatter object * @return string : HTML generated - */ + */ public function renderBeforeOutline($obj) { return '' ; } - + /** * Renders after an outline. * * @param object : BehatHTMLFormatter object * @return string : HTML generated - */ + */ public function renderAfterOutline($obj) { return '' ; - } - + } + /** * Renders before a step. * * @param object : BehatHTMLFormatter object * @return string : HTML generated - */ + */ public function renderBeforeStep($obj) { return '' ; } - + /** * Renders after a step. * * @param object : BehatHTMLFormatter object * @return string : HTML generated - */ + */ public function renderAfterStep($obj) { return '' ; - } - - - - + } + + + + /** * To include CSS * - * @return string : HTML generated + * @return string : HTML generated */ public function getCSS() { return '' ; - + } - + /** * To include JS * - * @return string : HTML generated + * @return string : HTML generated */ public function getJS() { return '' ; - } + } }