MediaWiki REL1_34
LuaEngineTestBase.php
Go to the documentation of this file.
1<?php
2
12abstract 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
128class 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}
static makeSuite( $className, $group=null)
trait Scribunto_LuaEngineTestHelper
Trait that helps LuaEngineTestBase and LuaEngineUnitTestBase.
This is the subclass for Lua library tests.
__construct( $name=null, array $data=[], $dataName='', $engineName=null)
getTestTitle()
Get the title used for unit tests.
testLua( $key, $testName, $expected)
@dataProvider provideLuaData
static string $dataProviderClass
Class to use for the data provider.
static string $moduleName
Name of the module being tested.
string $luaTestName
Name to display instead of the default.
__construct( $className='', $message='')