Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaCommon/LuaEngineUnitTestBase.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 MediaWikiCoversValidator;
8 use PHPUnit\Framework\TestCase;
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 LuaEngineUnitTestBase extends TestCase {
21 use MediaWikiCoversValidator;
22 use LuaEngineTestHelper;
23
24 /** @var string|null */
25 private static $staticEngineName = null;
26 /** @var string|null */
27 private $engineName = null;
28 /** @var LuaEngine|null */
29 private $engine = null;
30 /** @var LuaDataProvider|null */
31 private $luaDataProvider = null;
32
33 /**
34 * Name to display instead of the default
35 * @var string
36 */
37 protected $luaTestName = null;
38
39 /**
40 * Name of the module being tested
41 * @var string
42 */
43 protected static $moduleName = null;
44
45 /**
46 * Class to use for the data provider
47 * @var string
48 */
49 protected static $dataProviderClass = LuaDataProvider::class;
50
51 /**
52 * Tests to skip. Associative array mapping test name to skip reason.
53 * @var array
54 */
55 protected $skipTests = [];
56
57 /**
58 * @param string|null $name
59 * @param array $data
60 * @param string $dataName
61 * @param string|null $engineName Engine to test with
62 */
63 public function __construct(
64 $name = null, array $data = [], $dataName = '', $engineName = null
65 ) {
66 $this->engineName = $engineName ?? self::$staticEngineName;
67 parent::__construct( $name, $data, $dataName );
68 }
69
70 /**
71 * Create a PHPUnit test suite to run the test against all engines
72 * @param string $className Test class name
73 * @return TestSuite
74 */
75 public static function suite( $className ) {
76 return self::makeSuite( $className );
77 }
78
79 protected function tearDown(): void {
80 if ( $this->luaDataProvider ) {
81 $this->luaDataProvider->destroy();
82 $this->luaDataProvider = null;
83 }
84 if ( $this->engine ) {
85 $this->engine->destroy();
86 $this->engine = null;
87 }
88 parent::tearDown();
89 }
90
91 public function toString(): string {
92 // When running tests written in Lua, return a nicer representation in
93 // the failure message.
94 return $this->engineName . ': ' . ( $this->luaTestName ?: parent::toString() );
95 }
96
97 /**
98 * Modules that should exist
99 * @return string[] Mapping module names to files
100 */
101 protected function getTestModules() {
102 return [
103 'TestFramework' => __DIR__ . '/TestFramework.lua',
104 ];
105 }
106
107 public function provideLuaData() {
108 if ( !$this->luaDataProvider ) {
109 $class = static::$dataProviderClass;
110 $this->luaDataProvider = new $class ( $this->getEngine(), static::$moduleName );
111 }
112 return $this->luaDataProvider;
113 }
114
115 /**
116 * @dataProvider provideLuaData
117 * @param string $key
118 * @param string $testName
119 * @param mixed $expected
120 */
121 public function testLua( $key, $testName, $expected ) {
122 $this->luaTestName = static::$moduleName . "[$key]: $testName";
123 if ( isset( $this->skipTests[$testName] ) ) {
124 $this->markTestSkipped( $this->skipTests[$testName] );
125 } else {
126 try {
127 $actual = $this->provideLuaData()->run( $key );
128 } catch ( LuaError $ex ) {
129 if ( substr( $ex->getLuaMessage(), 0, 6 ) === 'SKIP: ' ) {
130 $this->markTestSkipped( substr( $ex->getLuaMessage(), 6 ) );
131 } else {
132 throw $ex;
133 }
134 }
135 $this->assertSame( $expected, $actual );
136 }
137 $this->luaTestName = null;
138 }
139 }