Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace tool_generator\local\testscenario;
18
 
19
use behat_data_generators;
20
use Behat\Gherkin\Parser;
21
use Behat\Gherkin\Lexer;
22
use Behat\Gherkin\Keywords\ArrayKeywords;
23
use Behat\Gherkin\Node\StepNode;
24
 
25
/**
26
 * Tests for steprunner class.
27
 *
28
 * @package tool_generator
29
 * @copyright 2023 Ferran Recio <ferran@moodel.com>
30
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @coversDefaultClass \tool_generator\local\testscenario\steprunner
32
 */
33
class steprunner_test extends \advanced_testcase {
34
    public static function setUpBeforeClass(): void {
35
        // Call the init method to include all behat libraries and attributes.
36
        $runner = new runner();
37
        $runner->init();
38
    }
39
 
40
    /**
41
     * Get a step node from a string.
42
     * @param string $step the step string.
43
     * @return StepNode the step node.
44
     */
45
    private function get_step(string $step): StepNode {
46
        $content = 'Feature: Test feature
47
            Scenario: Test scenario
48
            ' . $step . '
49
        ';
50
 
51
        $method = new \ReflectionMethod(runner::class, 'get_parser');
52
        $parser = $method->invoke(new runner());
53
 
54
        $feature = $parser->parse($content);
55
        $scenario = $feature->getScenarios()[0];
56
        $steps = $scenario->getSteps();
57
        return $steps[0];
58
    }
59
 
60
    /**
61
     * Test for parse_feature.
62
     * @covers ::is_valid
63
     * @param string $step the step to validate.
64
     * @param bool $expected if the step is expected to be valid.
65
     * @dataProvider execute_steps_provider
66
     */
67
    public function test_is_valid(string $step, bool $expected): void {
68
        $generator = new behat_data_generators();
69
        $validsteps = [
70
            '/^the following "(?P<element_string>(?:[^"]|\\")*)" exist:$/' => 'the_following_entities_exist',
71
            ':count :entitytype exist with the following data:' => 'the_following_repeated_entities_exist',
72
            'the following :entitytype exists:' => 'the_following_entity_exists',
73
        ];
74
 
75
        $step = $this->get_step($step);
76
        $steprunner = new steprunner($generator, $validsteps, $step);
77
        $this->assertEquals($expected, $steprunner->is_valid());
78
    }
79
 
80
    /**
81
     * Test for execute step.
82
     *
83
     * @covers ::is_executed
84
     * @covers ::execute
85
     * @param string $step the step to execute.
86
     * @param bool $expected if the step is expected to be executed.
87
     * @dataProvider execute_steps_provider
88
     */
89
    public function test_execute(string $step, bool $expected): void {
90
        global $DB;
91
 
92
        $this->resetAfterTest();
93
 
94
        $generator = new behat_data_generators();
95
        $validsteps = [
96
            '/^the following "(?P<element_string>(?:[^"]|\\")*)" exist:$/' => 'the_following_entities_exist',
97
            ':count :entitytype exist with the following data:' => 'the_following_repeated_entities_exist',
98
            'the following :entitytype exists:' => 'the_following_entity_exists',
99
        ];
100
 
101
        $step = $this->get_step($step);
102
        $steprunner = new steprunner($generator, $validsteps, $step);
103
 
104
        $this->assertFalse($steprunner->is_executed());
105
 
106
        $result = $steprunner->execute();
107
 
108
        $this->assertEquals($expected, $result);
109
        $this->assertEquals($expected, $steprunner->is_executed());
110
 
111
        if ($expected) {
112
            // Validate everything is created.
113
            $this->assertEquals(
114
                1,
115
                $DB->count_records('course', ['shortname' => 'C1'])
116
            );
117
        }
118
    }
119
 
120
    /**
121
     * Data provider for test_execute.
122
     * @return array the data.
123
     */
124
    public static function execute_steps_provider(): array {
125
        return [
126
            'Following exists' => [
127
                'step' => 'Given the following "course" exists:
128
                    | fullname         | Course test |
129
                    | shortname        | C1          |
130
                    | category         | 0           |',
131
                'expected' => true,
132
            ],
133
            'Following exist' => [
134
                'step' => 'Given the following "course" exist:
135
                    | fullname         | shortname | category |
136
                    | Course test      | C1        | 0        |',
137
                'expected' => true,
138
            ],
139
            'Repeated entities' => [
140
                'step' => 'Given "1" "courses" exist with the following data:
141
                    | fullname    | Course test |
142
                    | shortname   | C[count]    |
143
                    | category    | 0           |
144
                    | numsections | 3           |',
145
                'expected' => true,
146
            ],
147
            'Invalid step' => [
148
                'step' => 'Given I click on "Tokens filter" "link"',
149
                'expected' => false,
150
            ],
151
        ];
152
    }
153
 
154
    /**
155
     * Test for execute step.
156
     * @covers ::is_executed
157
     * @covers ::execute
158
     * @covers ::get_error
159
     */
160
    public function test_execute_duplicated(): void {
161
        global $DB;
162
 
163
        $this->resetAfterTest();
164
 
165
        $generator = new behat_data_generators();
166
        $validsteps = [
167
            '/^the following "(?P<element_string>(?:[^"]|\\")*)" exist:$/' => 'the_following_entities_exist',
168
            ':count :entitytype exist with the following data:' => 'the_following_repeated_entities_exist',
169
            'the following :entitytype exists:' => 'the_following_entity_exists',
170
        ];
171
 
172
        $step = $this->get_step('Given the following "course" exists:
173
            | fullname         | Course test |
174
            | shortname        | C1          |
175
            | category         | 0           |');
176
        $steprunner = new steprunner($generator, $validsteps, $step);
177
 
178
        $this->assertFalse($steprunner->is_executed());
179
 
180
        $result = $steprunner->execute();
181
 
182
        $this->assertTrue($result);
183
        $this->assertTrue($steprunner->is_executed());
184
        $this->assertEquals('', $steprunner->get_error());
185
 
186
        // Validate everything is created.
187
        $this->assertEquals(
188
            1,
189
            $DB->count_records('course', ['shortname' => 'C1'])
190
        );
191
 
192
        // Execute the same course creation.
193
        $steprunner = new steprunner($generator, $validsteps, $step);
194
        $this->assertFalse($steprunner->is_executed());
195
        $result = $steprunner->execute();
196
        $this->assertFalse($result);
197
        $this->assertTrue($steprunner->is_executed());
198
        $this->assertEquals(get_string('shortnametaken', 'error', 'C1'), $steprunner->get_error());
199
    }
200
 
201
    /**
202
     * Test for parse_feature.
203
     * @covers ::get_text
204
     * @covers ::get_arguments_string
205
     */
206
    public function test_get_step_content(): void {
207
        $generator = new behat_data_generators();
208
        $validsteps = [
209
            '/^the following "(?P<element_string>(?:[^"]|\\")*)" exist:$/' => 'the_following_entities_exist',
210
            ':count :entitytype exist with the following data:' => 'the_following_repeated_entities_exist',
211
            'the following :entitytype exists:' => 'the_following_entity_exists',
212
        ];
213
 
214
        $step = $this->get_step('Given the following "course" exists:
215
            | fullname    | Course test |
216
            | shortname   | C1          |
217
            | category    | 0           |
218
            | numsections | 3           |');
219
        $steprunner = new steprunner($generator, $validsteps, $step);
220
 
221
        $this->assertEquals(
222
            'the following "course" exists:',
223
            $steprunner->get_text()
224
        );
225
 
226
        $data = [
227
            '| fullname    | Course test |',
228
            '| shortname   | C1          |',
229
            '| category    | 0           |',
230
            '| numsections | 3           |',
231
        ];
232
        $arguments = explode("\n", $steprunner->get_arguments_string());
233
        $this->assertEquals(
234
            $data,
235
            $arguments
236
        );
237
    }
238
}