Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon;
4
5 use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaInterpreterNotFoundError;
6 use MediaWiki\Extension\Scribunto\Engines\LuaSandbox\LuaSandboxEngine;
7 use MediaWiki\Extension\Scribunto\Engines\LuaStandalone\LuaStandaloneEngine;
8 use MediaWiki\Extension\Scribunto\ScribuntoEngineBase;
9 use MediaWiki\MediaWikiServices;
10 use MediaWiki\Parser\Parser;
11 use MediaWiki\Parser\ParserOptions;
12 use MediaWiki\Title\Title;
13 use PHPUnit\Framework\DataProviderTestSuite;
14 use PHPUnit\Framework\TestSuite;
15 use PHPUnit\Framework\WarningTestCase;
16 use PHPUnit\Util\Test;
17 use ReflectionClass;
18
19 /**
20 * Trait that helps LuaEngineTestBase and LuaEngineUnitTestBase
21 */
22 trait LuaEngineTestHelper {
23 /** @var array[] */
24 private static $engineConfigurations = [
25 'LuaSandbox' => [
26 'class' => LuaSandboxEngine::class,
27 'memoryLimit' => 50000000,
28 'cpuLimit' => 30,
29 'allowEnvFuncs' => true,
30 'maxLangCacheSize' => 30,
31 ],
32 'LuaStandalone' => [
33 'class' => LuaStandaloneEngine::class,
34 'errorFile' => null,
35 'luaPath' => null,
36 'memoryLimit' => 50000000,
37 'cpuLimit' => 30,
38 'allowEnvFuncs' => true,
39 'maxLangCacheSize' => 30,
40 ],
41 ];
42 /** @var int[] */
43 protected $templateLoadCounts = [];
44 /** @var array */
45 protected $extraModules = [];
46
47 /**
48 * Create a PHPUnit test suite to run the test against all engines
49 * @param string $className Test class name
50 * @param string|null $group Engine to run with, or null to run all engines
51 * @return TestSuite
52 */
53 protected static function makeSuite( $className, $group = null ) {
54 $suite = new TestSuite;
55 $suite->setName( $className );
56
57 $class = new ReflectionClass( $className );
58
59 foreach ( self::$engineConfigurations as $engineName => $opts ) {
60 if ( $group !== null && $group !== $engineName ) {
61 continue;
62 }
63
64 try {
65 $parser = MediaWikiServices::getInstance()->getParserFactory()->create();
66 $parser->startExternalParse(
67 Title::newMainPage(),
68 ParserOptions::newFromAnon(),
69 Parser::OT_HTML,
70 true
71 );
72 $engineClass = $opts['class'];
73 $engine = new $engineClass(
74 self::$engineConfigurations[$engineName] + [ 'parser' => $parser ]
75 );
76 $parser->scribunto_engine = $engine;
77 $engine->setTitle( $parser->getTitle() );
78 $engine->getInterpreter();
79 } catch ( LuaInterpreterNotFoundError $e ) {
80 $suite->addTest(
81 new LuaEngineTestSkip(
82 $className, "interpreter for $engineName is not available"
83 ), [ 'Lua', $engineName ]
84 );
85 continue;
86 }
87
88 // Work around PHPUnit breakage: the only straightforward way to
89 // get the data provider is to call Test::getProvidedData, but that
90 // instantiates the class without passing any parameters to the
91 // constructor. But we *need* that engine name.
92 self::$staticEngineName = $engineName;
93
94 $engineSuite = new DataProviderTestSuite;
95 $engineSuite->setName( "$engineName: $className" );
96
97 foreach ( $class->getMethods() as $method ) {
98 if ( Test::isTestMethod( $method ) && $method->isPublic() ) {
99 $name = $method->getName();
100 $groups = Test::getGroups( $className, $name );
101 $groups[] = 'Lua';
102 $groups[] = $engineName;
103 // Only run tests locally if the engine isn't the MW sandbox T125050
104 if ( $engineName !== 'LuaSandbox' ) {
105 $groups[] = 'Standalone';
106 }
107 $groups = array_unique( $groups );
108
109 $data = Test::getProvidedData( $className, $name );
110 if ( is_iterable( $data ) ) {
111 // with @dataProvider
112 $dataSuite = new DataProviderTestSuite(
113 $className . '::' . $name
114 );
115 foreach ( $data as $k => $v ) {
116 $dataSuite->addTest(
117 new $className( $name, $v, $k, $engineName ),
118 $groups
119 );
120 }
121 $engineSuite->addTest( $dataSuite );
122 } elseif ( $data === false ) {
123 // invalid @dataProvider
124 $engineSuite->addTest( new WarningTestCase(
125 "The data provider specified for {$className}::$name is invalid."
126 ) );
127 } else {
128 // no @dataProvider
129 $engineSuite->addTest(
130 new $className( $name, [], '', $engineName ),
131 $groups
132 );
133 }
134 }
135 }
136
137 $suite->addTest( $engineSuite );
138 }
139
140 return $suite;
141 }
142
143 /**
144 * @return ScribuntoEngineBase
145 */
146 protected function getEngine() {
147 if ( !$this->engine ) {
148 $services = MediaWikiServices::getInstance();
149 $parser = $services->getParserFactory()->create();
150 $options = ParserOptions::newFromAnon();
151 $options->setTemplateCallback( [ $this, 'templateCallback' ] );
152 $options->setTargetLanguage( $services->getLanguageFactory()->getLanguage( 'en' ) );
153 $parser->startExternalParse( $this->getTestTitle(), $options, Parser::OT_HTML, true );
154
155 // HACK
156 if ( $this->engineName === 'LuaSandbox' ) {
157 $class = LuaSandboxEngine::class;
158 } elseif ( $this->engineName === 'LuaStandalone' ) {
159 $class = LuaStandaloneEngine::class;
160 }
161
162 $this->engine = new $class(
163 self::$engineConfigurations[$this->engineName] + [ 'parser' => $parser ]
164 );
165 $parser->scribunto_engine = $this->engine;
166 $this->engine->setTitle( $parser->getTitle() );
167 }
168 return $this->engine;
169 }
170
171 /**
172 * @see Parser::statelessFetchTemplate
173 * @param Title $title
174 * @param Parser|false $parser
175 * @return array
176 */
177 public function templateCallback( $title, $parser ) {
178 $this->templateLoadCounts[$title->getFullText()] =
179 ( $this->templateLoadCounts[$title->getFullText()] ?? 0 ) + 1;
180 if ( isset( $this->extraModules[$title->getFullText()] ) ) {
181 return [
182 'text' => $this->extraModules[$title->getFullText()],
183 'finalTitle' => $title,
184 'deps' => []
185 ];
186 }
187
188 $modules = $this->getTestModules();
189 foreach ( $modules as $name => $fileName ) {
190 $modTitle = Title::makeTitle( NS_MODULE, $name );
191 if ( $modTitle->equals( $title ) ) {
192 return [
193 'text' => file_get_contents( $fileName ),
194 'finalTitle' => $title,
195 'deps' => []
196 ];
197 }
198 }
199 return Parser::statelessFetchTemplate( $title, $parser );
200 }
201
202 /**
203 * Get the title used for unit tests
204 *
205 * @return Title
206 */
207 protected function getTestTitle() {
208 return Title::newMainPage();
209 }
210
211 /**
212 * Reset the cached engine. The next call to getEngine() will return a new
213 * object.
214 */
215 protected function resetEngine() {
216 $this->engine = null;
217 }
218
219 }