1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\DrupalExtension\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Behat\Hook\Scope\AfterScenarioScope; |
6
|
|
|
use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class RandomContext |
10
|
|
|
* @package Drupal\DrupalExtension\Context |
11
|
|
|
* |
12
|
|
|
* Transform tokens into random variables. |
13
|
|
|
*/ |
14
|
|
|
class RandomContext extends RawDrupalContext |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Tracks variable names for consistent replacement during a given scenario. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $values = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The regex to use for variable replacement. |
25
|
|
|
* |
26
|
|
|
* This matches placeholders in steps of the form `Given a string <?random>`. |
27
|
|
|
*/ |
28
|
|
|
const VARIABLE_REGEX = '#(\<\?.*?\>)#'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Transform random variables. |
32
|
|
|
* |
33
|
|
|
* @Transform #([^<]*\<\?.*\>[^>]*)# |
34
|
|
|
*/ |
35
|
|
|
public function transformVariables($message) |
36
|
|
|
{ |
37
|
|
|
$patterns = []; |
38
|
|
|
$replacements = []; |
39
|
|
|
|
40
|
|
|
preg_match_all(static::VARIABLE_REGEX, $message, $matches); |
41
|
|
|
foreach ($matches[0] as $variable) { |
42
|
|
|
$replacements[] = $this->values[$variable]; |
43
|
|
|
$patterns[] = '#' . preg_quote($variable) . '#'; |
44
|
|
|
} |
45
|
|
|
$message = preg_replace($patterns, $replacements, $message); |
46
|
|
|
|
47
|
|
|
return $message; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set values for each random variable found in the current scenario. |
52
|
|
|
* |
53
|
|
|
* @BeforeScenario |
54
|
|
|
*/ |
55
|
|
|
public function beforeScenarioSetVariables(BeforeScenarioScope $scope) |
56
|
|
|
{ |
57
|
|
|
$steps = []; |
58
|
|
|
if ($scope->getFeature()->hasBackground()) { |
59
|
|
|
$steps = $scope->getFeature()->getBackground()->getSteps(); |
60
|
|
|
} |
61
|
|
|
$steps = array_merge($steps, $scope->getScenario()->getSteps()); |
62
|
|
|
foreach ($steps as $step) { |
63
|
|
|
preg_match_all(static::VARIABLE_REGEX, $step->getText(), $matches); |
64
|
|
|
$variables_found = $matches[0]; |
65
|
|
|
// Find variables in are TableNodes or PyStringNodes. |
66
|
|
|
$step_argument = $step->getArguments(); |
67
|
|
|
if (!empty($step_argument) && $step_argument[0] instanceof TableNode) { |
|
|
|
|
68
|
|
|
preg_match_all(static::VARIABLE_REGEX, $step_argument[0]->getTableAsString(), $matches); |
69
|
|
|
$variables_found = array_filter(array_merge($variables_found, $matches[0])); |
70
|
|
|
} |
71
|
|
|
foreach ($variables_found as $variable_name) { |
72
|
|
|
if (!isset($this->values[$variable_name])) { |
73
|
|
|
$value = $this->getDriver()->getRandom()->name(10); |
74
|
|
|
// Value forced to lowercase to ensure it is machine-readable. |
75
|
|
|
$this->values[$variable_name] = strtolower($value); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Reset variables after the scenario. |
83
|
|
|
* |
84
|
|
|
* @AfterScenario |
85
|
|
|
*/ |
86
|
|
|
public function afterScenarioResetVariables(AfterScenarioScope $scope) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$this->values = []; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.