Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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  use core_external\external_api;
  18  use core_external\external_function_parameters;
  19  use core_external\external_single_structure;
  20  
  21  /**
  22   * An external function that throws an exception, for tests.
  23   *
  24   * @package    core
  25   * @category   phpunit
  26   * @copyright  2020 Dani Palou
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class test_external_function_throwable extends external_api {
  30  
  31      /**
  32       * Returns description of throw_exception() parameters.
  33       *
  34       * @return external_function_parameters
  35       */
  36      public static function throw_exception_parameters() {
  37          return new external_function_parameters(array());
  38      }
  39  
  40      /**
  41       * Throws a PHP error.
  42       *
  43       * @return array empty array.
  44       */
  45      public static function throw_exception() {
  46          $a = 1 % 0;
  47  
  48          return array();
  49      }
  50  
  51      /**
  52       * Returns description of throw_exception() result value.
  53       *
  54       * @return \core_external\external_description
  55       */
  56      public static function throw_exception_returns() {
  57          return new external_single_structure(array());
  58      }
  59  
  60      /**
  61       * Override external_function_info to accept our fake WebService.
  62       */
  63      public static function external_function_info($function, $strictness=MUST_EXIST) {
  64          if ($function == 'core_throw_exception') {
  65              // Convert it to an object.
  66              $function = new stdClass();
  67              $function->name = $function;
  68              $function->classname = 'test_external_function_throwable';
  69              $function->methodname = 'throw_exception';
  70              $function->classpath = ''; // No need to define class path because current file is already loaded.
  71              $function->component = 'fake';
  72              $function->capabilities = '';
  73              $function->services = 'moodle_mobile_app';
  74              $function->loginrequired = false;
  75          }
  76  
  77          return parent::external_function_info($function, $strictness);
  78      }
  79  }