Move DatabaseDomain to Rdbms namespace
[mediawiki.git] / tests / phpunit / includes / api / format / ApiFormatTestBase.php
blobfb086e9985aba0bd86d2ba45856f9813c7c37633
1 <?php
3 abstract class ApiFormatTestBase extends MediaWikiTestCase {
5 /**
6 * Name of the formatter being tested
7 * @var string
8 */
9 protected $printerName;
11 /**
12 * Return general data to be encoded for testing
13 * @return array See self::testGeneralEncoding
14 * @throws Exception
16 public static function provideGeneralEncoding() {
17 throw new Exception( 'Subclass must implement ' . __METHOD__ );
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
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 );
35 $result = $main->getResult();
36 $result->addArrayType( null, 'default' );
37 foreach ( $data as $k => $v ) {
38 $result->addValue( null, $k, $v );
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;
54 /**
55 * @dataProvider provideGeneralEncoding
57 public function testGeneralEncoding( array $data, $expect, array $params = [] ) {
58 if ( isset( $params['SKIP'] ) ) {
59 $this->markTestSkipped( $expect );
61 $this->assertSame( $expect, $this->encodeData( $params, $data ) );