Merge "Fix sessionfailure i18n message during authentication"
[lhc/web/wiklou.git] / tests / phpunit / maintenance / MaintenanceBaseTestCase.php
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance;
4
5 use Maintenance;
6 use MediaWikiTestCase;
7 use Wikimedia\TestingAccessWrapper;
8
9 abstract class MaintenanceBaseTestCase extends MediaWikiTestCase {
10
11 /**
12 * The main Maintenance instance that is used for testing, wrapped and mockable.
13 *
14 * @var Maintenance
15 */
16 protected $maintenance;
17
18 protected function setUp() {
19 parent::setUp();
20
21 $this->maintenance = $this->createMaintenance();
22 }
23
24 /**
25 * Do a little stream cleanup to prevent output in case the child class
26 * hasn't tested the capture buffer.
27 */
28 protected function tearDown() {
29 if ( $this->maintenance ) {
30 $this->maintenance->cleanupChanneled();
31 }
32
33 // This is smelly, but maintenance scripts usually produce output, so
34 // we anticipate and ignore with a regex that will catch everything.
35 //
36 // If you call $this->expectOutputRegex in your subclass, this guard
37 // won't be triggered, and your specific pattern will be respected.
38 if ( !$this->hasExpectationOnOutput() ) {
39 $this->expectOutputRegex( '/.*/' );
40 }
41
42 parent::tearDown();
43 }
44
45 /**
46 * @return string Class name
47 *
48 * Subclasses must implement this in order to use the $this->maintenance
49 * variable. Normally, it will be set like:
50 * return PopulateDatabaseMaintenance::class;
51 *
52 * If you need to change the way your maintenance class is constructed,
53 * override createMaintenance.
54 */
55 abstract protected function getMaintenanceClass();
56
57 /**
58 * Called by setUp to initialize $this->maintenance.
59 *
60 * @return object The Maintenance instance to test.
61 */
62 protected function createMaintenance() {
63 $className = $this->getMaintenanceClass();
64 $obj = new $className();
65
66 // We use TestingAccessWrapper in order to access protected internals
67 // such as `output()`.
68 return TestingAccessWrapper::newFromObject( $obj );
69 }
70
71 /**
72 * Asserts the output before and after simulating shutdown
73 *
74 * This function simulates shutdown of self::maintenance.
75 *
76 * @param string $preShutdownOutput Expected output before simulating shutdown
77 * @param bool $expectNLAppending Whether or not shutdown simulation is expected
78 * to add a newline to the output. If false, $preShutdownOutput is the
79 * expected output after shutdown simulation. Otherwise,
80 * $preShutdownOutput with an appended newline is the expected output
81 * after shutdown simulation.
82 */
83 protected function assertOutputPrePostShutdown( $preShutdownOutput, $expectNLAppending ) {
84 $this->assertEquals( $preShutdownOutput, $this->getActualOutput(),
85 "Output before shutdown simulation" );
86
87 $this->maintenance->cleanupChanneled();
88
89 $postShutdownOutput = $preShutdownOutput . ( $expectNLAppending ? "\n" : "" );
90 $this->expectOutputString( $postShutdownOutput );
91 }
92
93 }