Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaCommon/LuaEngineTestBase.php

raw symbols SCIP
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 MediaWiki\Title\Title;
8 use MediaWikiLangTestCase;
9 use PHPUnit\Framework\TestSuite;
10
11 /**
12 * This is the subclass for Lua library tests. It will automatically run all
13 * tests against LuaSandbox and LuaStandalone.
14 *
15 * Most of the time, you'll only need to override the following:
16 * - $moduleName: Name of the module being tested
17 * - getTestModules(): Add a mapping from $moduleName to the file containing
18 * the code.
19 */
20 abstract class LuaEngineTestBase extends MediaWikiLangTestCase {
21 use LuaEngineTestHelper;
22
23 /** @var string|null */
24 private static $staticEngineName = null;
25 /** @var string|null */
26 private $engineName = null;
27 /** @var LuaEngine|null */
28 private $engine = null;
29 /** @var LuaDataProvider|null */
30 private $luaDataProvider = null;
31
32 /**
33 * Name to display instead of the default
34 * @var string
35 */
36 protected $luaTestName = null;
37
38 /**
39 * Name of the module being tested
40 * @var string
41 */
42 protected static $moduleName = null;
43
44 /**
45 * Class to use for the data provider
46 * @var string
47 */
48 protected static $dataProviderClass = LuaDataProvider::class;
49
50 /**
51 * Tests to skip. Associative array mapping test name to skip reason.
52 * @var array
53 */
54 protected $skipTests = [];
55
56 /**
57 * @param string|null $name
58 * @param array $data
59 * @param string $dataName
60 * @param string|null $engineName Engine to test with
61 */
62 public function __construct(
63 $name = null, array $data = [], $dataName = '', $engineName = null
64 ) {
65 $this->engineName = $engineName ?? self::$staticEngineName;
66 parent::__construct( $name, $data, $dataName );
67 }
68
69 /**
70 * Create a PHPUnit test suite to run the test against all engines
71 * @param string $className Test class name
72 * @return TestSuite
73 */
74 public static function suite( $className ) {
75 return self::makeSuite( $className );
76 }
77
78 protected function tearDown(): void {
79 if ( $this->luaDataProvider ) {
80 $this->luaDataProvider->destroy();
81 $this->luaDataProvider = null;
82 }
83 if ( $this->engine ) {
84 $this->engine->destroy();
85 $this->engine = null;
86 }
87 parent::tearDown();
88 }
89
90 /**
91 * Get the title used for unit tests
92 *
93 * @return Title
94 */
95 protected function getTestTitle() {
96 // XXX This should use a dedicated test page, not the main page
97 $t = Title::newMainPage();
98 // Force content model to avoid DB queries
99 $t->setContentModel( CONTENT_MODEL_WIKITEXT );
100 return $t;
101 }
102
103 public function toString(): string {
104 // When running tests written in Lua, return a nicer representation in
105 // the failure message.
106 return $this->engineName . ': ' . ( $this->luaTestName ?: parent::toString() );
107 }
108
109 /**
110 * Modules that should exist
111 * @return string[] Mapping module names to files
112 */
113 protected function getTestModules() {
114 return [
115 'TestFramework' => __DIR__ . '/TestFramework.lua',
116 ];
117 }
118
119 public function provideLuaData() {
120 if ( !$this->luaDataProvider ) {
121 $class = static::$dataProviderClass;
122 $this->luaDataProvider = new $class ( $this->getEngine(), static::$moduleName );
123 }
124 return $this->luaDataProvider;
125 }
126
127 /**
128 * @dataProvider provideLuaData
129 * @param string $key
130 * @param string $testName
131 * @param mixed $expected
132 */
133 public function testLua( $key, $testName, $expected ) {
134 $this->luaTestName = static::$moduleName . "[$key]: $testName";
135 if ( isset( $this->skipTests[$testName] ) ) {
136 $this->markTestSkipped( $this->skipTests[$testName] );
137 } else {
138 try {
139 $actual = $this->provideLuaData()->run( $key );
140 } catch ( LuaError $ex ) {
141 if ( substr( $ex->getLuaMessage(), 0, 6 ) === 'SKIP: ' ) {
142 $this->markTestSkipped( substr( $ex->getLuaMessage(), 6 ) );
143 } else {
144 throw $ex;
145 }
146 }
147 $this->assertSame( $expected, $actual );
148 }
149 $this->luaTestName = null;
150 }
151 }
152
153 class_alias( LuaEngineTestBase::class, 'Scribunto_LuaEngineTestBase' );