Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaCommon/LuaEnvironmentComparisonTest.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\LuaInterpreterNotFoundError;
7 use MediaWiki\Extension\Scribunto\Engines\LuaSandbox\LuaSandboxEngine;
8 use MediaWiki\Extension\Scribunto\Engines\LuaStandalone\LuaStandaloneEngine;
9 use MediaWiki\MediaWikiServices;
10 use MediaWiki\Parser\Parser;
11 use MediaWiki\Parser\ParserOptions;
12 use MediaWiki\Title\Title;
13 use MediaWikiCoversValidator;
14 use PHPUnit\Framework\TestCase;
15
16 /**
17 * @group Lua
18 * @group LuaSandbox
19 * @group LuaStandalone
20 * @coversNothing
21 */
22 class LuaEnvironmentComparisonTest extends TestCase {
23 use MediaWikiCoversValidator;
24
25 /** @var array */
26 public $sandboxOpts = [
27 'memoryLimit' => 50000000,
28 'cpuLimit' => 30,
29 'allowEnvFuncs' => true,
30 ];
31 /** @var array */
32 public $standaloneOpts = [
33 'errorFile' => null,
34 'luaPath' => null,
35 'memoryLimit' => 50000000,
36 'cpuLimit' => 30,
37 'allowEnvFuncs' => true,
38 ];
39
40 /** @var LuaEngine[] */
41 protected $engines = [];
42
43 private function makeEngine( $class, $opts ) {
44 $parser = MediaWikiServices::getInstance()->getParserFactory()->create();
45 $options = ParserOptions::newFromAnon();
46 $options->setTemplateCallback( [ $this, 'templateCallback' ] );
47 $parser->startExternalParse( Title::newMainPage(), $options, Parser::OT_HTML, true );
48 $engine = new $class ( [ 'parser' => $parser ] + $opts );
49 $parser->scribunto_engine = $engine;
50 $engine->setTitle( $parser->getTitle() );
51 $engine->getInterpreter();
52 return $engine;
53 }
54
55 protected function setUp(): void {
56 parent::setUp();
57
58 try {
59 $this->engines['LuaSandbox'] = $this->makeEngine(
60 LuaSandboxEngine::class, $this->sandboxOpts
61 );
62 } catch ( LuaInterpreterNotFoundError $e ) {
63 $this->markTestSkipped( "LuaSandbox interpreter not available" );
64 }
65
66 try {
67 $this->engines['LuaStandalone'] = $this->makeEngine(
68 LuaStandaloneEngine::class, $this->standaloneOpts
69 );
70 } catch ( LuaInterpreterNotFoundError $e ) {
71 $this->markTestSkipped( "LuaStandalone interpreter not available" );
72 }
73 }
74
75 protected function tearDown(): void {
76 foreach ( $this->engines as $engine ) {
77 $engine->destroy();
78 }
79 $this->engines = [];
80 parent::tearDown();
81 }
82
83 private function getGlobalEnvironment( $engine ) {
84 static $script = <<<LUA
85 xxxseen = {}
86 function xxxGetTable( t )
87 if xxxseen[t] then
88 return 'table'
89 end
90 local ret = {}
91 xxxseen[t] = ret
92 for k, v in pairs( t ) do
93 if k ~= '_G' and string.sub( k, 1, 3 ) ~= 'xxx' then
94 if type( v ) == 'table' then
95 ret[k] = xxxGetTable( v )
96 elseif type( v ) == 'string'
97 or type( v ) == 'number'
98 or type( v ) == 'boolean'
99 or type( v ) == 'nil'
100 then
101 ret[k] = v
102 else
103 ret[k] = type( v )
104 end
105 end
106 end
107 return ret
108 end
109 return xxxGetTable( _G )
110 LUA;
111 $func = $engine->getInterpreter()->loadString( $script, 'script' );
112 return $engine->getInterpreter()->callFunction( $func );
113 }
114
115 public function testGlobalEnvironment() {
116 // Grab the first engine as the "standard"
117 $firstEngine = reset( $this->engines );
118 $firstName = key( $this->engines );
119 $firstEnv = $this->getGlobalEnvironment( $firstEngine );
120
121 // Test all others against it
122 foreach ( $this->engines as $secondName => $secondEngine ) {
123 if ( $secondName !== $firstName ) {
124 $secondEnv = $this->getGlobalEnvironment( $secondEngine );
125 $this->assertEquals( $firstEnv, $secondEnv,
126 "Environments for $firstName and $secondName are not equivalent" );
127 }
128 }
129 }
130 }