Completed
Push — master ( bd90c3...f3164d )
by Jonathan
9s
created

RandomContext::transformVariables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
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) {
0 ignored issues
show
Bug introduced by
The class Drupal\DrupalExtension\Context\TableNode does not exist. Did you forget a USE statement, or did you not list all dependencies?

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 the composer.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 or require-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 ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $scope is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
        $this->values = [];
89
    }
90
}
91