3 namespace MediaWiki\Tests\Maintenance
;
6 use Wikimedia\TestingAccessWrapper
;
8 abstract class MaintenanceBaseTestCase
extends MediaWikiTestCase
{
11 * The main Maintenance instance that is used for testing, wrapped and mockable.
15 protected $maintenance;
17 protected function setUp() {
20 $this->maintenance
= $this->createMaintenance();
24 * Do a little stream cleanup to prevent output in case the child class
25 * hasn't tested the capture buffer.
27 protected function tearDown() {
28 if ( $this->maintenance
) {
29 $this->maintenance
->cleanupChanneled();
32 // This is smelly, but maintenance scripts usually produce output, so
33 // we anticipate and ignore with a regex that will catch everything.
35 // If you call $this->expectOutputRegex in your subclass, this guard
36 // won't be triggered, and your specific pattern will be respected.
37 if ( !$this->hasExpectationOnOutput() ) {
38 $this->expectOutputRegex( '/.*/' );
45 * @return string Class name
47 * Subclasses must implement this in order to use the $this->maintenance
48 * variable. Normally, it will be set like:
49 * return PopulateDatabaseMaintenance::class;
51 * If you need to change the way your maintenance class is constructed,
52 * override createMaintenance.
54 abstract protected function getMaintenanceClass();
57 * Called by setUp to initialize $this->maintenance.
59 * @return object The Maintenance instance to test.
61 protected function createMaintenance() {
62 $className = $this->getMaintenanceClass();
63 $obj = new $className();
65 // We use TestingAccessWrapper in order to access protected internals
66 // such as `output()`.
67 return TestingAccessWrapper
::newFromObject( $obj );
71 * Asserts the output before and after simulating shutdown
73 * This function simulates shutdown of self::maintenance.
75 * @param string $preShutdownOutput Expected output before simulating shutdown
76 * @param bool $expectNLAppending Whether or not shutdown simulation is expected
77 * to add a newline to the output. If false, $preShutdownOutput is the
78 * expected output after shutdown simulation. Otherwise,
79 * $preShutdownOutput with an appended newline is the expected output
80 * after shutdown simulation.
82 protected function assertOutputPrePostShutdown( $preShutdownOutput, $expectNLAppending ) {
83 $this->assertEquals( $preShutdownOutput, $this->getActualOutput(),
84 "Output before shutdown simulation" );
86 $this->maintenance
->cleanupChanneled();
88 $postShutdownOutput = $preShutdownOutput . ( $expectNLAppending ?
"\n" : "" );
89 $this->expectOutputString( $postShutdownOutput );