Debian Codegraph @

tests/phpunit/includes/debug/DeprecationHelperTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
4 use Wikimedia\TestingAccessWrapper;
5
6 /**
7 * @covers \MediaWiki\Debug\DeprecationHelper
8 */
9 class DeprecationHelperTest extends MediaWikiIntegrationTestCase {
10
11 /** @var TestDeprecatedClass */
12 private $testClass;
13
14 /** @var TestDeprecatedSubclass */
15 private $testSubclass;
16
17 protected function setUp(): void {
18 parent::setUp();
19 $this->testClass = new TestDeprecatedClass();
20 $this->testSubclass = new TestDeprecatedSubclass();
21 $this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
22 }
23
24 /**
25 * @dataProvider provideGet
26 */
27 public function testGet( $propName, $expectError, string $expectedMessage ) {
28 if ( $expectError === true ) {
29 try {
30 $this->testClass->$propName;
31 } catch ( Throwable $e ) {
32 $this->assertInstanceOf( Error::class, $e );
33 $this->assertSame( $expectedMessage, $e->getMessage() );
34 return;
35 }
36 $this->fail( 'Expected an error' );
37 } elseif ( $expectError ) {
38 $this->assertErrorTriggered( function () use ( $propName ) {
39 $this->assertSame( null, $this->testClass->$propName );
40 }, $expectError, $expectedMessage );
41 } else {
42 $this->assertDeprecationWarningIssued( function () use ( $propName ) {
43 $expectedValue = $propName === 'fallbackDeprecatedMethodName' ? 'FOO' : 1;
44 $this->assertSame( $expectedValue, $this->testClass->$propName );
45 }, $expectedMessage );
46 }
47 }
48
49 public static function provideGet() {
50 return [
51 [ 'protectedDeprecated', null,
52 'Use of TestDeprecatedClass::$protectedDeprecated was deprecated in MediaWiki 1.23. ' .
53 '[Called from DeprecationHelperTest::{closure' ],
54 [ 'privateDeprecated', null,
55 'Use of TestDeprecatedClass::$privateDeprecated was deprecated in MediaWiki 1.24. ' .
56 '[Called from DeprecationHelperTest::{closure' ],
57 [ 'fallbackDeprecated', null,
58 'Use of TestDeprecatedClass::$fallbackDeprecated was deprecated in MediaWiki 1.25. ' .
59 '[Called from DeprecationHelperTest::{closure' ],
60 [ 'fallbackDeprecatedMethodName', null,
61 'Use of TestDeprecatedClass::$fallbackDeprecatedMethodName was deprecated in MediaWiki 1.26. ' .
62 '[Called from DeprecationHelperTest::{closure' ],
63 [ 'fallbackGetterOnly', null,
64 'Use of TestDeprecatedClass::$fallbackGetterOnly was deprecated in MediaWiki 1.25. ' .
65 '[Called from DeprecationHelperTest::{closure' ],
66 [ 'protectedNonDeprecated', true,
67 'Cannot access non-public property TestDeprecatedClass::$protectedNonDeprecated' ],
68 [ 'privateNonDeprecated', true,
69 'Cannot access non-public property TestDeprecatedClass::$privateNonDeprecated' ],
70 [ 'nonExistent', E_USER_NOTICE, 'Undefined property: TestDeprecatedClass::$nonExistent' ],
71 ];
72 }
73
74 public function testDeprecateDynamicPropertyAccess() {
75 $testObject = new class extends TestDeprecatedClass {
76 public function __construct() {
77 parent::__construct();
78 $this->deprecateDynamicPropertiesAccess( '1.23' );
79 }
80 };
81 $this->assertDeprecationWarningIssued(
82 static function () use ( $testObject ) {
83 $testObject->dynamic_property = 'bla';
84 },
85 'Use of TestDeprecatedClass::$dynamic_property was deprecated in MediaWiki 1.23. ' .
86 '[Called from DeprecationHelperTest::{closure'
87 );
88 }
89
90 public function testDynamicPropertyNullCoalesce() {
91 $testObject = new TestDeprecatedClass();
92 $this->assertSame( 'bla', $testObject->dynamic_property ?? 'bla' );
93 }
94
95 public function testDynamicPropertyNullCoalesceDeprecated() {
96 $testObject = new class extends TestDeprecatedClass {
97 public function __construct() {
98 parent::__construct();
99 $this->deprecateDynamicPropertiesAccess( '1.23' );
100 }
101 };
102 $this->assertDeprecationWarningIssued(
103 function () use ( $testObject ) {
104 $this->assertSame( 'bla', $testObject->dynamic_property ?? 'bla' );
105 },
106 'Use of TestDeprecatedClass::$dynamic_property was deprecated in MediaWiki 1.23. ' .
107 '[Called from DeprecationHelperTest::{closure'
108 );
109 }
110
111 public function testDynamicPropertyOnMockObject() {
112 $testObject = $this->getMockBuilder( TestDeprecatedClass::class )
113 ->enableProxyingToOriginalMethods()
114 ->getMock();
115 $testObject->blabla = 'test';
116 $this->assertSame( 'test', $testObject->blabla );
117 }
118
119 /**
120 * @dataProvider provideSet
121 */
122 public function testSet( $propName, $expectedError, $expectedMessage, $expectedValue = 0 ) {
123 $this->assertPropertySame( 1, $this->testClass, $propName );
124 if ( $expectedError === true ) {
125 // Test the hard error, not the deprecation warning in front of it, tested elsewhere
126 $this->hideDeprecated( 'TestDeprecatedClass::$fallbackGetterOnly' );
127 try {
128 $this->testClass->$propName = 0;
129 } catch ( Throwable $e ) {
130 $this->assertSame( $expectedMessage, $e->getMessage() );
131 $this->assertInstanceOf( Error::class, $e );
132 return;
133 }
134 $this->fail( 'Expected an error' );
135 } else {
136 if ( $propName === 'nonExistent' ) {
137 $this->testClass->$propName = 0;
138 } else {
139 $this->assertDeprecationWarningIssued( function () use ( $propName ) {
140 $this->testClass->$propName = 0;
141 }, $expectedMessage );
142 }
143 $this->assertPropertySame( 0, $this->testClass, $propName );
144 }
145
146 $this->assertPropertySame( $expectedValue, $this->testClass, $propName );
147 }
148
149 public static function provideSet() {
150 return [
151 [ 'protectedDeprecated', null,
152 'Use of TestDeprecatedClass::$protectedDeprecated was deprecated in MediaWiki 1.23. ' .
153 '[Called from DeprecationHelperTest::{closure' ],
154 [ 'privateDeprecated', null,
155 'Use of TestDeprecatedClass::$privateDeprecated was deprecated in MediaWiki 1.24. ' .
156 '[Called from DeprecationHelperTest::{closure' ],
157 [ 'fallbackDeprecated', null,
158 'Use of TestDeprecatedClass::$fallbackDeprecated was deprecated in MediaWiki 1.25. ' .
159 '[Called from DeprecationHelperTest::{closure' ],
160 [ 'fallbackDeprecatedMethodName', null,
161 'Use of TestDeprecatedClass::$fallbackDeprecatedMethodName was deprecated in MediaWiki 1.26. ' .
162 '[Called from DeprecationHelperTest::{closure' ],
163 [ 'fallbackGetterOnly', true,
164 'Cannot access non-public property TestDeprecatedClass::$fallbackGetterOnly' ],
165 [ 'protectedNonDeprecated', true,
166 'Cannot access non-public property TestDeprecatedClass::$protectedNonDeprecated', 1 ],
167 [ 'privateNonDeprecated', true,
168 'Cannot access non-public property TestDeprecatedClass::$privateNonDeprecated', 1 ],
169 [ 'nonExistent', null,
170 'Use of TestDeprecatedClass::$nonExistent was deprecated in MediaWiki 1.23. ' .
171 '[Called from DeprecationHelperTest::{closure' ],
172 ];
173 }
174
175 public function testInternalGet() {
176 $this->assertSame( [
177 'prod' => 1,
178 'prond' => 1,
179 'prid' => 1,
180 'prind' => 1,
181 ], $this->testClass->getThings() );
182 }
183
184 public function testInternalSet() {
185 $this->testClass->setThings( 2, 2, 2, 2 );
186 $wrapper = TestingAccessWrapper::newFromObject( $this->testClass );
187 $this->assertSame( 2, $wrapper->protectedDeprecated );
188 $this->assertSame( 2, $wrapper->protectedNonDeprecated );
189 $this->assertSame( 2, $wrapper->privateDeprecated );
190 $this->assertSame( 2, $wrapper->privateNonDeprecated );
191 }
192
193 public function testSubclassGetSet() {
194 $fullName = 'TestDeprecatedClass::$privateNonDeprecated';
195 $this->assertErrorCaught( function () {
196 $this->assertSame( null, $this->testSubclass->getNondeprecatedPrivateParentProperty() );
197 }, Error::class, "Cannot access non-public property $fullName" );
198 $this->assertErrorCaught( function () {
199 $this->testSubclass->setNondeprecatedPrivateParentProperty( 0 );
200 $wrapper = TestingAccessWrapper::newFromObject( $this->testSubclass );
201 $this->assertSame( 1, $wrapper->privateNonDeprecated );
202 }, Error::class, "Cannot access non-public property $fullName" );
203
204 $fullName = 'TestDeprecatedSubclass::$subclassPrivateNondeprecated';
205 $this->assertErrorCaught( function () {
206 $this->assertSame( null, $this->testSubclass->subclassPrivateNondeprecated );
207 }, Error::class, "Cannot access non-public property $fullName" );
208 $this->assertErrorCaught( function () {
209 $this->testSubclass->subclassPrivateNondeprecated = 0;
210 $wrapper = TestingAccessWrapper::newFromObject( $this->testSubclass );
211 $this->assertSame( 1, $wrapper->subclassPrivateNondeprecated );
212 }, Error::class, "Cannot access non-public property $fullName" );
213 }
214
215 protected function assertErrorCaught( callable $callback, $name, $message ) {
216 $actualClass = $actualMessage = null;
217 try {
218 $callback();
219 } catch ( Throwable $e ) {
220 $actualClass = get_class( $e );
221 $actualMessage = $e->getMessage();
222 }
223 $this->assertSame( $name, $actualClass );
224 $this->assertSame( $message, $actualMessage );
225 }
226
227 protected function assertErrorTriggered( callable $callback, $level, $message ) {
228 $actualLevel = $actualMessage = null;
229 set_error_handler( static function ( $errorCode, $errorStr ) use ( &$actualLevel, &$actualMessage ) {
230 $actualLevel = $errorCode;
231 $actualMessage = $errorStr;
232 } );
233 $callback();
234 restore_error_handler();
235 $this->assertSame( $level, $actualLevel );
236 $this->assertSame( $message, $actualMessage );
237 }
238
239 protected function assertPropertySame( $expected, $object, $propName ) {
240 try {
241 $this->assertSame( $expected, TestingAccessWrapper::newFromObject( $object )->$propName );
242 } catch ( ReflectionException $e ) {
243 if ( !preg_match( "/Property (TestDeprecated(Class|Subclass)::\\$?)?$propName does not exist/",
244 $e->getMessage() )
245 ) {
246 throw $e;
247 }
248 // property_exists accepts monkey-patching, Reflection / TestingAccessWrapper doesn't
249 if ( property_exists( $object, $propName ) ) {
250 $this->assertSame( $expected, $object->$propName );
251 }
252 }
253 }
254
255 protected function assertDeprecationWarningIssued( callable $callback, string $expectedMessage ) {
256 $this->expectDeprecationAndContinue( '/' . preg_quote( $expectedMessage, '/' ) . '/' );
257 $callback();
258 }
259
260 /**
261 * Test bad MW version values to throw exceptions as expected
262 *
263 * @dataProvider provideBadMWVersion
264 */
265 public function testBadMWVersion( $version, $expected ) {
266 $this->expectException( $expected );
267
268 wfDeprecated( __METHOD__, $version );
269 }
270
271 public static function provideBadMWVersion() {
272 return [
273 [ 1, Exception::class ],
274 [ 1.33, Exception::class ],
275 [ true, Exception::class ],
276 [ null, Exception::class ]
277 ];
278 }
279 }