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 - https://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 <https://www.gnu.org/licenses/>.
16
 
17
namespace tool_generator\local\testscenario;
18
 
19
use stdClass;
20
 
21
/**
22
 * Class with a scenario feature parsed.
23
 *
24
 * @package    tool_generator
25
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
class parsedfeature {
29
    /** @var int the number of steps. */
30
    private int $stepcount = 0;
31
 
32
    /** @var bool if the parser is ok or fail. */
33
    private bool $isvalid = true;
34
 
35
    /** @var stdClass[] the list of scenarios with all the steps.
36
     *
37
     * scenarionum => {type: string, title: string, steps: steprunner[]}.
38
     */
39
    private array $scenarios = [];
40
 
41
    /**
42
     * Get the general error, if any.
43
     * @return string
44
     */
45
    public function get_general_error(): string {
46
        if (!$this->isvalid) {
47
            return get_string('testscenario_invalidfile', 'tool_generator');
48
        }
49
        if ($this->stepcount == 0) {
50
            return get_string('testscenario_nosteps', 'tool_generator');
51
        }
52
        return '';
53
    }
54
 
55
    /**
56
     * Check if the parsed feature is valid.
57
     * @return bool
58
     */
59
    public function is_valid(): bool {
60
        return $this->isvalid && $this->stepcount > 0;
61
    }
62
 
63
    /**
64
     * Add a line to the current scenario.
65
     * @param steprunner $step the step to add.
66
     */
67
    public function add_step(steprunner $step) {
68
        if (empty($this->scenarios)) {
69
            $this->add_scenario('scenario', null);
70
        }
71
        $currentscenario = count($this->scenarios) - 1;
72
        $this->scenarios[$currentscenario]->steps[] = $step;
73
        $this->stepcount++;
74
        if (!$step->is_valid()) {
75
            $this->isvalid = false;
76
        }
77
    }
78
 
79
    /**
80
     * Insert a new scenario.
81
     * @param string $type the type of the scenario.
82
     * @param string|null $name the name of the scenario.
83
     */
84
    public function add_scenario(string $type, ?string $name) {
85
        $this->scenarios[] = (object) [
86
            'type' => $type,
87
            'name' => $name ?? '',
88
            'steps' => [],
89
            'error' => '',
90
        ];
91
    }
92
 
93
    /**
94
     * Add an error to the current scenario.
95
     * @param string $error
96
     */
97
    public function add_error(string $error) {
98
        $currentscenario = count($this->scenarios) - 1;
99
        $this->scenarios[$currentscenario]->error = $error;
100
    }
101
 
102
    /**
103
     * Get the list of scenarios.
104
     * @return stdClass[] array of scenarionum => {type: string, title: string, steps: steprunner[]}
105
     */
106
    public function get_scenarios(): array {
107
        return $this->scenarios;
108
    }
109
 
110
    /**
111
     * Get all the steps form all scenarios.
112
     * @return steprunner[]
113
     */
114
    public function get_all_steps(): array {
115
        $result = [];
116
        foreach ($this->scenarios as $scenario) {
117
            foreach ($scenario->steps as $step) {
118
                $result[] = $step;
119
            }
120
        }
121
        return $result;
122
    }
123
}