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
|
|
|
use rdx\behatvars\BehatVariablesDatabase; |
11
|
|
|
|
12
|
|
|
class BehatVariablesContext implements Context, SnippetAcceptingContext { |
13
|
|
|
|
14
|
|
|
protected $lastResult = []; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @AfterStep |
20
|
|
|
*/ |
21
|
|
|
public function afterStep(AfterStepScope $scope) { |
22
|
|
|
$this->lastResult = []; |
23
|
|
|
|
24
|
|
|
$result = $scope->getTestResult(); |
25
|
|
|
if (is_callable(array($result, 'getCallResult'))) { |
26
|
|
|
$result = $result->getCallResult(); |
27
|
|
|
if (is_callable(array($result, 'getReturn'))) { |
28
|
|
|
$result = $result->getReturn(); |
29
|
|
|
if ($result !== null) { |
30
|
|
|
$this->lastResult = is_array($result) && isset($result[0]) ? array_values($result) : [$result]; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @AfterFeature |
38
|
|
|
*/ |
39
|
|
|
static public function afterFeature(AfterFeatureScope $scope) { |
|
|
|
|
40
|
|
|
BehatVariablesDatabase::clear(); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @When /^(?:I|we) save (?:it|that|those|them) into "([\w,]+)"$/ |
47
|
|
|
*/ |
48
|
|
|
public function saveItInto($slot) { |
49
|
|
|
if (!$this->lastResult) { |
|
|
|
|
50
|
|
|
throw new \Exception("Can't store empty return value. Have a step method return a value."); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$slots = explode(',', $slot); |
54
|
|
|
if (count($slots) != count($this->lastResult)) { |
55
|
|
|
$slots = count($slots); |
56
|
|
|
$results = count($this->lastResult); |
57
|
|
|
throw new \Exception("Number of slots ($slots) does not match number of last results ($results)."); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$valids = array_filter($slots, [BehatVariablesArgumentTransformer::class, 'validSlotName']); |
61
|
|
|
if ($valids !== $slots) { |
62
|
|
|
throw new \Exception("Invalid slot name(s) in '$slot'"); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
foreach ($slots as $index => $slot) { |
66
|
|
|
$value = $this->lastResult[$index]; |
67
|
|
|
BehatVariablesDatabase::set($slot, $value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->lastResult = []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.