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