Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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 enrol_lti\local\ltiadvantage\lib;
  18  
  19  use Packback\Lti1p3\Interfaces\IHttpResponse;
  20  
  21  /**
  22   * Tests for the http_exception class.
  23   *
  24   * @package enrol_lti
  25   * @copyright 2022 Jake Dallimore <jrhdallimore@gmail.com>
  26   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   * @coversDefaultClass \enrol_lti\local\ltiadvantage\lib\http_exception
  28   */
  29  class http_exception_test extends \basic_testcase {
  30  
  31      /**
  32       * Test constructor and getters for a range of inputs.
  33       *
  34       * @dataProvider exception_data_provider
  35       * @param array $args the arguments to the exception constructor.
  36       * @covers ::__construct
  37       */
  38      public function test_exception(array $args) {
  39  
  40          $exception = new http_exception(...array_values($args));
  41          $this->assertInstanceOf(IHttpResponse::class, $exception->getResponse());
  42          if (isset($args['message'])) {
  43              $this->assertEquals($args['message'], $exception->getMessage());
  44          }
  45          if (isset($args['code'])) {
  46              $this->assertEquals($args['code'], $exception->getCode());
  47          }
  48          if (isset($args['throwable'])) {
  49              $this->assertEquals($args['throwable'], $exception->getPrevious());
  50          }
  51      }
  52  
  53      /**
  54       * Data provider for testing http_exception instances.
  55       *
  56       * @return array the test case data.
  57       */
  58      public function exception_data_provider() {
  59          return [
  60              'With only the response object' => [
  61                  'args' => [
  62                      'response' => new http_response(
  63                          ['body' => '', 'headers' => ['Content-Type' => 'application/json']],
  64                          401
  65                      )
  66                  ]
  67              ],
  68              'With response and message, code and throwable omitted' => [
  69                  'args' => [
  70                      'response' => new http_response(
  71                          ['body' => '', 'headers' => ['Content-Type' => 'application/json']],
  72                          401
  73                      ),
  74                      'message' => 'HTTP error: 401 Unauthorised'
  75                  ]
  76              ],
  77              'With response, message and code, throwable omitted' => [
  78                  'args' => [
  79                      'response' => new http_response(
  80                          ['body' => '', 'headers' => ['Content-Type' => 'application/json']],
  81                          401
  82                      ),
  83                      'message' => 'HTTP error: 401 Unauthorised',
  84                      'code' => 401
  85                  ]
  86              ],
  87              'With response, message, code, throwable' => [
  88                  'args' => [
  89                      'response' => new http_response(
  90                          ['body' => '', 'headers' => ['Content-Type' => 'application/json']],
  91                          401
  92                      ),
  93                      'message' => 'HTTP error: 401 Unauthorised',
  94                      'code' => 401,
  95                      'throwable' => new \Exception('another exception')
  96                  ]
  97              ]
  98          ];
  99      }
 100  }