]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - tests/phpunit/includes/api/format/ApiFormatTestBase.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / tests / phpunit / includes / api / format / ApiFormatTestBase.php
diff --git a/tests/phpunit/includes/api/format/ApiFormatTestBase.php b/tests/phpunit/includes/api/format/ApiFormatTestBase.php
new file mode 100644 (file)
index 0000000..fb086e9
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+
+abstract class ApiFormatTestBase extends MediaWikiTestCase {
+
+       /**
+        * Name of the formatter being tested
+        * @var string
+        */
+       protected $printerName;
+
+       /**
+        * Return general data to be encoded for testing
+        * @return array See self::testGeneralEncoding
+        * @throws Exception
+        */
+       public static function provideGeneralEncoding() {
+               throw new Exception( 'Subclass must implement ' . __METHOD__ );
+       }
+
+       /**
+        * Get the formatter output for the given input data
+        * @param array $params Query parameters
+        * @param array $data Data to encode
+        * @param string $class Printer class to use instead of the normal one
+        * @return string
+        * @throws Exception
+        */
+       protected function encodeData( array $params, array $data, $class = null ) {
+               $context = new RequestContext;
+               $context->setRequest( new FauxRequest( $params, true ) );
+               $main = new ApiMain( $context );
+               if ( $class !== null ) {
+                       $main->getModuleManager()->addModule( $this->printerName, 'format', $class );
+               }
+               $result = $main->getResult();
+               $result->addArrayType( null, 'default' );
+               foreach ( $data as $k => $v ) {
+                       $result->addValue( null, $k, $v );
+               }
+
+               $printer = $main->createPrinterByName( $this->printerName );
+               $printer->initPrinter();
+               $printer->execute();
+               ob_start();
+               try {
+                       $printer->closePrinter();
+                       return ob_get_clean();
+               } catch ( Exception $ex ) {
+                       ob_end_clean();
+                       throw $ex;
+               }
+       }
+
+       /**
+        * @dataProvider provideGeneralEncoding
+        */
+       public function testGeneralEncoding( array $data, $expect, array $params = [] ) {
+               if ( isset( $params['SKIP'] ) ) {
+                       $this->markTestSkipped( $expect );
+               }
+               $this->assertSame( $expect, $this->encodeData( $params, $data ) );
+       }
+
+}