Merge "Migrate BagOStuff::incr() calls to incrWithInit()"
[lhc/web/wiklou.git] / tests / phpunit / includes / config / TestAllServiceOptionsUsed.php
1 <?php
2
3 /**
4 * Use this trait to check that code run by tests accesses every key declared for this class'
5 * ServiceOptions, e.g., in a $constructorOptions member variable. To use this trait, you need to do
6 * two things (other than use-ing it):
7 *
8 * 1) Don't use the regular ServiceOptions when constructing your objects, but rather
9 * LoggedServiceOptions. These are used the same as ServiceOptions, except in the constructor, pass
10 * self::$serviceOptionsAccessLog before the regular arguments.
11 *
12 * 2) Make a test that calls assertAllServiceOptionsUsed(). If some ServiceOptions keys are not yet
13 * accessed in tests but actually are used by the class, pass their names as an argument.
14 *
15 * Currently we support only one ServiceOptions per test class.
16 */
17 trait TestAllServiceOptionsUsed {
18 /** @var array [ expected keys (as list), keys accessed so far (as dictionary) ] */
19 private static $serviceOptionsAccessLog = [];
20
21 /**
22 * @param string[] $expectedUnused Options that we know are not yet tested
23 */
24 public function assertAllServiceOptionsUsed( array $expectedUnused = [] ) {
25 $this->assertNotEmpty( self::$serviceOptionsAccessLog,
26 'You need to pass LoggedServiceOptions to your class instead of ServiceOptions ' .
27 'for TestAllServiceOptionsUsed to work.'
28 );
29
30 list( $expected, $actual ) = self::$serviceOptionsAccessLog;
31
32 $expected = array_diff( $expected, $expectedUnused );
33
34 $this->assertSame(
35 [],
36 array_diff( $expected, array_keys( $actual ) ),
37 "Some ServiceOptions keys were not accessed in tests. If they really aren't used, " .
38 "remove them from the class' option list. If they are used, add tests to cover them, " .
39 "or ignore the problem for now by passing them to assertAllServiceOptionsUsed() in " .
40 "its \$expectedUnused argument."
41 );
42
43 if ( $expectedUnused ) {
44 $this->markTestIncomplete( 'Some ServiceOptions keys are not yet accessed by tests: ' .
45 implode( ', ', $expectedUnused ) );
46 }
47 }
48 }