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