]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/api/format/ApiFormatTestBase.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / tests / phpunit / includes / api / format / ApiFormatTestBase.php
1 <?php
2
3 abstract class ApiFormatTestBase extends MediaWikiTestCase {
4
5         /**
6          * Name of the formatter being tested
7          * @var string
8          */
9         protected $printerName;
10
11         /**
12          * Return general data to be encoded for testing
13          * @return array See self::testGeneralEncoding
14          * @throws Exception
15          */
16         public static function provideGeneralEncoding() {
17                 throw new Exception( 'Subclass must implement ' . __METHOD__ );
18         }
19
20         /**
21          * Get the formatter output for the given input data
22          * @param array $params Query parameters
23          * @param array $data Data to encode
24          * @param string $class Printer class to use instead of the normal one
25          * @return string
26          * @throws Exception
27          */
28         protected function encodeData( array $params, array $data, $class = null ) {
29                 $context = new RequestContext;
30                 $context->setRequest( new FauxRequest( $params, true ) );
31                 $main = new ApiMain( $context );
32                 if ( $class !== null ) {
33                         $main->getModuleManager()->addModule( $this->printerName, 'format', $class );
34                 }
35                 $result = $main->getResult();
36                 $result->addArrayType( null, 'default' );
37                 foreach ( $data as $k => $v ) {
38                         $result->addValue( null, $k, $v );
39                 }
40
41                 $printer = $main->createPrinterByName( $this->printerName );
42                 $printer->initPrinter();
43                 $printer->execute();
44                 ob_start();
45                 try {
46                         $printer->closePrinter();
47                         return ob_get_clean();
48                 } catch ( Exception $ex ) {
49                         ob_end_clean();
50                         throw $ex;
51                 }
52         }
53
54         /**
55          * @dataProvider provideGeneralEncoding
56          */
57         public function testGeneralEncoding( array $data, $expect, array $params = [] ) {
58                 if ( isset( $params['SKIP'] ) ) {
59                         $this->markTestSkipped( $expect );
60                 }
61                 $this->assertSame( $expect, $this->encodeData( $params, $data ) );
62         }
63
64 }