|
1
|
|
|
<?php namespace Chekote\BehatRetryExtension\Context; |
|
2
|
|
|
|
|
3
|
|
|
use Behat\Behat\Context\Context; |
|
4
|
|
|
use Chekote\BehatRetryExtension\Tester\RuntimeStepTester; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Context for working with auto-retrying assertions (Then steps). |
|
8
|
|
|
*/ |
|
9
|
|
|
class BehatRetryContext implements Context |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var float the timeout setting for RuntimeStepTester before a Scenario is ran */ |
|
12
|
|
|
protected $originalTimeout; |
|
13
|
|
|
|
|
14
|
|
|
/** @var int the interval setting for RuntimeStepTester before a Scenario is ran */ |
|
15
|
|
|
protected $originalInterval; |
|
16
|
|
|
|
|
17
|
|
|
/** @var bool the strict keywords setting for RuntimeStepTester before a Scenario is ran */ |
|
18
|
|
|
protected $originalStrictKeywords; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Sets the number of seconds that an assertion (Then step) should retry before failing. |
|
22
|
|
|
* |
|
23
|
|
|
* @Given assertions will retry for :timeout seconds before failing |
|
24
|
|
|
* @param float $timeout the number of seconds |
|
25
|
|
|
*/ |
|
26
|
|
|
public function setTimeout($timeout) |
|
27
|
|
|
{ |
|
28
|
|
|
RuntimeStepTester::$timeout = $timeout; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Sets the number of nanoseconds that should pass between each retry of an assertion (Then step). |
|
33
|
|
|
* |
|
34
|
|
|
* @Given assertions will retry every :interval nanoseconds |
|
35
|
|
|
* @param int $interval the number of nanoseconds |
|
36
|
|
|
*/ |
|
37
|
|
|
public function setInterval($interval) |
|
38
|
|
|
{ |
|
39
|
|
|
RuntimeStepTester::$interval = $interval; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Requires that steps should only match when the correct keyword is used. |
|
44
|
|
|
* |
|
45
|
|
|
* @Given strict keywords are required |
|
46
|
|
|
*/ |
|
47
|
|
|
public function enableStrictKeywords() |
|
48
|
|
|
{ |
|
49
|
|
|
RuntimeStepTester::$strictKeywords = true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Allows steps to match if if the correct keyword is not used. |
|
54
|
|
|
* |
|
55
|
|
|
* @Given strict keywords are not required |
|
56
|
|
|
*/ |
|
57
|
|
|
public function disableStrictKeywords() |
|
58
|
|
|
{ |
|
59
|
|
|
RuntimeStepTester::$strictKeywords = false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Records the configuration of the RuntimeStepTester. |
|
64
|
|
|
* |
|
65
|
|
|
* The config is recorded so that it can be modified after the Scenario has ran, thus ensuring that config settings |
|
66
|
|
|
* changed during the Scenario are reset back to their original settings. |
|
67
|
|
|
* |
|
68
|
|
|
* @BeforeScenario |
|
69
|
|
|
*/ |
|
70
|
|
|
public function recordConfig() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->originalTimeout = RuntimeStepTester::$timeout; |
|
73
|
|
|
$this->originalInterval = RuntimeStepTester::$interval; |
|
74
|
|
|
$this->originalStrictKeywords = RuntimeStepTester::$strictKeywords; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Restores the RuntimeStepTester config to it's original state. |
|
79
|
|
|
* |
|
80
|
|
|
* The config is restored to it's state before the Scenario was ran. |
|
81
|
|
|
* |
|
82
|
|
|
* @AfterScenario |
|
83
|
|
|
*/ |
|
84
|
|
|
public function restoreConfig() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->setTimeout($this->originalTimeout); |
|
87
|
|
|
$this->setInterval($this->originalInterval); |
|
88
|
|
|
$this->originalStrictKeywords ? $this->enableStrictKeywords() : $this->disableStrictKeywords(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|