| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaEngine; |
| 6 |
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaError; |
| 7 |
use MediaWikiCoversValidator; |
| 8 |
use PHPUnit\Framework\TestCase; |
| 9 |
use PHPUnit\Framework\TestSuite; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
abstract class LuaEngineUnitTestBase extends TestCase { |
| 21 |
use MediaWikiCoversValidator; |
| 22 |
use LuaEngineTestHelper; |
| 23 |
|
| 24 |
|
| 25 |
private static $staticEngineName = null; |
| 26 |
|
| 27 |
private $engineName = null; |
| 28 |
|
| 29 |
private $engine = null; |
| 30 |
|
| 31 |
private $luaDataProvider = null; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
protected $luaTestName = null; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
protected static $moduleName = null; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
protected static $dataProviderClass = LuaDataProvider::class; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
protected $skipTests = []; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function __construct( |
| 64 |
$name = null, array $data = [], $dataName = '', $engineName = null |
| 65 |
) { |
| 66 |
$this->engineName = $engineName ?? self::$staticEngineName; |
| 67 |
parent::__construct( $name, $data, $dataName ); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static function suite( $className ) { |
| 76 |
return self::makeSuite( $className ); |
| 77 |
} |
| 78 |
|
| 79 |
protected function tearDown(): void { |
| 80 |
if ( $this->luaDataProvider ) { |
| 81 |
$this->luaDataProvider->destroy(); |
| 82 |
$this->luaDataProvider = null; |
| 83 |
} |
| 84 |
if ( $this->engine ) { |
| 85 |
$this->engine->destroy(); |
| 86 |
$this->engine = null; |
| 87 |
} |
| 88 |
parent::tearDown(); |
| 89 |
} |
| 90 |
|
| 91 |
public function toString(): string { |
| 92 |
|
| 93 |
|
| 94 |
return $this->engineName . ': ' . ( $this->luaTestName ?: parent::toString() ); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
protected function getTestModules() { |
| 102 |
return [ |
| 103 |
'TestFramework' => __DIR__ . '/TestFramework.lua', |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
public function provideLuaData() { |
| 108 |
if ( !$this->luaDataProvider ) { |
| 109 |
$class = static::$dataProviderClass; |
| 110 |
$this->luaDataProvider = new $class ( $this->getEngine(), static::$moduleName ); |
| 111 |
} |
| 112 |
return $this->luaDataProvider; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function testLua( $key, $testName, $expected ) { |
| 122 |
$this->luaTestName = static::$moduleName . "[$key]: $testName"; |
| 123 |
if ( isset( $this->skipTests[$testName] ) ) { |
| 124 |
$this->markTestSkipped( $this->skipTests[$testName] ); |
| 125 |
} else { |
| 126 |
try { |
| 127 |
$actual = $this->provideLuaData()->run( $key ); |
| 128 |
} catch ( LuaError $ex ) { |
| 129 |
if ( substr( $ex->getLuaMessage(), 0, 6 ) === 'SKIP: ' ) { |
| 130 |
$this->markTestSkipped( substr( $ex->getLuaMessage(), 6 ) ); |
| 131 |
} else { |
| 132 |
throw $ex; |
| 133 |
} |
| 134 |
} |
| 135 |
$this->assertSame( $expected, $actual ); |
| 136 |
} |
| 137 |
$this->luaTestName = null; |
| 138 |
} |
| 139 |
} |