3 namespace MediaWiki\Tests\Maintenance
;
5 use MediaWiki\Maintenance\Maintenance
;
6 use MediaWiki\Maintenance\MaintenanceFatalError
;
7 use MediaWikiIntegrationTestCase
;
8 use Wikimedia\TestingAccessWrapper
;
10 abstract class MaintenanceBaseTestCase
extends MediaWikiIntegrationTestCase
{
13 * The main Maintenance instance that is used for testing, wrapped and mockable.
17 protected $maintenance;
19 protected function setUp(): void
{
22 $this->maintenance
= $this->createMaintenance();
26 * Do a little stream cleanup to prevent output in case the child class
27 * hasn't tested the capture buffer.
29 protected function tearDown(): void
{
30 if ( $this->maintenance
) {
31 $this->maintenance
->cleanupChanneled();
34 // This is smelly, but maintenance scripts usually produce output, so
35 // we anticipate and ignore with a regex that will catch everything.
37 // If you call $this->expectOutputRegex in your subclass, this guard
38 // won't be triggered, and your specific pattern will be respected.
39 if ( !$this->hasExpectationOnOutput() ) {
40 $this->expectOutputRegex( '/.*/' );
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 * @return string Class name
56 abstract protected function getMaintenanceClass();
59 * Called by setUp to initialize $this->maintenance.
61 * @return Maintenance The Maintenance instance to test.
63 protected function createMaintenance() {
64 $className = $this->getMaintenanceClass();
65 $obj = new $className();
67 // We use TestingAccessWrapper in order to access protected internals
68 // such as `output()`.
69 return TestingAccessWrapper
::newFromObject( $obj );
73 * Asserts the output before and after simulating shutdown
75 * This function simulates shutdown of self::maintenance.
77 * @param string $preShutdownOutput Expected output before simulating shutdown
78 * @param bool $expectNLAppending Whether or not shutdown simulation is expected
79 * to add a newline to the output. If false, $preShutdownOutput is the
80 * expected output after shutdown simulation. Otherwise,
81 * $preShutdownOutput with an appended newline is the expected output
82 * after shutdown simulation.
84 protected function assertOutputPrePostShutdown( $preShutdownOutput, $expectNLAppending ) {
85 $this->assertEquals( $preShutdownOutput, $this->getActualOutput(),
86 "Output before shutdown simulation" );
88 $this->maintenance
->cleanupChanneled();
90 $postShutdownOutput = $preShutdownOutput . ( $expectNLAppending ?
"\n" : "" );
91 $this->expectOutputString( $postShutdownOutput );
95 * Expects that a call to Maintenance::fatalError occurs. When Maintenance::fatalError
96 * is called, an exception is thrown which is marked as expected through this method.
98 * If you wish to assert on the error message provided to Maintenance::fatalError,
99 * then use ::expectOutputString or ::expectOutputRegex.
101 * @param ?int $expectedCode The expected error code provided to Maintenance::fatalError
104 protected function expectCallToFatalError( ?
int $expectedCode = null ) {
105 $this->expectException( MaintenanceFatalError
::class );
106 if ( $expectedCode !== null ) {
107 $this->expectExceptionCode( $expectedCode );