3 use Wikimedia\TestingAccessWrapper
;
6 * @covers DeprecationHelper
8 class DeprecationHelperTest
extends MediaWikiTestCase
{
10 /** @var TestDeprecatedClass */
13 /** @var TestDeprecatedSubclass */
14 private $testSubclass;
16 public function setUp() {
18 $this->testClass
= new TestDeprecatedClass();
19 $this->testSubclass
= new TestDeprecatedSubclass();
20 $this->setMwGlobals( 'wgDevelopmentWarnings', false );
24 * @dataProvider provideGet
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 );
32 $this->assertDeprecationWarningIssued( function () use ( $propName ) {
33 $this->assertSame( 1, $this->testClass
->$propName );
38 public function provideGet() {
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' ],
49 * @dataProvider provideSet
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 );
59 if ( $propName === 'nonExistent' ) {
60 $this->testClass
->$propName = 0;
62 $this->assertDeprecationWarningIssued( function () use ( $propName ) {
63 $this->testClass
->$propName = 0;
66 $this->assertPropertySame( 0, $this->testClass
, $propName );
70 public function provideSet() {
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 ],
80 public function testInternalGet() {
86 ], $this->testClass
->getThings() );
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
);
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" );
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" );
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;
127 restore_error_handler();
128 $this->assertSame( $level, $actualLevel );
129 $this->assertSame( $message, $actualMessage );
132 protected function assertPropertySame( $expected, $object, $propName ) {
134 $this->assertSame( $expected, TestingAccessWrapper
::newFromObject( $object )->$propName );
135 } catch ( ReflectionException
$e ) {
136 if ( !preg_match( "/Property (TestDeprecated(Class|Subclass)::)?$propName does not exist/",
141 // property_exists accepts monkey-patching, Reflection / TestingAccessWrapper doesn't
142 if ( property_exists( $object, $propName ) ) {
143 $this->assertSame( $expected, $object->$propName );
148 protected function assertDeprecationWarningIssued( callable
$callback ) {
151 $wrapper = TestingAccessWrapper
::newFromClass( MWDebug
::class );
152 $this->assertNotEmpty( $wrapper->deprecationWarnings
);
156 * Test bad MW version values to throw exceptions as expected
158 * @dataProvider provideBadMWVersion
160 public function testBadMWVersion( $version, $expected ) {
161 $this->setExpectedException( $expected );
163 wfDeprecated( __METHOD__
, $version );
166 public function provideBadMWVersion() {
168 [ 1, Exception
::class ],
169 [ 1.33, Exception
::class ],
170 [ null, Exception
::class ]