| Línea 14... | Línea 14... | 
          
            | 14 | // You should have received a copy of the GNU General Public License
 | 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/>.
 | 15 | // along with Moodle.  If not, see <https://www.gnu.org/licenses/>.
 | 
          
            | Línea 16... | Línea 16... | 
          
            | 16 |  
 | 16 |  
 | 
          
            | Línea 17... | Línea 17... | 
          
            | 17 | namespace tool_generator\local\testscenario;
 | 17 | namespace tool_generator\local\testscenario;
 | 
          
            | 18 |  
 | 18 |  
 | 
          
            | Línea 19... | Línea 19... | 
          
            | 19 | use behat_data_generators;
 | 19 | use behat_base;
 | 
          
            | 20 | use Behat\Gherkin\Node\StepNode;
 | 20 | use Behat\Gherkin\Node\StepNode;
 | 
          
            | 21 |  
 | 21 |  
 | 
          
            | 22 | /**
 | 22 | /**
 | 
          
            | 23 |  * Class to validate and process a scenario step.
 | 23 |  * Class to validate and process a scenario step.
 | 
          
            | 24 |  *
 | 24 |  *
 | 
          
            | 25 |  * @package    tool_generator
 | 25 |  * @package    tool_generator
 | 
          
            | 26 |  * @copyright  2023 Ferran Recio <ferran@moodle.com>
 | 26 |  * @copyright  2023 Ferran Recio <ferran@moodle.com>
 | 
          
            | 27 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 27 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
          
            | 28 |  */
 | 28 |  */
 | 
          
            | Línea 29... | Línea 29... | 
          
            | 29 | class steprunner {
 | 29 | class steprunner {
 | 
          
            | 30 |     /** @var behat_data_generators the behat data generator instance. */
 | 30 |     /** @var behat_base|null the behat step class instance. */
 | 
          
            | Línea 31... | Línea 31... | 
          
            | 31 |     private behat_data_generators $generator;
 | 31 |     private ?behat_base $generator = null;
 | 
          
            | Línea 51... | Línea 51... | 
          
            | 51 |     /** @var string the error message if any. */
 | 51 |     /** @var string the error message if any. */
 | 
          
            | 52 |     private string $error = '';
 | 52 |     private string $error = '';
 | 
          
            | Línea 53... | Línea 53... | 
          
            | 53 |  
 | 53 |  
 | 
          
            | 54 |     /**
 | 54 |     /**
 | 
          
            | 55 |      * Constructor.
 | 55 |      * Constructor.
 | 
          
            | 56 |      * @param behat_data_generators $generator the behat data generator instance.
 | 56 |      * @param behat_base|null $unused This does nothing, do not use it.
 | 
          
            | 57 |      * @param array $validsteps the valid steps indexed by given expression tag.
 | 57 |      * @param array $validsteps the valid steps indexed by given expression tag.
 | 
          
            | 58 |      * @param StepNode $stepnode the step node to process.
 | 58 |      * @param StepNode $stepnode the step node to process.
 | 
          
            | 59 |      */
 | 59 |      */
 | 
          
            | 60 |     public function __construct(behat_data_generators $generator, array $validsteps, StepNode $stepnode) {
 | 60 |     public function __construct($unused, array $validsteps, StepNode $stepnode) {
 | 
          
            | - |   | 61 |         if ($unused !== null) {
 | 
          
            | - |   | 62 |             debugging('Deprecated argument passed to ' . __FUNCTION__, DEBUG_DEVELOPER);
 | 
          
            | 61 |         $this->generator = $generator;
 | 63 |         }
 | 
          
            | 62 |         $this->validsteps = $validsteps;
 | 64 |         $this->validsteps = $validsteps;
 | 
          
            | 63 |         $this->stepnode = $stepnode;
 | 65 |         $this->stepnode = $stepnode;
 | 
          
            | 64 |         $this->init();
 | 66 |         $this->init();
 | 
          
            | Línea 71... | Línea 73... | 
          
            | 71 |      * in case it is executed.
 | 73 |      * in case it is executed.
 | 
          
            | 72 |      */
 | 74 |      */
 | 
          
            | 73 |     private function init() {
 | 75 |     private function init() {
 | 
          
            | 74 |         $matches = [];
 | 76 |         $matches = [];
 | 
          
            | 75 |         $linetext = $this->stepnode->getText();
 | 77 |         $linetext = $this->stepnode->getText();
 | 
          
            | 76 |         foreach ($this->validsteps as $pattern => $method) {
 | 78 |         foreach ($this->validsteps as $method) {
 | 
          
            | 77 |             if (!$this->match_given($pattern, $linetext, $matches)) {
 | 79 |             if (!$this->match_given($method->given, $linetext, $matches)) {
 | 
          
            | 78 |                 continue;
 | 80 |                 continue;
 | 
          
            | 79 |             }
 | 81 |             }
 | 
          
            | 80 |             $this->method = $method;
 | 82 |             $this->method = $method->name;
 | 
          
            | 81 |             $this->params = $this->build_method_params($method, $matches);
 | 83 |             $this->params = $this->build_method_params($method->name, $matches, $method->generator);
 | 
          
            | - |   | 84 |             $this->generator = $method->generator;
 | 
          
            | 82 |             $this->isvalid = true;
 | 85 |             $this->isvalid = true;
 | 
          
            | 83 |             return;
 | 86 |             return;
 | 
          
            | 84 |         }
 | 87 |         }
 | 
          
            | 85 |         $this->error = get_string('testscenario_invalidstep', 'tool_generator');
 | 88 |         $this->error = get_string('testscenario_invalidstep', 'tool_generator');
 | 
          
            | 86 |     }
 | 89 |     }
 | 
          
            | Línea 87... | Línea 90... | 
          
            | 87 |  
 | 90 |  
 | 
          
            | 88 |     /**
 | 91 |     /**
 | 
          
            | 89 |      * Build the method parameters.
 | 92 |      * Build the method parameters.
 | 
          
            | 90 |      * @param string $methodname the method name.
 | 93 |      * @param string $methodname the method name.
 | 
          
            | - |   | 94 |      * @param array $matches the matches.
 | 
          
            | 91 |      * @param array $matches the matches.
 | 95 |      * @param behat_base $generator the method class.
 | 
          
            | 92 |      * @return array the method parameters.
 | 96 |      * @return array the method parameters.
 | 
          
            | 93 |      */
 | 97 |      */
 | 
          
            | 94 |     private function build_method_params($methodname, $matches) {
 | 98 |     private function build_method_params(string $methodname, array $matches, behat_base $generator) {
 | 
          
            | 95 |         $method = new \ReflectionMethod($this->generator, $methodname);
 | 99 |         $method = new \ReflectionMethod($generator, $methodname);
 | 
          
            | 96 |         $params = [];
 | 100 |         $params = [];
 | 
          
            | 97 |         foreach ($method->getParameters() as $param) {
 | 101 |         foreach ($method->getParameters() as $param) {
 | 
          
            | 98 |             $paramname = $param->getName();
 | 102 |             $paramname = $param->getName();
 | 
          
            | 99 |             if (isset($matches[$paramname])) {
 | 103 |             if (isset($matches[$paramname])) {
 | 
          
            | 100 |                 $params[] = $matches[$paramname];
 | 104 |                 $params[] = $matches[$paramname];
 | 
          
            | - |   | 105 |                 unset($matches[$paramname]);
 | 
          
            | - |   | 106 |             } else if (isset($matches["{$paramname}_string"])) {
 | 
          
            | - |   | 107 |                 // If the param uses a regular expression with a name.
 | 
          
            | - |   | 108 |                 $params[] = $matches["{$paramname}_string"];
 | 
          
            | 101 |                 unset($matches[$paramname]);
 | 109 |                 unset($matches["{$paramname}_string"]);
 | 
          
            | 102 |             } else if (count($matches) > 0) {
 | 110 |             } else if (count($matches) > 0) {
 | 
          
            | 103 |                 // If the param is not present means the regular expressions does not use
 | 111 |                 // If the param is not present means the regular expressions does not use
 | 
          
            | 104 |                 // proper names. So we will try to find the param by position.
 | 112 |                 // proper names. So we will try to find the param by position.
 | 
          
            | 105 |                 $params[] = array_pop($matches);
 | 113 |                 $params[] = array_pop($matches);
 |