MediaWiki  master
DeprecationHelperTest.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
11  private $testClass;
12 
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 
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  [ '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' ],
47  ];
48  }
49 
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 );
60  } else {
61  if ( $propName === 'nonExistent' ) {
62  $this->testClass->$propName = 0;
63  } else {
64  $this->assertDeprecationWarningIssued( function () use ( $propName ) {
65  $this->testClass->$propName = 0;
66  } );
67  }
68  $this->assertPropertySame( 0, $this->testClass, $propName );
69  }
70  }
71 
72  public function provideSet() {
73  return [
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 ],
81  ];
82  }
83 
84  public function testInternalGet() {
85  $this->assertSame( [
86  'prod' => 1,
87  'prond' => 1,
88  'prid' => 1,
89  'prind' => 1,
90  ], $this->testClass->getThings() );
91  }
92 
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 );
100  }
101 
102  public function testSubclassGetSet() {
103  $this->assertDeprecationWarningIssued( function () {
104  $this->assertSame( 1, $this->testSubclass->getDeprecatedPrivateParentProperty() );
105  } );
106  $this->assertDeprecationWarningIssued( function () {
107  $this->testSubclass->setDeprecatedPrivateParentProperty( 0 );
108  } );
109  $wrapper = TestingAccessWrapper::newFromObject( $this->testSubclass );
110  $this->assertSame( 0, $wrapper->privateDeprecated );
111 
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" );
121 
122  $fullName = 'TestDeprecatedSubclass::$subclassPrivateNondeprecated';
123  $this->assertErrorTriggered( function () {
124  $this->assertSame( null, $this->testSubclass->subclassPrivateNondeprecated );
125  }, E_USER_ERROR, "Cannot access non-public property $fullName" );
126  $this->assertErrorTriggered( function () {
127  $this->testSubclass->subclassPrivateNondeprecated = 0;
128  $wrapper = TestingAccessWrapper::newFromObject( $this->testSubclass );
129  $this->assertSame( 1, $wrapper->subclassPrivateNondeprecated );
130  }, E_USER_ERROR, "Cannot access non-public property $fullName" );
131  }
132 
133  protected function assertErrorTriggered( callable $callback, $level, $message ) {
134  $actualLevel = $actualMessage = null;
135  set_error_handler( function ( $errorCode, $errorStr ) use ( &$actualLevel, &$actualMessage ) {
136  $actualLevel = $errorCode;
137  $actualMessage = $errorStr;
138  } );
139  $callback();
140  restore_error_handler();
141  $this->assertSame( $level, $actualLevel );
142  $this->assertSame( $message, $actualMessage );
143  }
144 
145  protected function assertPropertySame( $expected, $object, $propName ) {
146  try {
147  $this->assertSame( $expected, TestingAccessWrapper::newFromObject( $object )->$propName );
148  } catch ( ReflectionException $e ) {
149  if ( !preg_match( "/Property (TestDeprecated(Class|Subclass)::)?$propName does not exist/",
150  $e->getMessage() )
151  ) {
152  throw $e;
153  }
154  // property_exists accepts monkey-patching, Reflection / TestingAccessWrapper doesn't
155  if ( property_exists( $object, $propName ) ) {
156  $this->assertSame( $expected, $object->$propName );
157  }
158  }
159  }
160 
161  protected function assertDeprecationWarningIssued( callable $callback ) {
163  $callback();
164  $wrapper = TestingAccessWrapper::newFromClass( MWDebug::class );
165  $this->assertNotEmpty( $wrapper->deprecationWarnings );
166  }
167 
168 }
assertDeprecationWarningIssued(callable $callback)
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2159
TestDeprecatedClass $testClass
$fullName
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
static clearLog()
Clears internal log array and deprecation tracking.
Definition: MWDebug.php:135
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown...
testGet( $propName, $expectedLevel, $expectedMessage)
provideGet
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
TestDeprecatedSubclass $testSubclass
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
assertPropertySame( $expected, $object, $propName)
assertErrorTriggered(callable $callback, $level, $message)
testSet( $propName, $expectedLevel, $expectedMessage)
provideSet