Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / debug / DeprecationHelperTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * @covers DeprecationHelper
7 */
8 class DeprecationHelperTest extends MediaWikiTestCase {
9
10 /** @var TestDeprecatedClass */
11 private $testClass;
12
13 /** @var TestDeprecatedSubclass */
14 private $testSubclass;
15
16 public function setUp() {
17 parent::setUp();
18 $this->testClass = new TestDeprecatedClass();
19 $this->testSubclass = new TestDeprecatedSubclass();
20 $this->setMwGlobals( 'wgDevelopmentWarnings', false );
21 }
22
23 /**
24 * @dataProvider provideGet
25 */
26 public function testGet( $propName, $expectedLevel, $expectedMessage ) {
27 if ( $expectedLevel ) {
28 $this->assertErrorTriggered( function () use ( $propName ) {
29 $this->assertSame( null, $this->testClass->$propName );
30 }, $expectedLevel, $expectedMessage );
31 } else {
32 $this->assertDeprecationWarningIssued( function () use ( $propName ) {
33 $this->assertSame( 1, $this->testClass->$propName );
34 } );
35 }
36 }
37
38 public function provideGet() {
39 return [
40 [ 'protectedNonDeprecated', E_USER_ERROR,
41 'Cannot access non-public property TestDeprecatedClass::$protectedNonDeprecated' ],
42 [ 'privateNonDeprecated', E_USER_ERROR,
43 'Cannot access non-public property TestDeprecatedClass::$privateNonDeprecated' ],
44 [ 'nonExistent', E_USER_NOTICE, 'Undefined property: TestDeprecatedClass::$nonExistent' ],
45 ];
46 }
47
48 /**
49 * @dataProvider provideSet
50 */
51 public function testSet( $propName, $expectedLevel, $expectedMessage ) {
52 $this->assertPropertySame( 1, $this->testClass, $propName );
53 if ( $expectedLevel ) {
54 $this->assertErrorTriggered( function () use ( $propName ) {
55 $this->testClass->$propName = 0;
56 $this->assertPropertySame( 1, $this->testClass, $propName );
57 }, $expectedLevel, $expectedMessage );
58 } else {
59 if ( $propName === 'nonExistent' ) {
60 $this->testClass->$propName = 0;
61 } else {
62 $this->assertDeprecationWarningIssued( function () use ( $propName ) {
63 $this->testClass->$propName = 0;
64 } );
65 }
66 $this->assertPropertySame( 0, $this->testClass, $propName );
67 }
68 }
69
70 public function provideSet() {
71 return [
72 [ 'protectedNonDeprecated', E_USER_ERROR,
73 'Cannot access non-public property TestDeprecatedClass::$protectedNonDeprecated' ],
74 [ 'privateNonDeprecated', E_USER_ERROR,
75 'Cannot access non-public property TestDeprecatedClass::$privateNonDeprecated' ],
76 [ 'nonExistent', null, null ],
77 ];
78 }
79
80 public function testInternalGet() {
81 $this->assertSame( [
82 'prod' => 1,
83 'prond' => 1,
84 'prid' => 1,
85 'prind' => 1,
86 ], $this->testClass->getThings() );
87 }
88
89 public function testInternalSet() {
90 $this->testClass->setThings( 2, 2, 2, 2 );
91 $wrapper = TestingAccessWrapper::newFromObject( $this->testClass );
92 $this->assertSame( 2, $wrapper->protectedDeprecated );
93 $this->assertSame( 2, $wrapper->protectedNonDeprecated );
94 $this->assertSame( 2, $wrapper->privateDeprecated );
95 $this->assertSame( 2, $wrapper->privateNonDeprecated );
96 }
97
98 public function testSubclassGetSet() {
99 $fullName = 'TestDeprecatedClass::$privateNonDeprecated';
100 $this->assertErrorTriggered( function () {
101 $this->assertSame( null, $this->testSubclass->getNonDeprecatedPrivateParentProperty() );
102 }, E_USER_ERROR, "Cannot access non-public property $fullName" );
103 $this->assertErrorTriggered( function () {
104 $this->testSubclass->setNonDeprecatedPrivateParentProperty( 0 );
105 $wrapper = TestingAccessWrapper::newFromObject( $this->testSubclass );
106 $this->assertSame( 1, $wrapper->privateNonDeprecated );
107 }, E_USER_ERROR, "Cannot access non-public property $fullName" );
108
109 $fullName = 'TestDeprecatedSubclass::$subclassPrivateNondeprecated';
110 $this->assertErrorTriggered( function () {
111 $this->assertSame( null, $this->testSubclass->subclassPrivateNondeprecated );
112 }, E_USER_ERROR, "Cannot access non-public property $fullName" );
113 $this->assertErrorTriggered( function () {
114 $this->testSubclass->subclassPrivateNondeprecated = 0;
115 $wrapper = TestingAccessWrapper::newFromObject( $this->testSubclass );
116 $this->assertSame( 1, $wrapper->subclassPrivateNondeprecated );
117 }, E_USER_ERROR, "Cannot access non-public property $fullName" );
118 }
119
120 protected function assertErrorTriggered( callable $callback, $level, $message ) {
121 $actualLevel = $actualMessage = null;
122 set_error_handler( function ( $errorCode, $errorStr ) use ( &$actualLevel, &$actualMessage ) {
123 $actualLevel = $errorCode;
124 $actualMessage = $errorStr;
125 } );
126 $callback();
127 restore_error_handler();
128 $this->assertSame( $level, $actualLevel );
129 $this->assertSame( $message, $actualMessage );
130 }
131
132 protected function assertPropertySame( $expected, $object, $propName ) {
133 try {
134 $this->assertSame( $expected, TestingAccessWrapper::newFromObject( $object )->$propName );
135 } catch ( ReflectionException $e ) {
136 if ( !preg_match( "/Property (TestDeprecated(Class|Subclass)::)?$propName does not exist/",
137 $e->getMessage() )
138 ) {
139 throw $e;
140 }
141 // property_exists accepts monkey-patching, Reflection / TestingAccessWrapper doesn't
142 if ( property_exists( $object, $propName ) ) {
143 $this->assertSame( $expected, $object->$propName );
144 }
145 }
146 }
147
148 protected function assertDeprecationWarningIssued( callable $callback ) {
149 MWDebug::clearLog();
150 $callback();
151 $wrapper = TestingAccessWrapper::newFromClass( MWDebug::class );
152 $this->assertNotEmpty( $wrapper->deprecationWarnings );
153 }
154
155 /**
156 * Test bad MW version values to throw exceptions as expected
157 *
158 * @dataProvider provideBadMWVersion
159 */
160 public function testBadMWVersion( $version, $expected ) {
161 $this->setExpectedException( $expected );
162
163 wfDeprecated( __METHOD__, $version );
164 }
165
166 public function provideBadMWVersion() {
167 return [
168 [ 1, Exception::class ],
169 [ 1.33, Exception::class ],
170 [ null, Exception::class ]
171 ];
172 }
173 }