8134c50975c749e46b4679f2b7df5f70b0952e8d
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
15 public static function provideGeneralEncoding() {
16 throw new Exception( 'Subclass must implement ' . __METHOD__
);
20 * Get the formatter output for the given input data
21 * @param array $params Query parameters
22 * @param array $data Data to encode
23 * @param string $class Printer class to use instead of the normal one
25 protected function encodeData( array $params, array $data, $class = null ) {
26 $context = new RequestContext
;
27 $context->setRequest( new FauxRequest( $params, true ) );
28 $main = new ApiMain( $context );
29 if ( $class !== null ) {
30 $main->getModuleManager()->addModule( $this->printerName
, 'format', $class );
32 $result = $main->getResult();
33 foreach ( $data as $k => $v ) {
34 $result->addValue( null, $k, $v );
37 $printer = $main->createPrinterByName( $this->printerName
);
38 $printer->initPrinter();
42 $printer->closePrinter();
43 return ob_get_clean();
44 } catch ( Exception
$ex ) {
51 * @dataProvider provideGeneralEncoding
53 public function testGeneralEncoding( array $data, $expect, array $params = array() ) {
54 if ( isset( $params['SKIP'] ) ) {
55 $this->markTestSkipped( $expect );
57 $this->assertSame( $expect, $this->encodeData( $params, $data ) );