2011-01-03 21:36:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
2014-12-17 21:48:03 +00:00
|
|
|
abstract class ApiFormatTestBase extends MediaWikiTestCase {
|
2013-10-23 15:10:02 +00:00
|
|
|
|
|
|
|
|
/**
|
2014-12-17 21:48:03 +00:00
|
|
|
* Name of the formatter being tested
|
|
|
|
|
* @var string
|
2013-10-23 15:10:02 +00:00
|
|
|
*/
|
2014-12-17 21:48:03 +00:00
|
|
|
protected $printerName;
|
2011-07-01 16:34:02 +00:00
|
|
|
|
2014-12-17 21:48:03 +00:00
|
|
|
/**
|
|
|
|
|
* Return general data to be encoded for testing
|
|
|
|
|
* @return array See self::testGeneralEncoding
|
|
|
|
|
*/
|
|
|
|
|
public static function provideGeneralEncoding() {
|
|
|
|
|
throw new Exception( 'Subclass must implement ' . __METHOD__ );
|
|
|
|
|
}
|
2013-02-14 13:10:38 +00:00
|
|
|
|
2014-12-17 21:48:03 +00:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
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();
|
|
|
|
|
foreach ( $data as $k => $v ) {
|
|
|
|
|
$result->addValue( null, $k, $v );
|
|
|
|
|
}
|
2011-01-03 21:36:49 +00:00
|
|
|
|
2014-12-17 21:48:03 +00:00
|
|
|
$printer = $main->createPrinterByName( $this->printerName );
|
|
|
|
|
$printer->initPrinter();
|
2011-01-03 21:36:49 +00:00
|
|
|
$printer->execute();
|
2014-12-17 21:48:03 +00:00
|
|
|
ob_start();
|
|
|
|
|
try {
|
|
|
|
|
$printer->closePrinter();
|
|
|
|
|
return ob_get_clean();
|
|
|
|
|
} catch ( Exception $ex ) {
|
|
|
|
|
ob_end_clean();
|
|
|
|
|
throw $ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-03 21:36:49 +00:00
|
|
|
|
2014-12-17 21:48:03 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGeneralEncoding
|
|
|
|
|
*/
|
|
|
|
|
public function testGeneralEncoding( array $data, $expect, array $params = array() ) {
|
|
|
|
|
if ( isset( $params['SKIP'] ) ) {
|
|
|
|
|
$this->markTestSkipped( $expect );
|
|
|
|
|
}
|
|
|
|
|
$this->assertSame( $expect, $this->encodeData( $params, $data ) );
|
2011-01-03 21:36:49 +00:00
|
|
|
}
|
2013-10-23 15:10:02 +00:00
|
|
|
|
2011-01-03 21:36:49 +00:00
|
|
|
}
|