Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 */
1441 ariadna 33
final class steprunner_test extends \advanced_testcase {
1 efrain 34
    public static function setUpBeforeClass(): void {
1441 ariadna 35
        parent::setUpBeforeClass();
1 efrain 36
        // Call the init method to include all behat libraries and attributes.
37
        $runner = new runner();
38
        $runner->init();
39
    }
40
 
41
    /**
42
     * Get a step node from a string.
43
     * @param string $step the step string.
44
     * @return StepNode the step node.
45
     */
46
    private function get_step(string $step): StepNode {
47
        $content = 'Feature: Test feature
48
            Scenario: Test scenario
49
            ' . $step . '
50
        ';
51
 
52
        $method = new \ReflectionMethod(runner::class, 'get_parser');
53
        $parser = $method->invoke(new runner());
54
 
55
        $feature = $parser->parse($content);
56
        $scenario = $feature->getScenarios()[0];
57
        $steps = $scenario->getSteps();
58
        return $steps[0];
59
    }
60
 
61
    /**
1441 ariadna 62
     * Get the list of valid behat steps for the tests.
63
     * @return array the valid steps details.
64
     */
65
    private function get_valid_steps(): array {
66
        $generator = new behat_data_generators();
67
        return [
68
            '/^the following "(?P<element_string>(?:[^"]|\\")*)" exist:$/' => (object) [
69
                'name' => 'the_following_entities_exist',
70
                'given' => '/^the following "(?P<element_string>(?:[^"]|\\")*)" exist:$/',
71
                'generator' => $generator,
72
            ],
73
            ':count :entitytype exist with the following data:' => (object) [
74
                'name' => 'the_following_repeated_entities_exist',
75
                'given' => ':count :entitytype exist with the following data:',
76
                'generator' => $generator,
77
            ],
78
            'the following :entitytype exists:' => (object) [
79
                'name' => 'the_following_entity_exists',
80
                'given' => 'the following :entitytype exists:',
81
                'generator' => $generator,
82
            ],
83
        ];
84
    }
85
 
86
    /**
1 efrain 87
     * Test for parse_feature.
88
     * @covers ::is_valid
89
     * @param string $step the step to validate.
90
     * @param bool $expected if the step is expected to be valid.
91
     * @dataProvider execute_steps_provider
92
     */
93
    public function test_is_valid(string $step, bool $expected): void {
1441 ariadna 94
        $validsteps = $this->get_valid_steps();
1 efrain 95
        $step = $this->get_step($step);
1441 ariadna 96
        $steprunner = new steprunner(null, $validsteps, $step);
1 efrain 97
        $this->assertEquals($expected, $steprunner->is_valid());
98
    }
99
 
100
    /**
101
     * Test for execute step.
102
     *
103
     * @covers ::is_executed
104
     * @covers ::execute
105
     * @param string $step the step to execute.
106
     * @param bool $expected if the step is expected to be executed.
107
     * @dataProvider execute_steps_provider
108
     */
109
    public function test_execute(string $step, bool $expected): void {
110
        global $DB;
111
 
112
        $this->resetAfterTest();
113
 
1441 ariadna 114
        $validsteps = $this->get_valid_steps();
1 efrain 115
 
116
        $step = $this->get_step($step);
1441 ariadna 117
        $steprunner = new steprunner(null, $validsteps, $step);
1 efrain 118
 
119
        $this->assertFalse($steprunner->is_executed());
120
 
121
        $result = $steprunner->execute();
122
 
123
        $this->assertEquals($expected, $result);
124
        $this->assertEquals($expected, $steprunner->is_executed());
125
 
126
        if ($expected) {
127
            // Validate everything is created.
128
            $this->assertEquals(
129
                1,
130
                $DB->count_records('course', ['shortname' => 'C1'])
131
            );
132
        }
133
    }
134
 
135
    /**
136
     * Data provider for test_execute.
137
     * @return array the data.
138
     */
139
    public static function execute_steps_provider(): array {
140
        return [
141
            'Following exists' => [
142
                'step' => 'Given the following "course" exists:
143
                    | fullname         | Course test |
144
                    | shortname        | C1          |
145
                    | category         | 0           |',
146
                'expected' => true,
147
            ],
148
            'Following exist' => [
149
                'step' => 'Given the following "course" exist:
150
                    | fullname         | shortname | category |
151
                    | Course test      | C1        | 0        |',
152
                'expected' => true,
153
            ],
154
            'Repeated entities' => [
155
                'step' => 'Given "1" "courses" exist with the following data:
156
                    | fullname    | Course test |
157
                    | shortname   | C[count]    |
158
                    | category    | 0           |
159
                    | numsections | 3           |',
160
                'expected' => true,
161
            ],
162
            'Invalid step' => [
163
                'step' => 'Given I click on "Tokens filter" "link"',
164
                'expected' => false,
165
            ],
166
        ];
167
    }
168
 
169
    /**
170
     * Test for execute step.
171
     * @covers ::is_executed
172
     * @covers ::execute
173
     * @covers ::get_error
174
     */
175
    public function test_execute_duplicated(): void {
176
        global $DB;
177
 
178
        $this->resetAfterTest();
179
 
1441 ariadna 180
        $validsteps = $this->get_valid_steps();
1 efrain 181
 
182
        $step = $this->get_step('Given the following "course" exists:
183
            | fullname         | Course test |
184
            | shortname        | C1          |
185
            | category         | 0           |');
1441 ariadna 186
        $steprunner = new steprunner(null, $validsteps, $step);
1 efrain 187
 
188
        $this->assertFalse($steprunner->is_executed());
189
 
190
        $result = $steprunner->execute();
191
 
192
        $this->assertTrue($result);
193
        $this->assertTrue($steprunner->is_executed());
194
        $this->assertEquals('', $steprunner->get_error());
195
 
196
        // Validate everything is created.
197
        $this->assertEquals(
198
            1,
199
            $DB->count_records('course', ['shortname' => 'C1'])
200
        );
201
 
202
        // Execute the same course creation.
1441 ariadna 203
        $steprunner = new steprunner(null, $validsteps, $step);
1 efrain 204
        $this->assertFalse($steprunner->is_executed());
205
        $result = $steprunner->execute();
206
        $this->assertFalse($result);
207
        $this->assertTrue($steprunner->is_executed());
208
        $this->assertEquals(get_string('shortnametaken', 'error', 'C1'), $steprunner->get_error());
209
    }
210
 
211
    /**
212
     * Test for parse_feature.
213
     * @covers ::get_text
214
     * @covers ::get_arguments_string
215
     */
216
    public function test_get_step_content(): void {
217
        $step = $this->get_step('Given the following "course" exists:
1441 ariadna 218
        | fullname    | Course test |
219
        | shortname   | C1          |
220
        | category    | 0           |
221
        | numsections | 3           |');
1 efrain 222
 
1441 ariadna 223
        $validsteps = $this->get_valid_steps();
224
        $steprunner = new steprunner(null, $validsteps, $step);
225
 
1 efrain 226
        $this->assertEquals(
227
            'the following "course" exists:',
228
            $steprunner->get_text()
229
        );
230
 
231
        $data = [
232
            '| fullname    | Course test |',
233
            '| shortname   | C1          |',
234
            '| category    | 0           |',
235
            '| numsections | 3           |',
236
        ];
237
        $arguments = explode("\n", $steprunner->get_arguments_string());
238
        $this->assertEquals(
239
            $data,
240
            $arguments
241
        );
242
    }
243
}