TemplateParser: make most functions protected, only expose processTemplate()
authorKunal Mehta <legoktm@gmail.com>
Mon, 23 Mar 2015 03:06:28 +0000 (20:06 -0700)
committerKunal Mehta <legoktm@gmail.com>
Mon, 23 Mar 2015 03:31:59 +0000 (20:31 -0700)
All of the other functions expose internal implementation details, which no
external caller should ever need. In fact, no external caller does use these
functions directly.

The TemplateParser::compile() tests were removed as they're simply just
checking LightnCandy functionality, which is something the library should be
doing.

Change-Id: If9003d40315e0e5aa361c174b764b799e3b88c34

includes/TemplateParser.php
tests/phpunit/data/templates/foobar_args.mustache [new file with mode: 0644]
tests/phpunit/includes/TemplateParserTest.php

index 0131fe6..b105ca4 100644 (file)
@@ -51,7 +51,7 @@ class TemplateParser {
         * @return string
         * @throws UnexpectedValueException Disallows upwards directory traversal via $templateName
         */
-       public function getTemplateFilename( $templateName ) {
+       protected function getTemplateFilename( $templateName ) {
                // Prevent upwards directory traversal using same methods as Title::secureAndSplit
                if (
                        strpos( $templateName, '.' ) !== false &&
@@ -77,7 +77,7 @@ class TemplateParser {
         * @return callable
         * @throws RuntimeException
         */
-       public function getTemplate( $templateName ) {
+       protected function getTemplate( $templateName ) {
                // If a renderer has already been defined for this template, reuse it
                if ( isset( $this->renderers[$templateName] ) && is_callable( $this->renderers[$templateName] ) ) {
                        return $this->renderers[$templateName];
@@ -145,9 +145,9 @@ class TemplateParser {
         * @return string PHP code (without '<?php')
         * @throws RuntimeException
         */
-       public function compileForEval( $fileContents, $filename ) {
+       protected function compileForEval( $fileContents, $filename ) {
                // Compile the template into PHP code
-               $code = self::compile( $fileContents );
+               $code = $this->compile( $fileContents );
 
                if ( !$code ) {
                        throw new RuntimeException( "Could not compile template: {$filename}" );
@@ -167,7 +167,7 @@ class TemplateParser {
         * @return string PHP code (with '<?php')
         * @throws RuntimeException
         */
-       public static function compile( $code ) {
+       protected function compile( $code ) {
                if ( !class_exists( 'LightnCandy' ) ) {
                        throw new RuntimeException( 'LightnCandy class not defined' );
                }
diff --git a/tests/phpunit/data/templates/foobar_args.mustache b/tests/phpunit/data/templates/foobar_args.mustache
new file mode 100644 (file)
index 0000000..cfbe3d0
--- /dev/null
@@ -0,0 +1 @@
+hello {{planet}}!
index f884a8e..81854ff 100644 (file)
@@ -19,64 +19,45 @@ class TemplateParserTest extends MediaWikiTestCase {
        }
 
        /**
+        * @dataProvider provideProcessTemplate
+        * @covers TemplateParser::processTemplate
+        * @covers TemplateParser::getTemplate
         * @covers TemplateParser::getTemplateFilename
-        * @dataProvider provideGetTemplateFilename
         */
-       public function testGetTemplateFilename( $dir, $name, $result, $exception = false ) {
+       public function testProcessTemplate( $name, $args, $result, $exception = false ) {
                if ( $exception ) {
                        $this->setExpectedException( $exception );
                }
-
-               $tp = new TemplateParser( $dir );
-               $path = $tp->getTemplateFilename( $name );
-               $this->assertEquals( $result, $path );
+               $tp = new TemplateParser( $this->templateDir );
+               $this->assertEquals( $result, $tp->processTemplate( $name, $args ) );
        }
 
-       public static function provideGetTemplateFilename() {
+       public static function provideProcessTemplate() {
                return array(
                        array(
-                               'dir/templates',
                                'foobar',
-                               'dir/templates/foobar.mustache',
+                               array(),
+                               "hello world!\n"
+                       ),
+                       array(
+                               'foobar_args',
+                               array(
+                                       'planet' => 'world',
+                               ),
+                               "hello world!\n",
                        ),
                        array(
-                               'dir/templates',
                                '../foobar',
-                               '',
+                               array(),
+                               false,
                                'UnexpectedValueException'
                        ),
-               );
-       }
-
-       /**
-        * @covers TemplateParser::getTemplate
-        */
-       public function testGetTemplate() {
-               $tp = new TemplateParser( $this->templateDir );
-               $this->assertTrue( is_callable( $tp->getTemplate( 'foobar' ) ) );
-       }
-
-       /**
-        * @covers TemplateParser::compile
-        */
-       public function testTemplateCompilation() {
-               $this->assertRegExp(
-                       '/^<\?php return function/',
-                       TemplateParser::compile( "test" ),
-                       'compile a simple mustache template'
-               );
-       }
-
-       /**
-        * @covers TemplateParser::compile
-        */
-       public function testTemplateCompilationWithVariable() {
-               $this->assertRegExp(
-                       '/return \'\'\.htmlentities\(\(string\)\(\(isset\(\$in\[\'value\'\]\) && '
-                               . 'is_array\(\$in\)\) \? \$in\[\'value\'\] : null\), ENT_QUOTES, '
-                               . '\'UTF-8\'\)\.\'\';/',
-                       TemplateParser::compile( "{{value}}" ),
-                       'compile a mustache template with an escaped variable'
+                       array(
+                               'nonexistenttemplate',
+                               array(),
+                               false,
+                               'RuntimeException',
+                       )
                );
        }
 }