MediaWiki  1.34.0
LuaEngineUnitTestBase.php
Go to the documentation of this file.
1 <?php
2 
12 abstract class Scribunto_LuaEngineUnitTestBase extends \PHPUnit\Framework\TestCase {
13  use MediaWikiCoversValidator;
14  use PHPUnit4And6Compat;
16 
17  private static $staticEngineName = null;
18  private $engineName = null;
19  private $engine = null;
20  private $luaDataProvider = null;
21 
26  protected $luaTestName = null;
27 
32  protected static $moduleName = null;
33 
38  protected static $dataProviderClass = 'Scribunto_LuaDataProvider';
39 
44  protected $skipTests = [];
45 
46  public function __construct(
47  $name = null, array $data = [], $dataName = '', $engineName = null
48  ) {
49  if ( $engineName === null ) {
51  }
52  $this->engineName = $engineName;
53  parent::__construct( $name, $data, $dataName );
54  }
55 
56  public static function suite( $className ) {
57  return self::makeSuite( $className );
58  }
59 
60  protected function tearDown() {
61  if ( $this->luaDataProvider ) {
62  $this->luaDataProvider->destroy();
63  $this->luaDataProvider = null;
64  }
65  if ( $this->engine ) {
66  $this->engine->destroy();
67  $this->engine = null;
68  }
69  parent::tearDown();
70  }
71 
72  public function toString() {
73  // When running tests written in Lua, return a nicer representation in
74  // the failure message.
75  if ( $this->luaTestName ) {
76  return $this->engineName . ': ' . $this->luaTestName;
77  }
78  return $this->engineName . ': ' . parent::toString();
79  }
80 
81  protected function getTestModules() {
82  return [
83  'TestFramework' => __DIR__ . '/TestFramework.lua',
84  ];
85  }
86 
87  public function provideLuaData() {
88  if ( !$this->luaDataProvider ) {
89  $class = static::$dataProviderClass;
90  $this->luaDataProvider = new $class ( $this->getEngine(), static::$moduleName );
91  }
93  }
94 
101  public function testLua( $key, $testName, $expected ) {
102  $this->luaTestName = static::$moduleName . "[$key]: $testName";
103  if ( isset( $this->skipTests[$testName] ) ) {
104  $this->markTestSkipped( $this->skipTests[$testName] );
105  } else {
106  try {
107  $actual = $this->provideLuaData()->run( $key );
108  } catch ( Scribunto_LuaError $ex ) {
109  if ( substr( $ex->getLuaMessage(), 0, 6 ) === 'SKIP: ' ) {
110  $this->markTestSkipped( substr( $ex->getLuaMessage(), 6 ) );
111  } else {
112  throw $ex;
113  }
114  }
115  $this->assertSame( $expected, $actual );
116  }
117  $this->luaTestName = null;
118  }
119 }
Scribunto_LuaEngineUnitTestBase\tearDown
tearDown()
Definition: LuaEngineUnitTestBase.php:60
Scribunto_LuaEngineUnitTestBase\__construct
__construct( $name=null, array $data=[], $dataName='', $engineName=null)
Definition: LuaEngineUnitTestBase.php:46
Scribunto_LuaError
Definition: LuaCommon.php:992
Scribunto_LuaEngineUnitTestBase\testLua
testLua( $key, $testName, $expected)
@dataProvider provideLuaData
Definition: LuaEngineUnitTestBase.php:101
Scribunto_LuaEngineUnitTestBase\provideLuaData
provideLuaData()
Definition: LuaEngineUnitTestBase.php:87
Scribunto_LuaEngineUnitTestBase
This is the subclass for Lua library tests.
Definition: LuaEngineUnitTestBase.php:12
Scribunto_LuaEngineUnitTestBase\toString
toString()
Definition: LuaEngineUnitTestBase.php:72
Scribunto_LuaEngineUnitTestBase\$luaTestName
string $luaTestName
Name to display instead of the default.
Definition: LuaEngineUnitTestBase.php:26
Scribunto_LuaEngineUnitTestBase\$engine
$engine
Definition: LuaEngineUnitTestBase.php:19
Scribunto_LuaEngineUnitTestBase\getTestModules
getTestModules()
Definition: LuaEngineUnitTestBase.php:81
makeSuite
static makeSuite( $className, $group=null)
Definition: LuaEngineTestHelper.php:24
Scribunto_LuaError\getLuaMessage
getLuaMessage()
Definition: LuaCommon.php:1007
Scribunto_LuaEngineUnitTestBase\$engineName
$engineName
Definition: LuaEngineUnitTestBase.php:18
Scribunto_LuaEngineUnitTestBase\$staticEngineName
static $staticEngineName
Definition: LuaEngineUnitTestBase.php:17
Scribunto_LuaEngineTestHelper
trait Scribunto_LuaEngineTestHelper
Trait that helps LuaEngineTestBase and LuaEngineUnitTestBase.
Definition: LuaEngineTestHelper.php:6
Scribunto_LuaEngineUnitTestBase\$luaDataProvider
$luaDataProvider
Definition: LuaEngineUnitTestBase.php:20
Scribunto_LuaEngineUnitTestBase\$moduleName
static string $moduleName
Name of the module being tested.
Definition: LuaEngineUnitTestBase.php:32
Scribunto_LuaEngineUnitTestBase\$skipTests
array $skipTests
Tests to skip.
Definition: LuaEngineUnitTestBase.php:44
Scribunto_LuaEngineUnitTestBase\suite
static suite( $className)
Definition: LuaEngineUnitTestBase.php:56
Scribunto_LuaEngineUnitTestBase\$dataProviderClass
static string $dataProviderClass
Class to use for the data provider.
Definition: LuaEngineUnitTestBase.php:38
getEngine
getEngine()
Definition: LuaEngineTestHelper.php:109