Skip to content

Commit

Permalink
NEW Add RetryScenarioTester
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Apr 10, 2024
1 parent 8bd1452 commit 404195b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Behat\Behat\Tester\ServiceContainer\TesterExtension;
use SilverStripe\BehatExtension\Utility\RetryScenarioTester;

/*
* This file is part of the SilverStripe\BehatExtension
Expand Down Expand Up @@ -100,6 +102,17 @@ public function load(ContainerBuilder $container, array $config)
$container->setParameter('silverstripe_extension.region_map', $config['region_map']);
}
$container->setParameter('silverstripe_extension.bootstrap_file', $config['bootstrap_file']);

// When running in CI, behat scenarios will occasionally sporadically fail
// decorate the scenario tester with our own class so we can retry it once if it fails
$isCI = $config['is_ci'] ?? false;
if ($isCI) {
$definition = new Definition(RetryScenarioTester::class, [
new Reference(TesterExtension::SCENARIO_TESTER_ID),
]);
$definition->addTag(TesterExtension::SCENARIO_TESTER_WRAPPER_TAG);
$container->setDefinition(TesterExtension::SCENARIO_TESTER_WRAPPER_TAG . '.silverstripe', $definition);
}
}

/**
Expand Down
42 changes: 42 additions & 0 deletions src/Utility/RetryScenarioTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace SilverStripe\BehatExtension\Utility;

use Behat\Behat\Tester\ScenarioTester;
use Behat\Gherkin\Node\FeatureNode;
use Behat\Gherkin\Node\ScenarioInterface;
use Behat\Testwork\Environment\Environment;
use Behat\Testwork\Tester\Result\TestResult;

/**
* This tester will retry a scenario if it fails
*/
class RetryScenarioTester implements ScenarioTester
{
private ScenarioTester $decoratedTester;

public function __construct(ScenarioTester $decoratedTester)
{
$this->decoratedTester = $decoratedTester;
}

public function setUp(Environment $env, FeatureNode $feature, ScenarioInterface $scenario, $skip)
{
return $this->decoratedTester->setUp($env, $feature, $scenario, $skip);
}

public function test(Environment $env, FeatureNode $feature, ScenarioInterface $scenario, $skip)
{
$result = $this->decoratedTester->test($env, $feature, $scenario, $skip);
if (!$skip && !$result->isPassed()) {
// Run the scenario again
$result = $this->decoratedTester->test($env, $feature, $scenario, $skip);
}
return $result;
}

public function tearDown(Environment $env, FeatureNode $feature, ScenarioInterface $scenario, $skip, TestResult $result)
{
return $this->decoratedTester->tearDown($env, $feature, $scenario, $skip, $result);
}
}

0 comments on commit 404195b

Please sign in to comment.