Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400]

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