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 [ 'protectedDeprecated', null, null ],
41 [ 'protectedNonDeprecated', E_USER_ERROR
,
42 'Cannot access non-public property TestDeprecatedClass::$protectedNonDeprecated' ],
43 [ 'privateDeprecated', null, null ],
44 [ 'privateNonDeprecated', E_USER_ERROR
,
45 'Cannot access non-public property TestDeprecatedClass::$privateNonDeprecated' ],
46 [ 'nonExistent', E_USER_NOTICE
, 'Undefined property: TestDeprecatedClass::$nonExistent' ],
51 * @dataProvider provideSet
53 public function testSet( $propName, $expectedLevel, $expectedMessage ) {
54 $this->assertPropertySame( 1, $this->testClass
, $propName );
55 if ( $expectedLevel ) {
56 $this->assertErrorTriggered( function () use ( $propName ) {
57 $this->testClass
->$propName = 0;
58 $this->assertPropertySame( 1, $this->testClass
, $propName );
59 }, $expectedLevel, $expectedMessage );
61 if ( $propName === 'nonExistent' ) {
62 $this->testClass
->$propName = 0;
64 $this->assertDeprecationWarningIssued( function () use ( $propName ) {
65 $this->testClass
->$propName = 0;
68 $this->assertPropertySame( 0, $this->testClass
, $propName );
72 public function provideSet() {
74 [ 'protectedDeprecated', null, null ],
75 [ 'protectedNonDeprecated', E_USER_ERROR
,
76 'Cannot access non-public property TestDeprecatedClass::$protectedNonDeprecated' ],
77 [ 'privateDeprecated', null, null ],
78 [ 'privateNonDeprecated', E_USER_ERROR
,
79 'Cannot access non-public property TestDeprecatedClass::$privateNonDeprecated' ],
80 [ 'nonExistent', null, null ],
84 public function testInternalGet() {
90 ], $this->testClass
->getThings() );
93 public function testInternalSet() {
94 $this->testClass
->setThings( 2, 2, 2, 2 );
95 $wrapper = TestingAccessWrapper
::newFromObject( $this->testClass
);
96 $this->assertSame( 2, $wrapper->protectedDeprecated
);
97 $this->assertSame( 2, $wrapper->protectedNonDeprecated
);
98 $this->assertSame( 2, $wrapper->privateDeprecated
);
99 $this->assertSame( 2, $wrapper->privateNonDeprecated
);
102 public function testSubclassGetSet() {
103 $this->assertDeprecationWarningIssued( function () {
104 $this->assertSame( 1, $this->testSubclass
->getDeprecatedPrivateParentProperty() );
106 $this->assertDeprecationWarningIssued( function () {
107 $this->testSubclass
->setDeprecatedPrivateParentProperty( 0 );
109 $wrapper = TestingAccessWrapper
::newFromObject( $this->testSubclass
);
110 $this->assertSame( 0, $wrapper->privateDeprecated
);
112 $fullName = 'TestDeprecatedClass::$privateNonDeprecated';
113 $this->assertErrorTriggered( function () {
114 $this->assertSame( null, $this->testSubclass
->getNonDeprecatedPrivateParentProperty() );
115 }, E_USER_ERROR
, "Cannot access non-public property $fullName" );
116 $this->assertErrorTriggered( function () {
117 $this->testSubclass
->setNonDeprecatedPrivateParentProperty( 0 );
118 $wrapper = TestingAccessWrapper
::newFromObject( $this->testSubclass
);
119 $this->assertSame( 1, $wrapper->privateNonDeprecated
);
120 }, E_USER_ERROR
, "Cannot access non-public property $fullName" );
123 protected function assertErrorTriggered( callable
$callback, $level, $message ) {
124 $actualLevel = $actualMessage = null;
125 set_error_handler( function ( $errorCode, $errorStr ) use ( &$actualLevel, &$actualMessage ) {
126 $actualLevel = $errorCode;
127 $actualMessage = $errorStr;
130 restore_error_handler();
131 $this->assertSame( $level, $actualLevel );
132 $this->assertSame( $message, $actualMessage );
135 protected function assertPropertySame( $expected, $object, $propName ) {
137 $this->assertSame( $expected, TestingAccessWrapper
::newFromObject( $object )->$propName );
138 } catch ( ReflectionException
$e ) {
139 if ( !preg_match( "/Property (TestDeprecated(Class|Subclass)::)?$propName does not exist/",
144 // property_exists accepts monkey-patching, Reflection / TestingAccessWrapper doesn't
145 if ( property_exists( $object, $propName ) ) {
146 $this->assertSame( $expected, $object->$propName );
151 protected function assertDeprecationWarningIssued( callable
$callback ) {
154 $wrapper = TestingAccessWrapper
::newFromClass( MWDebug
::class );
155 $this->assertNotEmpty( $wrapper->deprecationWarnings
);