Completed
Push — master ( 28216c...bfead4 )
by Kévin
01:32
created

BaseContext::iUseTheDefaultDataset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RulerZ\Test;
6
7
use Behat\Behat\Context\Context;
8
use Behat\Gherkin\Node\PyStringNode;
9
use Behat\Gherkin\Node\TableNode;
10
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
11
12
/**
13
 * @internal Meant to be used by the compilation target libraries.
14
 */
15
abstract class BaseContext implements Context
16
{
17
    /** @var \RulerZ\RulerZ */
18
    protected $rulerz;
19
20
    /** @var array */
21
    protected $parameters = [];
22
23
    /** @var array */
24
    protected $executionContext = [];
25
26
    /** @var mixed */
27
    protected $dataset;
28
29
    /** @var mixed */
30
    protected $results;
31
32
    /**
33
     * Returns the compilation target to be tested.
34
     *
35
     * @return \RulerZ\Compiler\CompilationTarget
36
     */
37
    abstract protected function getCompilationTarget();
38
39
    /**
40
     * Returns the default dataset to be filtered.
41
     *
42
     * @return mixed
43
     */
44
    abstract protected function getDefaultDataset();
45
46
    public function __construct()
47
    {
48
        $this->initialize();
49
    }
50
51
    /**
52
     * @BeforeSuite
53
     */
54
    public static function prepare(BeforeSuiteScope $scope)
55
    {
56
        echo 'Current suite: '.$scope->getSuite()->getName();
57
    }
58
59
    /**
60
     * Will be called right after the construction of the context, useful to
61
     * initialize stuff before the tests are launched.
62
     */
63
    protected function initialize()
64
    {
65
    }
66
67
    /**
68
     * Create a default execution context that will be given to RulerZ when
69
     * filtering a dataset.
70
     *
71
     * @return mixed
72
     */
73
    protected function getDefaultExecutionContext()
74
    {
75
        return [];
76
    }
77
78
    /**
79
     * @Given RulerZ is configured
80
     */
81
    public function rulerzIsConfigured()
82
    {
83
        // compiler
84
        $compiler = new \RulerZ\Compiler\Compiler(new \RulerZ\Compiler\EvalEvaluator());
85
86
        // RulerZ engine
87
        $this->rulerz = new \RulerZ\RulerZ($compiler, [$this->getCompilationTarget()]);
88
    }
89
90
    /**
91
     * @When I define the parameters:
92
     */
93
    public function iDefineTheParameters(TableNode $parameters)
94
    {
95
        // named parameters
96
        if (count($parameters->getRow(0)) !== 1) {
97
            $this->parameters = $parameters->getRowsHash();
98
99
            return;
100
        }
101
102
        // positional parameters
103
        $this->parameters = array_map(function ($row) {
104
            return $row[0];
105
        }, $parameters->getRows());
106
    }
107
108
    /**
109
     * @When I use the default execution context
110
     */
111
    public function iUseTheDefaultExecutionContext()
112
    {
113
        $this->executionContext = $this->getDefaultExecutionContext();
114
    }
115
116
    /**
117
     * @When I use the default dataset
118
     */
119
    public function iUseTheDefaultDataset()
120
    {
121
        $this->dataset = $this->getDefaultDataset();
122
    }
123
124
    /**
125
     * @When I filter the dataset with the rule:
126
     */
127
    public function iFilterTheDatasetWithTheRule(PyStringNode $rule)
128
    {
129
        $this->results = $this->rulerz->filter($this->dataset, (string) $rule, $this->parameters, $this->executionContext);
130
131
        $this->parameters = [];
132
        $this->executionContext = [];
133
    }
134
135
    /**
136
     * @Then I should have the following results:
137
     */
138
    public function iShouldHaveTheFollowingResults(TableNode $table)
139
    {
140
        $results = iterator_to_array($this->results);
141
142
        if (count($table->getHash()) !== count($results)) {
143
            throw new \RuntimeException(sprintf("Expected %d results, got %d. Expected:\n%s\nGot:\n%s", count($table->getHash()), count($results), $table, var_export($results, true)));
144
        }
145
146
        foreach ($table as $row) {
147
            foreach ($results as $result) {
148
                $value = $this->fieldFromResult($result, 'pseudo');
149
150
                if ($value === $row['pseudo']) {
151
                    return;
152
                }
153
            }
154
155
            throw new \RuntimeException(sprintf('Player "%s" not found in the results.', $row['pseudo']));
156
        }
157
    }
158
159
    /**
160
     * Fetches a field from a result.
161
     * Meant to be overriden by other contexts.
162
     *
163
     * @param mixed $result
164
     * @param string $field
165
     *
166
     * @return mixed
167
     */
168
    protected function fieldFromResult($result, $field)
169
    {
170
        $objectResult = is_array($result) ? (object) $result : $result;
171
172
        return $objectResult->$field;
173
    }
174
}
175