3 abstract class ApiFormatTestBase
extends MediaWikiTestCase
{
6 * Name of the formatter being tested
9 protected $printerName;
12 * Return general data to be encoded for testing
13 * @return array See self::testGeneralEncoding
16 public static function provideGeneralEncoding() {
17 throw new Exception( 'Subclass must implement ' . __METHOD__
);
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
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();
46 $printer->closePrinter();
47 return ob_get_clean();
48 } catch ( Exception
$ex ) {
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 ) );