MediaWiki  1.34.0
LuaEngineTestHelper.php
Go to the documentation of this file.
1 <?php
2 
7  private static $engineConfigurations = [
8  'LuaSandbox' => [
9  'memoryLimit' => 50000000,
10  'cpuLimit' => 30,
11  'allowEnvFuncs' => true,
12  'maxLangCacheSize' => 30,
13  ],
14  'LuaStandalone' => [
15  'errorFile' => null,
16  'luaPath' => null,
17  'memoryLimit' => 50000000,
18  'cpuLimit' => 30,
19  'allowEnvFuncs' => true,
20  'maxLangCacheSize' => 30,
21  ],
22  ];
23 
24  protected static function makeSuite( $className, $group = null ) {
25  $suite = new PHPUnit_Framework_TestSuite;
26  $suite->setName( $className );
27 
28  $class = new ReflectionClass( $className );
29 
30  foreach ( self::$engineConfigurations as $engineName => $opts ) {
31  if ( $group !== null && $group !== $engineName ) {
32  continue;
33  }
34 
35  try {
36  $parser = new Parser;
37  $parser->startExternalParse( Title::newMainPage(), new ParserOptions, Parser::OT_HTML, true );
38  $engineClass = "Scribunto_{$engineName}Engine";
39  $engine = new $engineClass(
40  self::$engineConfigurations[$engineName] + [ 'parser' => $parser ]
41  );
42  $parser->scribunto_engine = $engine;
43  $engine->setTitle( $parser->getTitle() );
44  $engine->getInterpreter();
46  $suite->addTest(
48  $className, "interpreter for $engineName is not available"
49  ), [ 'Lua', $engineName ]
50  );
51  continue;
52  }
53 
54  // Work around PHPUnit breakage: the only straightforward way to
55  // get the data provider is to call
56  // PHPUnit_Util_Test::getProvidedData, but that instantiates the
57  // class without passing any parameters to the constructor. But we
58  // *need* that engine name.
59  self::$staticEngineName = $engineName;
60 
61  $engineSuite = new PHPUnit_Framework_TestSuite;
62  $engineSuite->setName( "$engineName: $className" );
63 
64  foreach ( $class->getMethods() as $method ) {
65  if ( PHPUnit_Framework_TestSuite::isTestMethod( $method ) && $method->isPublic() ) {
66  $name = $method->getName();
67  $groups = PHPUnit_Util_Test::getGroups( $className, $name );
68  $groups[] = 'Lua';
69  $groups[] = $engineName;
70  $groups = array_unique( $groups );
71 
72  $data = PHPUnit_Util_Test::getProvidedData( $className, $name );
73  if ( is_array( $data ) || $data instanceof Iterator ) {
74  // with @dataProvider
75  $dataSuite = new PHPUnit_Framework_TestSuite_DataProvider(
76  $className . '::' . $name
77  );
78  foreach ( $data as $k => $v ) {
79  $dataSuite->addTest(
80  new $className( $name, $v, $k, $engineName ),
81  $groups
82  );
83  }
84  $engineSuite->addTest( $dataSuite );
85  } elseif ( $data === false ) {
86  // invalid @dataProvider
87  $engineSuite->addTest( new PHPUnit_Framework_Warning(
88  "The data provider specified for {$className}::$name is invalid."
89  ) );
90  } else {
91  // no @dataProvider
92  $engineSuite->addTest(
93  new $className( $name, [], '', $engineName ),
94  $groups
95  );
96  }
97  }
98  }
99 
100  $suite->addTest( $engineSuite );
101  }
102 
103  return $suite;
104  }
105 
109  protected function getEngine() {
110  if ( !$this->engine ) {
111  $parser = new Parser;
112  $options = new ParserOptions;
113  $options->setTemplateCallback( [ $this, 'templateCallback' ] );
114  $parser->startExternalParse( $this->getTestTitle(), $options, Parser::OT_HTML, true );
115  $class = "Scribunto_{$this->engineName}Engine";
116  $this->engine = new $class(
117  self::$engineConfigurations[$this->engineName] + [ 'parser' => $parser ]
118  );
119  $parser->scribunto_engine = $this->engine;
120  $this->engine->setTitle( $parser->getTitle() );
121  }
122  return $this->engine;
123  }
124 
125  public function templateCallback( $title, $parser ) {
126  if ( isset( $this->extraModules[$title->getFullText()] ) ) {
127  return [
128  'text' => $this->extraModules[$title->getFullText()],
129  'finalTitle' => $title,
130  'deps' => []
131  ];
132  }
133 
134  $modules = $this->getTestModules();
135  foreach ( $modules as $name => $fileName ) {
136  $modTitle = Title::makeTitle( NS_MODULE, $name );
137  if ( $modTitle->equals( $title ) ) {
138  return [
139  'text' => file_get_contents( $fileName ),
140  'finalTitle' => $title,
141  'deps' => []
142  ];
143  }
144  }
145  return Parser::statelessFetchTemplate( $title, $parser );
146  }
147 
153  protected function getTestTitle() {
154  return Title::newMainPage();
155  }
156 
157 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:42
templateCallback
templateCallback( $title, $parser)
Definition: LuaEngineTestHelper.php:125
Scribunto_LuaEngineTestSkip
Definition: LuaEngineTestBase.php:128
ParserOptions\setTemplateCallback
setTemplateCallback( $x)
Callback for template fetching; first argument to call_user_func().
Definition: ParserOptions.php:837
Title\newMainPage
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:649
Scribunto_LuaInterpreterNotFoundError
Definition: LuaInterpreter.php:64
NS_MODULE
const NS_MODULE
Definition: Scribunto.constants.php:5
$modules
$modules
Definition: HTMLFormElement.php:13
$title
$title
Definition: testCompression.php:34
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
makeSuite
static makeSuite( $className, $group=null)
Definition: LuaEngineTestHelper.php:24
Scribunto_LuaEngineTestHelper
trait Scribunto_LuaEngineTestHelper
Trait that helps LuaEngineTestBase and LuaEngineUnitTestBase.
Definition: LuaEngineTestHelper.php:6
OT_HTML
const OT_HTML
Definition: Defines.php:164
getEngine
getEngine()
Definition: LuaEngineTestHelper.php:109
getTestTitle
getTestTitle()
Get the title used for unit tests.
Definition: LuaEngineTestHelper.php:153