phpunit.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Bootstrapping for MediaWiki PHPUnit tests
  5. *
  6. * @file
  7. */
  8. // Set a flag which can be used to detect when other scripts have been entered
  9. // through this entry point or not.
  10. define( 'MW_PHPUNIT_TEST', true );
  11. // Start up MediaWiki in command-line mode
  12. require_once dirname( dirname( __DIR__ ) ) . "/maintenance/Maintenance.php";
  13. class PHPUnitMaintClass extends Maintenance {
  14. public function __construct() {
  15. parent::__construct();
  16. $this->setAllowUnregisteredOptions( true );
  17. $this->addOption(
  18. 'debug-tests',
  19. 'Log testing activity to the PHPUnitCommand log channel (deprecated, always on).',
  20. false, # not required
  21. false # no arg needed
  22. );
  23. $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
  24. $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
  25. $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
  26. $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
  27. $this->addOption(
  28. 'reuse-db', 'Init DB only if tables are missing and keep after finish.',
  29. false,
  30. false
  31. );
  32. }
  33. public function finalSetup() {
  34. parent::finalSetup();
  35. // Inject test autoloader
  36. self::requireTestsAutoloader();
  37. TestSetup::applyInitialConfig();
  38. }
  39. public function execute() {
  40. // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
  41. // stays in tact.
  42. // Has to in execute() instead of finalSetup(), because finalSetup() runs before
  43. // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
  44. restore_error_handler();
  45. $this->forceFormatServerArgv();
  46. if ( !class_exists( 'PHPUnit\\Framework\\TestCase' ) ) {
  47. echo "PHPUnit not found. Please install it and other dev dependencies by
  48. running `composer install` in MediaWiki root directory.\n";
  49. exit( 1 );
  50. }
  51. echo defined( 'HHVM_VERSION' ) ?
  52. 'Using HHVM ' . HHVM_VERSION . ' (' . PHP_VERSION . ")\n" :
  53. 'Using PHP ' . PHP_VERSION . "\n";
  54. // Tell PHPUnit to ignore options meant for MediaWiki
  55. $ignore = [];
  56. foreach ( $this->mParams as $name => $param ) {
  57. if ( empty( $param['withArg'] ) ) {
  58. $ignore[] = $name;
  59. } else {
  60. $ignore[] = "$name=";
  61. }
  62. }
  63. // Pass through certain options to MediaWikiTestCase
  64. $cliArgs = [];
  65. foreach (
  66. [
  67. 'use-filebackend',
  68. 'use-bagostuff',
  69. 'use-jobqueue',
  70. 'use-normal-tables',
  71. 'reuse-db'
  72. ] as $name
  73. ) {
  74. $cliArgs[$name] = $this->getOption( $name );
  75. }
  76. $command = new MediaWikiPHPUnitCommand( $ignore, $cliArgs );
  77. $command->run( $_SERVER['argv'], true );
  78. }
  79. public function getDbType() {
  80. return Maintenance::DB_ADMIN;
  81. }
  82. protected function addOption( $name, $description, $required = false,
  83. $withArg = false, $shortName = false, $multiOccurrence = false
  84. ) {
  85. // ignore --quiet which does not really make sense for unit tests
  86. if ( $name !== 'quiet' ) {
  87. parent::addOption( $name, $description, $required, $withArg, $shortName, $multiOccurrence );
  88. }
  89. }
  90. /**
  91. * Force the format of elements in $_SERVER['argv']
  92. * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
  93. */
  94. private function forceFormatServerArgv() {
  95. $argv = [];
  96. foreach ( $_SERVER['argv'] as $key => $arg ) {
  97. if ( $key === 0 ) {
  98. $argv[0] = $arg;
  99. } elseif ( strstr( $arg, '=' ) ) {
  100. foreach ( explode( '=', $arg, 2 ) as $argPart ) {
  101. $argv[] = $argPart;
  102. }
  103. } else {
  104. $argv[] = $arg;
  105. }
  106. }
  107. $_SERVER['argv'] = $argv;
  108. }
  109. }
  110. $maintClass = 'PHPUnitMaintClass';
  111. require RUN_MAINTENANCE_IF_MAIN;