MaintenanceBaseTestCase.php 2.7 KB

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