wiki.techinc.nl/maintenance/includes/MaintenanceFatalError.php
Dreamy Jazz 65d62ae39e Throw exception from Maintenance::fatalError when in PHPUnit tests
Why:
* The Maintenance::fatalError method calls the exit method, which
  when called causes the PHPUnit test suites to exit early.
* This means that code that calls ::fatalError cannot be tested
  unless the method is mocked for each test.
* To avoid this problem the ::fatalError method should not call
  exit() when running PHPUnit tests, as described in T272241.
  Instead, it should throw an exception which can be caught when
  the call is expected.

What:
* Create MaintenanceFatalError which extends Exception, which is
  thrown by Maintenance::fatalError instead of calling exit() if
  the 'MW_PHPUNIT_TEST' constant is defined.
** This new exception takes the error code passed to the
   ::fatalError method and uses it as the exception error code.
* Create MaintenanceBaseTestCase::expectCallToFatalError which
  makes it easier for tests to assert that a call to
  Maintenance::fatalError occurs. This can optionally assert
  that the $code passed is as expected.
** Code which wishes to assert against the provided $msg can use
   ::expectOutputString or ::expectOutputRegex.

Bug: T272241
Change-Id: I554a963c63eb4f22ebb3273053a7b83a33dcb4d1
2024-08-03 22:54:38 +00:00

21 lines
514 B
PHP

<?php
namespace MediaWiki\Maintenance;
use Exception;
/**
* Exception thrown by Maintenance::fatalError if called during PHPUnit tests. This is done because
* calling exit() would cause the test suite to stop early.
*
* @since 1.43
*/
class MaintenanceFatalError extends Exception {
/**
* @param int $code The error code that would have been passed to exit() if the method was not
* called during a PHPUnit test.
*/
public function __construct( $code ) {
parent::__construct( "", $code );
}
}