phpunit.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 through this entry point or not
  9. define( 'MW_PHPUNIT_TEST', true );
  10. // Start up MediaWiki in command-line mode
  11. require_once dirname( dirname( __DIR__ ) ) . "/maintenance/Maintenance.php";
  12. class PHPUnitMaintClass extends Maintenance {
  13. public function __construct() {
  14. parent::__construct();
  15. $this->addOption( 'with-phpunitdir',
  16. 'Directory to include PHPUnit from, for example when using a git fetchout from upstream. Path will be prepended to PHP `include_path`.',
  17. false, # not required
  18. true # need arg
  19. );
  20. }
  21. public function finalSetup() {
  22. parent::finalSetup();
  23. global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
  24. global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
  25. global $wgLocaltimezone, $wgLocalisationCacheConf;
  26. global $wgDevelopmentWarnings;
  27. // Inject test autoloader
  28. require_once __DIR__ . '/../TestsAutoLoader.php';
  29. // wfWarn should cause tests to fail
  30. $wgDevelopmentWarnings = true;
  31. $wgMainCacheType = CACHE_NONE;
  32. $wgMessageCacheType = CACHE_NONE;
  33. $wgParserCacheType = CACHE_NONE;
  34. $wgLanguageConverterCacheType = CACHE_NONE;
  35. $wgUseDatabaseMessages = false; # Set for future resets
  36. // Assume UTC for testing purposes
  37. $wgLocaltimezone = 'UTC';
  38. $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
  39. // Bug 44192 Do not attempt to send a real e-mail
  40. Hooks::clear( 'AlternateUserMailer' );
  41. Hooks::register(
  42. 'AlternateUserMailer',
  43. function () {
  44. return false;
  45. }
  46. );
  47. }
  48. public function execute() {
  49. global $IP;
  50. # Make sure we have --configuration or PHPUnit might complain
  51. if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
  52. //Hack to eliminate the need to use the Makefile (which sucks ATM)
  53. array_splice( $_SERVER['argv'], 1, 0,
  54. array( '--configuration', $IP . '/tests/phpunit/suite.xml' ) );
  55. }
  56. # --with-phpunitdir let us override the default PHPUnit version
  57. if ( $this->hasOption( 'with-phpunitdir' ) ) {
  58. $phpunitDir = $this->getOption( 'with-phpunitdir' );
  59. # Sanity checks
  60. if ( !is_dir( $phpunitDir ) ) {
  61. $this->error( "--with-phpunitdir should be set to an existing directory", 1 );
  62. }
  63. if ( !is_readable( $phpunitDir . "/PHPUnit/Runner/Version.php" ) ) {
  64. $this->error( "No usable PHPUnit installation in $phpunitDir.\nAborting.\n", 1 );
  65. }
  66. # Now prepends provided PHPUnit directory
  67. $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
  68. set_include_path( $phpunitDir . PATH_SEPARATOR . get_include_path() );
  69. # Cleanup $args array so the option and its value do not
  70. # pollute PHPUnit
  71. $key = array_search( '--with-phpunitdir', $_SERVER['argv'] );
  72. unset( $_SERVER['argv'][$key] ); // the option
  73. unset( $_SERVER['argv'][$key + 1] ); // its value
  74. $_SERVER['argv'] = array_values( $_SERVER['argv'] );
  75. }
  76. }
  77. public function getDbType() {
  78. return Maintenance::DB_ADMIN;
  79. }
  80. }
  81. $maintClass = 'PHPUnitMaintClass';
  82. require RUN_MAINTENANCE_IF_MAIN;
  83. if ( !class_exists( 'PHPUnit_Runner_Version' ) ) {
  84. require_once 'PHPUnit/Runner/Version.php';
  85. }
  86. if ( PHPUnit_Runner_Version::id() !== '@package_version@'
  87. && version_compare( PHPUnit_Runner_Version::id(), '3.7.0', '<' )
  88. ) {
  89. die( 'PHPUnit 3.7.0 or later required, you have ' . PHPUnit_Runner_Version::id() . ".\n" );
  90. }
  91. if ( !class_exists( 'PHPUnit_TextUI_Command' ) ) {
  92. require_once 'PHPUnit/Autoload.php';
  93. }
  94. // Prevent segfault when we have lots of unit tests (bug 62623)
  95. if ( version_compare( PHP_VERSION, '5.4.0', '<' )
  96. && version_compare( PHP_VERSION, '5.3.0', '>=' )
  97. ) {
  98. register_shutdown_function( function() {
  99. gc_collect_cycles();
  100. gc_disable();
  101. } );
  102. }
  103. MediaWikiPHPUnitCommand::main();