MediaWiki  1.34.0
LuaEngineTestBase.php
Go to the documentation of this file.
1 <?php
2 
12 abstract class Scribunto_LuaEngineTestBase extends MediaWikiLangTestCase {
14 
15  private static $staticEngineName = null;
16  private $engineName = null;
17  private $engine = null;
18  private $luaDataProvider = null;
19 
24  protected $luaTestName = null;
25 
30  protected static $moduleName = null;
31 
36  protected static $dataProviderClass = 'Scribunto_LuaDataProvider';
37 
42  protected $skipTests = [];
43 
44  public function __construct(
45  $name = null, array $data = [], $dataName = '', $engineName = null
46  ) {
47  if ( $engineName === null ) {
49  }
50  $this->engineName = $engineName;
51  parent::__construct( $name, $data, $dataName );
52  }
53 
54  public static function suite( $className ) {
55  return self::makeSuite( $className );
56  }
57 
58  protected function tearDown() {
59  if ( $this->luaDataProvider ) {
60  $this->luaDataProvider->destroy();
61  $this->luaDataProvider = null;
62  }
63  if ( $this->engine ) {
64  $this->engine->destroy();
65  $this->engine = null;
66  }
67  parent::tearDown();
68  }
69 
75  protected function getTestTitle() {
76  return Title::newMainPage();
77  }
78 
79  public function toString() {
80  // When running tests written in Lua, return a nicer representation in
81  // the failure message.
82  if ( $this->luaTestName ) {
83  return $this->engineName . ': ' . $this->luaTestName;
84  }
85  return $this->engineName . ': ' . parent::toString();
86  }
87 
88  protected function getTestModules() {
89  return [
90  'TestFramework' => __DIR__ . '/TestFramework.lua',
91  ];
92  }
93 
94  public function provideLuaData() {
95  if ( !$this->luaDataProvider ) {
96  $class = static::$dataProviderClass;
97  $this->luaDataProvider = new $class ( $this->getEngine(), static::$moduleName );
98  }
100  }
101 
108  public function testLua( $key, $testName, $expected ) {
109  $this->luaTestName = static::$moduleName . "[$key]: $testName";
110  if ( isset( $this->skipTests[$testName] ) ) {
111  $this->markTestSkipped( $this->skipTests[$testName] );
112  } else {
113  try {
114  $actual = $this->provideLuaData()->run( $key );
115  } catch ( Scribunto_LuaError $ex ) {
116  if ( substr( $ex->getLuaMessage(), 0, 6 ) === 'SKIP: ' ) {
117  $this->markTestSkipped( substr( $ex->getLuaMessage(), 6 ) );
118  } else {
119  throw $ex;
120  }
121  }
122  $this->assertSame( $expected, $actual );
123  }
124  $this->luaTestName = null;
125  }
126 }
127 
128 class Scribunto_LuaEngineTestSkip extends PHPUnit\Framework\TestCase {
129  private $className = '';
130  private $message = '';
131 
132  public function __construct( $className = '', $message = '' ) {
133  $this->className = $className;
134  $this->message = $message;
135  parent::__construct( 'testDummy' );
136  }
137 
138  public function testDummy() {
139  if ( $this->className ) {
140  $this->markTestSkipped( $this->message );
141  } else {
142  // Dummy
143  $this->assertTrue( true );
144  }
145  }
146 
147  public function toString() {
148  return $this->className;
149  }
150 }
Scribunto_LuaEngineTestSkip\testDummy
testDummy()
Definition: LuaEngineTestBase.php:138
Scribunto_LuaError
Definition: LuaCommon.php:992
Scribunto_LuaEngineTestBase\$luaTestName
string $luaTestName
Name to display instead of the default.
Definition: LuaEngineTestBase.php:24
Scribunto_LuaEngineTestBase\$engineName
$engineName
Definition: LuaEngineTestBase.php:16
Scribunto_LuaEngineTestSkip
Definition: LuaEngineTestBase.php:128
Title\newMainPage
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:649
Scribunto_LuaEngineTestBase\$skipTests
array $skipTests
Tests to skip.
Definition: LuaEngineTestBase.php:42
Scribunto_LuaEngineTestBase\provideLuaData
provideLuaData()
Definition: LuaEngineTestBase.php:94
Scribunto_LuaEngineTestBase\getTestTitle
getTestTitle()
Get the title used for unit tests.
Definition: LuaEngineTestBase.php:75
Scribunto_LuaEngineTestBase\toString
toString()
Definition: LuaEngineTestBase.php:79
Scribunto_LuaEngineTestSkip\$className
$className
Definition: LuaEngineTestBase.php:129
Scribunto_LuaEngineTestBase\testLua
testLua( $key, $testName, $expected)
@dataProvider provideLuaData
Definition: LuaEngineTestBase.php:108
Scribunto_LuaEngineTestBase\suite
static suite( $className)
Definition: LuaEngineTestBase.php:54
Scribunto_LuaEngineTestBase\$dataProviderClass
static string $dataProviderClass
Class to use for the data provider.
Definition: LuaEngineTestBase.php:36
Scribunto_LuaEngineTestBase
This is the subclass for Lua library tests.
Definition: LuaEngineTestBase.php:12
Scribunto_LuaEngineTestBase\tearDown
tearDown()
Definition: LuaEngineTestBase.php:58
Scribunto_LuaEngineTestSkip\toString
toString()
Definition: LuaEngineTestBase.php:147
Scribunto_LuaEngineTestBase\getTestModules
getTestModules()
Definition: LuaEngineTestBase.php:88
Scribunto_LuaEngineTestSkip\$message
$message
Definition: LuaEngineTestBase.php:130
Scribunto_LuaEngineTestBase\$luaDataProvider
$luaDataProvider
Definition: LuaEngineTestBase.php:18
Scribunto_LuaEngineTestBase\$engine
$engine
Definition: LuaEngineTestBase.php:17
makeSuite
static makeSuite( $className, $group=null)
Definition: LuaEngineTestHelper.php:24
Scribunto_LuaError\getLuaMessage
getLuaMessage()
Definition: LuaCommon.php:1007
Scribunto_LuaEngineTestBase\__construct
__construct( $name=null, array $data=[], $dataName='', $engineName=null)
Definition: LuaEngineTestBase.php:44
Scribunto_LuaEngineTestHelper
trait Scribunto_LuaEngineTestHelper
Trait that helps LuaEngineTestBase and LuaEngineUnitTestBase.
Definition: LuaEngineTestHelper.php:6
Scribunto_LuaEngineTestSkip\__construct
__construct( $className='', $message='')
Definition: LuaEngineTestBase.php:132
Scribunto_LuaEngineTestBase\$staticEngineName
static $staticEngineName
Definition: LuaEngineTestBase.php:15
getEngine
getEngine()
Definition: LuaEngineTestHelper.php:109
Scribunto_LuaEngineTestBase\$moduleName
static string $moduleName
Name of the module being tested.
Definition: LuaEngineTestBase.php:30