Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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   * std_proxy tests.
  19   *
  20   * @package    core_calendar
  21   * @copyright  2017 Cameron Ball <cameron@cameron1729.xyz>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use core_calendar\local\event\proxies\std_proxy;
  28  
  29  /**
  30   * std_proxy testcase.
  31   *
  32   * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
  33   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class core_calendar_std_proxy_testcase extends advanced_testcase {
  36      /**
  37       * @var \stdClass[] $objects Array of objects to proxy.
  38       */
  39      public $objects;
  40  
  41      public function setUp(): void {
  42          $this->objects = [
  43              1 => (object) [
  44                  'member1' => 'Hello',
  45                  'member2' => 1729,
  46                  'member3' => 'Something else'
  47              ],
  48              5 => (object) [
  49                  'member1' => 'Hej',
  50                  'member2' => 87539319,
  51                  'member3' => 'nagot annat'
  52              ]
  53          ];
  54      }
  55  
  56      /**
  57       * Test proxying.
  58       *
  59       * @dataProvider test_proxy_testcases()
  60       * @param int    $id       Object ID.
  61       * @param string $member   Object member to retrieve.
  62       * @param mixed  $expected Expected value of member.
  63       */
  64      public function test_proxy($id, $member, $expected) {
  65          $proxy = new std_proxy($id, function($id) {
  66              return $this->objects[$id];
  67          });
  68  
  69          $this->assertEquals($proxy->get($member), $expected);
  70      }
  71  
  72      /**
  73       * Test setting values with a base class.
  74       *
  75       * @dataProvider test_proxy_testcases()
  76       * @param int    $id          Object ID.
  77       * @param string $member      Object member to retrieve.
  78       * @param mixed  $storedvalue Value as would be stored externally.
  79       */
  80      public function test_base_values($id, $member, $storedvalue) {
  81          $proxy = new std_proxy(
  82              $id,
  83              function($id) {
  84                  return $this->objects[$id];
  85              },
  86              (object)['member1' => 'should clobber 1']
  87          );
  88  
  89          $expected = $member == 'member1' ? 'should clobber 1' : $storedvalue;
  90          $this->assertEquals($proxy->get($member), $expected);
  91      }
  92  
  93      /**
  94       * Test getting a non existant member.
  95       *
  96       * @dataProvider test_get_set_testcases()
  97       * @param int $id ID of the object being proxied.
  98       */
  99      public function test_get_invalid_member($id) {
 100          $proxy = new std_proxy($id, function($id) {
 101              return $this->objects[$id];
 102          });
 103  
 104          $this->expectException('\core_calendar\local\event\exceptions\member_does_not_exist_exception');
 105          $proxy->get('thisdoesnotexist');
 106      }
 107  
 108      /**
 109       * Test get proxied instance.
 110       *
 111       * @dataProvider test_get_set_testcases()
 112       * @param int $id Object ID.
 113       */
 114      public function test_get_proxied_instance($id) {
 115          $proxy = new std_proxy($id, function($id) {
 116              return $this->objects[$id];
 117          });
 118  
 119          $this->assertEquals($proxy->get_proxied_instance(), $this->objects[$id]);
 120      }
 121  
 122      /**
 123       * Test cases for proxying test.
 124       */
 125      public function test_proxy_testcases() {
 126          return [
 127              'Object 1 member 1' => [
 128                  1,
 129                  'member1',
 130                  'Hello'
 131              ],
 132              'Object 1 member 2' => [
 133                  1,
 134                  'member2',
 135                  1729
 136              ],
 137              'Object 1 member 3' => [
 138                  1,
 139                  'member3',
 140                  'Something else'
 141              ],
 142              'Object 2 member 1' => [
 143                  5,
 144                  'member1',
 145                  'Hej'
 146              ],
 147              'Object 2 member 2' => [
 148                  5,
 149                  'member2',
 150                  87539319
 151              ],
 152              'Object 3 member 3' => [
 153                  5,
 154                  'member3',
 155                  'nagot annat'
 156              ]
 157          ];
 158      }
 159  
 160      /**
 161       * Test cases for getting and setting tests.
 162       */
 163      public function test_get_set_testcases() {
 164          return [
 165              'Object 1' => [1],
 166              'Object 2' => [5]
 167          ];
 168      }
 169  }