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