Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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  /**
  18   * Unit tests for core_table\local\filter\filter.
  19   *
  20   * @package   core_table
  21   * @category  test
  22   * @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU Public License
  24   */
  25  
  26  declare(strict_types=1);
  27  
  28  namespace core_table\local\filter;
  29  
  30  use advanced_testcase;
  31  use TypeError;
  32  
  33  /**
  34   * Unit tests for core_table\local\filter\integer_filter.
  35   *
  36   * @package   core_table
  37   * @category  test
  38   * @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class integer_filter_test extends advanced_testcase {
  42      /**
  43       * Ensure that the add_filter_value function works as expected with valid values.
  44       */
  45      public function test_add_filter_value_int(): void {
  46          $filter = new integer_filter('example');
  47  
  48          // Initially an empty list.
  49          $this->assertEmpty($filter->get_filter_values());
  50  
  51          // Adding a value should return that value.
  52          $filter->add_filter_value(10);
  53          $this->assertSame([
  54              10,
  55          ], $filter->get_filter_values());
  56  
  57          // Adding a second value should add that value.
  58          // The values should sorted.
  59          $filter->add_filter_value(2);
  60          $this->assertSame([
  61              2,
  62              10,
  63          ], $filter->get_filter_values());
  64  
  65          // Adding a duplicate value should not lead to that value being added again.
  66          $filter->add_filter_value(10);
  67          $this->assertSame([
  68              2,
  69              10,
  70          ], $filter->get_filter_values());
  71      }
  72  
  73      /**
  74       * Ensure that the add_filter_value function rejects invalid types.
  75       *
  76       * @dataProvider add_filter_value_invalid_types_provider
  77       * @param mixed $value
  78       * @param string $type
  79       */
  80      public function test_add_filter_value_type_invalid($value, string $type): void {
  81          $filter = new integer_filter('example');
  82  
  83          // Adding empty string is not supported.
  84          $this->expectException(TypeError::class);
  85          $this->expectExceptionMessage("The value supplied was of type '{$type}'. An integer was expected.");
  86          $filter->add_filter_value($value);
  87      }
  88  
  89      /**
  90       * Data provider for add_filter_value tests with invalid types.
  91       *
  92       * @return array
  93       */
  94      public function add_filter_value_invalid_types_provider(): array {
  95          return [
  96              'Null' => [null, 'NULL'],
  97              'Empty string' => ['', 'string'],
  98              'Filled string' => ['example', 'string'],
  99              'Float 1.0' => [1.0, 'double'],
 100              'Float 1.1' => [1.1, 'double'],
 101              'bool' => [false, 'boolean'],
 102              'array' => [[], 'array'],
 103              'stdClass' => [(object) [], 'stdClass'],
 104  
 105              // Note: The comparison value will be a fully-qualfied class name.
 106              'Class' => [new filter('example'), filter::class],
 107          ];
 108      }
 109  }