Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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