1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rdx\behatvars; |
4
|
|
|
|
5
|
|
|
use Behat\Behat\Context\Context; |
6
|
|
|
use Behat\Behat\Context\SnippetAcceptingContext; |
7
|
|
|
use Behat\Behat\Hook\Scope\AfterFeatureScope; |
8
|
|
|
use Behat\Behat\Hook\Scope\AfterStepScope; |
9
|
|
|
use Behat\Behat\Tester\Result\ExecutedStepResult; |
10
|
|
|
use rdx\behatvars\BehatVariablesArgumentTransformer; |
11
|
|
|
use rdx\behatvars\BehatVariablesDatabase; |
12
|
|
|
|
13
|
|
|
class BehatVariablesContext implements Context, SnippetAcceptingContext { |
14
|
|
|
|
15
|
|
|
protected $lastResult = []; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @AfterStep |
21
|
|
|
*/ |
22
|
|
|
public function afterStep(AfterStepScope $scope) { |
23
|
|
|
$this->lastResult = []; |
24
|
|
|
|
25
|
|
|
$result = $scope->getTestResult(); |
26
|
|
|
if ($result instanceof ExecutedStepResult) { |
27
|
|
|
$result = $result->getCallResult()->getReturn(); |
28
|
|
|
if ($result !== null) { |
29
|
|
|
$this->lastResult = is_array($result) && isset($result[0]) ? array_values($result) : [$result]; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @AfterFeature |
36
|
|
|
*/ |
37
|
|
|
static public function afterFeature(AfterFeatureScope $scope) { |
38
|
|
|
BehatVariablesDatabase::clear(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @When /^(?:I|we) save (?:it|that|those|them) into "([^"]+)"$/ |
45
|
|
|
*/ |
46
|
|
|
public function saveItInto($slot) { |
47
|
|
|
if (!$this->lastResult) { |
|
|
|
|
48
|
|
|
throw new \Exception("Can't store empty return value. Have a step method return a value."); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$slots = explode(',', $slot); |
52
|
|
|
if (count($slots) != count($this->lastResult)) { |
53
|
|
|
$slots = count($slots); |
54
|
|
|
$results = count($this->lastResult); |
55
|
|
|
throw new \Exception("Number of slots ($slots) does not match number of last results ($results)."); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$valids = array_filter($slots, [BehatVariablesArgumentTransformer::class, 'validSlotName']); |
59
|
|
|
if ($valids !== $slots) { |
60
|
|
|
throw new \Exception("Invalid slot name(s) in '$slot'. Beware the white space!"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($slots as $index => $slot) { |
64
|
|
|
if ($slot) { |
65
|
|
|
$value = $this->lastResult[$index]; |
66
|
|
|
BehatVariablesDatabase::set($slot, $value); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->lastResult = []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.