1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
<?php
use MediaWiki\Extension\FakeExtension\Maintenance\FakeScript;
use MediaWiki\Maintenance\MaintenanceRunner;
use MediaWiki\Maintenance\Version;
use PHPUnit\Framework\TestCase;
/**
* @covers \MediaWiki\Maintenance\MaintenanceRunner
*/
class MaintenanceRunnerTest extends TestCase {
public const FIXTURE_DIRECTORY = MW_INSTALL_PATH . '/tests/phpunit/data/MaintenanceRunner';
/** @var string */
private $oldWorkDir;
/**
* @before
*/
public function workDirSetUp() {
// Needed for testing the resolution of paths relative to CWD
$this->oldWorkDir = getcwd();
chdir( self::FIXTURE_DIRECTORY );
}
/**
* @after
*/
public function workDirTearDown() {
chdir( $this->oldWorkDir );
}
public static function provideFindScriptClass() {
// NOTE: We must use a different class for each test case,
// otherwise we may trigger a "cannot re-declare class" error.
yield 'plain name'
=> [ 'fakeScript', EditCLI::class ];
yield 'name with suffix'
=> [ 'fakeScript.php', EditCLI::class ];
yield 'name with path but no suffix'
=> [ 'storage/fakeScript', DumpRev::class ];
yield 'name with path and suffix'
=> [ 'storage/fakeScript.php', DumpRev::class ];
yield 'relative path with "./"'
=> [ './maintenance/storage/fakeScript.php', DumpRev::class ];
$dir = basename( self::FIXTURE_DIRECTORY );
yield 'relative path with "../"'
=> [ "../$dir/maintenance/fakeScriptThatReturnsName.php", CleanupImages::class ];
yield 'absolute path'
=> [ self::FIXTURE_DIRECTORY . '/maintenance/fakeScript.php', EditCLI::class ];
yield 'class name'
=> [ 'MediaWiki\Maintenance\Version', Version::class ];
yield 'extension script path, using prefix'
=> [ 'FakeExtension:fakeScript.php', FakeScript::class ];
// NOTE: assumes the class has been loaded by the previous test case!
yield 'extension class, using prefix'
=> [ 'FakeExtension:FakeScript', FakeScript::class ];
yield 'extension class'
=> [ 'MediaWiki\Extension\FakeExtension\Maintenance\FakeScript', FakeScript::class ];
yield 'extension class, using dots'
=> [ 'MediaWiki.Extension.FakeExtension.Maintenance.FakeScript', FakeScript::class ];
}
private function newRunner(): MaintenanceRunner {
return new class () extends MaintenanceRunner {
public function getScriptClass(): string {
return parent::getScriptClass();
}
public function findScriptClass( string $script ): string {
return parent::findScriptClass( $script );
}
public function preloadScriptFile( string $script ): void {
parent::preloadScriptFile( $script );
}
protected function fatalError( $msg, $exitCode = 1 ) {
$this->error( $msg );
throw new UnexpectedValueException( $msg );
}
protected function error( string $msg ) {
echo $msg;
}
protected function getMwInstallPath(): string {
// Fake the install path as the current directory, so that the fake maintenance scripts are found
// when providing a php file name in the tests.
return MaintenanceRunnerTest::FIXTURE_DIRECTORY;
}
protected function getExtensionInfo( string $extName ): ?array {
return [
'namespace' => "MediaWiki\\Extension\\$extName",
'path' => MaintenanceRunnerTest::FIXTURE_DIRECTORY . '/FakeExtension/extension.json',
];
}
};
}
/**
* @dataProvider provideFindScriptClass
*/
public function testFindScriptClass( $script, $expected ) {
$runner = $this->newRunner();
$class = $runner->findScriptClass( $script );
if ( str_starts_with( $class, '\\' ) ) {
$class = substr( $class, 1 );
}
$this->assertSame( $expected, $class );
}
public static function providePreloadScriptFile() {
// NOTE: We must use a different class for each test case,
// otherwise we may trigger a "cannot re-declare class" error.
yield 'plain name'
=> [ 'fakeScript', EditCLI::class ];
yield 'name with suffix'
=> [ 'fakeScriptThatReturnsName.php', CleanupImages::class ];
yield 'class name'
=> [ 'FindOrphanedFiles', FindOrphanedFiles::class ];
}
/**
* @dataProvider providePreloadScriptFile
*/
public function testPreloadScriptFile( $script, $expected ) {
$runner = $this->newRunner();
$runner->preloadScriptFile( $script );
$class = $runner->getScriptClass();
if ( str_starts_with( $class, '\\' ) ) {
$class = substr( $class, 1 );
}
$this->assertSame( $expected, $class );
}
}
|