Debian Codegraph @

tests/phpunit/integration/includes/StubObject/StubObjectTest.php

raw symbols SCIP
1 <?php
2
3 /**
4 * This program 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 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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 along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22 use MediaWiki\StubObject\StubObject;
23
24 /**
25 * Testing the magic for __get(), __set(), and __call() for our
26 * example global, $wgDummy, which would be an instance
27 * of DemoStubbed but is wrapped in a MediaWiki\StubObject\StubObject
28 * @author DannyS712
29 *
30 * @covers \MediaWiki\StubObject\StubObject
31 */
32 class StubObjectTest extends MediaWikiIntegrationTestCase {
33
34 /** @var int */
35 private $oldErrorLevel;
36
37 protected function setUp(): void {
38 parent::setUp();
39
40 // Make sure deprecation notices are seen
41 $this->oldErrorLevel = error_reporting( -1 );
42
43 global $wgDummy;
44 $wgDummy = new StubObject(
45 'wgDummy',
46 [ __CLASS__, 'factory' ]
47 );
48 }
49
50 protected function tearDown(): void {
51 error_reporting( $this->oldErrorLevel );
52 parent::tearDown();
53 }
54
55 /**
56 * Static factory method for creating the underlying global, which is
57 * a DemoStubbed with the starting value of 5
58 *
59 * @return DemoStubbed
60 */
61 public static function factory(): DemoStubbed {
62 return new DemoStubbed( 5 );
63 }
64
65 public function testCallMagic() {
66 global $wgDummy;
67 $this->assertInstanceOf(
68 StubObject::class,
69 $wgDummy,
70 'Global starts as stub object'
71 );
72 $this->assertSame(
73 5,
74 $wgDummy->getNum(),
75 '__call() based on id set in ::setUp()'
76 );
77 $this->assertInstanceOf(
78 DemoStubbed::class,
79 $wgDummy,
80 '__call() resulted in unstubbing'
81 );
82 }
83
84 public function testGetMagic() {
85 // MediaWiki\StubObject\StubObject::__get() returning DemoStubbed::$num
86 global $wgDummy;
87 $this->assertInstanceOf(
88 StubObject::class,
89 $wgDummy,
90 'Global starts as stub object'
91 );
92 $this->assertSame(
93 5,
94 $wgDummy->num,
95 '__get() based on id set in ::setUp()'
96 );
97 $this->assertInstanceOf(
98 DemoStubbed::class,
99 $wgDummy,
100 '__get() resulted in unstubbing'
101 );
102 }
103
104 public function testGetMagic_virtual() {
105 // MediaWiki\StubObject\StubObject::__get() calling DemoStubbed::__get()
106 global $wgDummy;
107 $this->assertInstanceOf(
108 StubObject::class,
109 $wgDummy,
110 'Global starts as stub object'
111 );
112 $this->assertSame(
113 10,
114 $wgDummy->doubleNum,
115 '__get() a virtual property based on id set in ::setUp()'
116 );
117 $this->assertInstanceOf(
118 DemoStubbed::class,
119 $wgDummy,
120 '__get() resulted in unstubbing'
121 );
122 }
123
124 public function testSetMagic() {
125 // MediaWiki\StubObject\StubObject::__set() changing DemoStubbed::$num
126 global $wgDummy;
127 $this->assertInstanceOf(
128 StubObject::class,
129 $wgDummy,
130 'Global starts as stub object'
131 );
132 $wgDummy->num = 100;
133 $this->assertInstanceOf(
134 DemoStubbed::class,
135 $wgDummy,
136 '__set() resulted in unstubbing'
137 );
138 $this->assertSame(
139 100,
140 $wgDummy->num,
141 '__set() changed the value'
142 );
143 }
144
145 public function testSetMagic_virtual() {
146 // MediaWiki\StubObject\StubObject::__set() calling DemoStubbed::__set()
147 global $wgDummy;
148 $this->assertInstanceOf(
149 StubObject::class,
150 $wgDummy,
151 'Global starts as stub object'
152 );
153 $wgDummy->doubleNum = 100;
154 $this->assertInstanceOf(
155 DemoStubbed::class,
156 $wgDummy,
157 '__set() resulted in unstubbing'
158 );
159 $this->assertSame(
160 50,
161 $wgDummy->num,
162 '__set() changed the value'
163 );
164 }
165 }
166
167 /**
168 * This is the object that we are stubbing so we can test the various magic methods
169 */
170 class DemoStubbed {
171
172 /** @var int */
173 public $num;
174
175 /**
176 * @param int $num
177 */
178 public function __construct( int $num ) {
179 $this->num = $num;
180 }
181
182 /**
183 * @return int
184 */
185 public function getNum(): int {
186 return $this->num;
187 }
188
189 /**
190 * Magic handling for retrieving fake property "doubleNum"
191 *
192 * @param string $field
193 * @return mixed
194 */
195 public function __get( $field ) {
196 if ( $field === 'doubleNum' ) {
197 return ( 2 * $this->num );
198 }
199 trigger_error( 'Inaccessible property via __get(): ' . $field, E_USER_NOTICE );
200 }
201
202 /**
203 * Magic handling for setting fake property "doubleNum"
204 *
205 * @param string $field
206 * @param mixed $value
207 */
208 public function __set( $field, $value ) {
209 if ( $field === 'doubleNum' ) {
210 $this->num = (int)( $value / 2 );
211 return;
212 }
213 trigger_error( 'Inaccessible property via __set(): ' . $field, E_USER_NOTICE );
214 }
215
216 }