wiki.techinc.nl/tests/phpunit/includes/api/format/ApiFormatTestBase.php
Brad Jorsch 39703e9318 Improve testing for ApiFormatBase subclasses
I7b37295e is going to be changing around how ApiResult works,
which is going to need corresponding changes in the formatters. So it
would probably be a good idea to have a decent starting point to catch
any breakage. The non-backwards-compatible changes to ApiFormatTestBase
shouldn't be a concern, as no extensions in Gerrit reference this class
or any /ApiFormat.*Test/ class.

This also fixes two small bugs in ApiFormatWddx (null handling and
spacing for non-fm slow path) discovered during testing, and works
around some HHVM wddx extension bugs.

Bug: T85236
Change-Id: I9cdf896e7070ed51e42625d61609ad9ef91cd567
2014-12-23 14:55:23 -05:00

60 lines
1.5 KiB
PHP

<?php
abstract class ApiFormatTestBase extends MediaWikiTestCase {
/**
* Name of the formatter being tested
* @var string
*/
protected $printerName;
/**
* Return general data to be encoded for testing
* @return array See self::testGeneralEncoding
*/
public static function provideGeneralEncoding() {
throw new Exception( 'Subclass must implement ' . __METHOD__ );
}
/**
* 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 );
}
$printer = $main->createPrinterByName( $this->printerName );
$printer->initPrinter();
$printer->execute();
ob_start();
try {
$printer->closePrinter();
return ob_get_clean();
} catch ( Exception $ex ) {
ob_end_clean();
throw $ex;
}
}
/**
* @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 ) );
}
}