|
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 rdx\behatvars\BehatVariablesArgumentTransformer; |
|
10
|
|
|
|
|
11
|
|
|
class BehatVariablesContext implements Context, SnippetAcceptingContext { |
|
12
|
|
|
|
|
13
|
|
|
protected $lastResult = []; |
|
14
|
|
|
|
|
15
|
|
|
static protected $storage = array(); |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Initializes context. |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct() { |
|
21
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @AfterStep |
|
28
|
|
|
*/ |
|
29
|
|
|
public function afterStep(AfterStepScope $scope) { |
|
30
|
|
|
$this->lastResult = []; |
|
31
|
|
|
|
|
32
|
|
|
$result = $scope->getTestResult(); |
|
33
|
|
|
if (is_callable(array($result, 'getCallResult'))) { |
|
34
|
|
|
$result = $result->getCallResult(); |
|
35
|
|
|
if (is_callable(array($result, 'getReturn'))) { |
|
36
|
|
|
$result = $result->getReturn(); |
|
37
|
|
|
if ($result !== null) { |
|
38
|
|
|
$this->lastResult = is_array($result) && isset($result[0]) ? array_values($result) : [$result]; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @AfterFeature |
|
46
|
|
|
*/ |
|
47
|
|
|
static public function afterFeature(AfterFeatureScope $scope) { |
|
|
|
|
|
|
48
|
|
|
self::storageClear(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @When /^(?:I|we) save (?:it|that|those|them) into "([\w,]+)"$/ |
|
55
|
|
|
*/ |
|
56
|
|
|
public function saveItInto($slot) { |
|
57
|
|
|
if (!$this->lastResult) { |
|
|
|
|
|
|
58
|
|
|
throw new \Exception("Can't store empty return value. Have a step method return a value."); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$slots = explode(',', $slot); |
|
62
|
|
|
if (count($slots) != count($this->lastResult)) { |
|
63
|
|
|
$slots = count($slots); |
|
64
|
|
|
$results = count($this->lastResult); |
|
65
|
|
|
throw new \Exception("Number of slots ($slots) does not match number of last results ($results)."); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$valids = array_filter($slots, [BehatVariablesArgumentTransformer::class, 'validSlotName']); |
|
69
|
|
|
if ($valids !== $slots) { |
|
70
|
|
|
throw new \Exception("Invalid slot name(s) in '$slot'"); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
foreach ($slots as $index => $slot) { |
|
74
|
|
|
$value = $this->lastResult[$index]; |
|
75
|
|
|
$this->storageSet($slot, $value); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->lastResult = []; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function storageSet($name, $value) { |
|
87
|
|
|
if (!is_scalar($value)) { |
|
88
|
|
|
$type = gettype($value); |
|
89
|
|
|
throw new \Exception("Storing value must be scalar, but it's a '$type'."); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
self::$storage[$name] = $value; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* |
|
97
|
|
|
*/ |
|
98
|
|
|
public function storageGet($name) { |
|
99
|
|
|
if (!isset(self::$storage[$name])) { |
|
100
|
|
|
throw new \Exception("Value for '$name' does not exist."); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return self::$storage[$name]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* |
|
108
|
|
|
*/ |
|
109
|
|
|
static protected function storageClear() { |
|
|
|
|
|
|
110
|
|
|
self::$storage = array(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.