1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Before step hook that provide parameter syntax notation |
4
|
|
|
* for accessing fixture data between Gherkin steps/tests |
5
|
|
|
* example: |
6
|
|
|
* I see "{{param}}" |
7
|
|
|
* {{param}} will be replaced by the value of Fixtures::get('param') |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
namespace Codeception\Extension; |
11
|
|
|
|
12
|
|
|
use RuntimeException; |
13
|
|
|
use Codeception\Util\Fixtures; |
14
|
|
|
use Behat\Gherkin\Node\TableNode; |
15
|
|
|
use ReflectionProperty; |
16
|
|
|
|
17
|
|
|
class GherkinParam extends \Codeception\Platform\Extension |
18
|
|
|
{ |
19
|
|
|
// list events to listen to |
20
|
|
|
public static $events = array( |
21
|
|
|
//run before any suite |
22
|
|
|
'suite.before' => 'beforeSuite', |
23
|
|
|
//run before any steps |
24
|
|
|
'step.before' => 'beforeStep' |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
private static $suite_config; |
28
|
|
|
|
29
|
|
|
private static $regEx = Array( |
30
|
|
|
'match' => '/^{{[A-z0-9_:-]+}}$/', |
31
|
|
|
'filter' => '/[{}]/', |
32
|
|
|
'config' => '/(?:^config)?:([A-z0-9_-]+)+(?=:|$)/', |
33
|
|
|
'array' => '/^(?P<var>[A-z0-9_-]+)(?:\[(?P<key>.+)])$/' |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
// parse param and replace {{.*}} by its Fixtures::get() value if exists |
37
|
|
|
protected function getValueFromParam($param) |
38
|
|
|
{ |
39
|
|
|
if (preg_match(static::$regEx['match'], $param)) { |
|
|
|
|
40
|
|
|
$arg = preg_filter(static::$regEx['filter'], '', $param); |
|
|
|
|
41
|
|
|
if (preg_match(static::$regEx['config'], $arg)) { |
|
|
|
|
42
|
|
|
return $this->getValueFromConfig($arg); |
43
|
|
|
} elseif (preg_match(static::$regEx['array'], $arg)) { |
|
|
|
|
44
|
|
|
return $this->getValueFromArray($arg); |
45
|
|
|
}else { |
46
|
|
|
return Fixtures::get($arg); |
47
|
|
|
} |
48
|
|
|
} else { |
49
|
|
|
return $param; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function getValueFromConfig($param) |
54
|
|
|
{ |
55
|
|
|
$value = null; |
56
|
|
|
$config = static::$suite_config; |
|
|
|
|
57
|
|
|
|
58
|
|
|
preg_match_all(static::$regEx['config'], $param, $args, PREG_PATTERN_ORDER); |
|
|
|
|
59
|
|
|
foreach ($args[1] as $arg) { |
60
|
|
|
if (array_key_exists($arg, $config)) { |
61
|
|
|
$value = $config[$arg]; |
62
|
|
|
if (is_array($value)) { |
63
|
|
|
$config = $value; |
64
|
|
|
} else { |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return $value; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function getValueFromArray($param) |
73
|
|
|
{ |
74
|
|
|
$value = null; |
75
|
|
|
|
76
|
|
|
preg_match_all(static::$regEx['array'], $param, $args); |
|
|
|
|
77
|
|
|
$array = Fixtures::get($args['var'][0]); |
78
|
|
|
foreach ($args['key'] as $arg) { |
79
|
|
|
do { |
80
|
|
|
$exist = array_key_exists($arg, $array); |
81
|
|
|
if ($exist) { |
82
|
|
|
$value = $array[$arg]; |
83
|
|
|
if (is_array($value)) { |
84
|
|
|
$array = $value; |
85
|
|
|
} else { |
86
|
|
|
break; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} while($exist); |
90
|
|
|
} |
91
|
|
|
if ($exist) { |
|
|
|
|
92
|
|
|
return $value; |
93
|
|
|
} else { |
94
|
|
|
throw new RuntimeException("{$array}[{$arg}] does not exist"); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function beforeSuite(\Codeception\Event\SuiteEvent $e) |
99
|
|
|
{ |
100
|
|
|
static::$suite_config = $e->getSettings(); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function beforeStep(\Codeception\Event\StepEvent $e) |
104
|
|
|
{ |
105
|
|
|
$step = $e->getStep(); |
106
|
|
|
// access to the protected property using reflection |
107
|
|
|
$refArgs = new ReflectionProperty(get_class($step), 'arguments'); |
108
|
|
|
// change property accessibility to public |
109
|
|
|
$refArgs->setAccessible(true); |
110
|
|
|
// retrieve 'arguments' value |
111
|
|
|
$args = $refArgs->getValue($step); |
112
|
|
|
foreach ($args as $index => $arg) { |
113
|
|
|
if (is_string($arg)) { |
114
|
|
|
// case if arg is a string |
115
|
|
|
// e.g. I see "{{param}}" |
116
|
|
|
$args[$index] = $this->getValueFromParam($arg); |
117
|
|
|
} elseif (is_a($arg, '\Behat\Gherkin\Node\TableNode')) { |
118
|
|
|
// case if arg is a table |
119
|
|
|
// e.g. I see : |
120
|
|
|
// | paramater | |
121
|
|
|
// | {{param}} | |
122
|
|
|
$table = Array(); |
123
|
|
|
foreach ($arg->getRows() as $i => $row) { |
124
|
|
|
foreach ($row as $j => $cell) { |
125
|
|
|
$table[$i][$j] = $this->getValueFromParam($cell); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
$args[$index] = new TableNode($table); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
// set new arguments value |
132
|
|
|
$refArgs->setValue($step, $args); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: