67949ab9f2df6be08b084341e639dd695452d3e9
[lhc/web/wiklou.git] / tests / phpunit / includes / api / format / ApiFormatTestBase.php
1 <?php
2
3 abstract class ApiFormatTestBase extends MediaWikiTestCase {
4
5 /**
6 * Name of the formatter being tested
7 * @var string
8 */
9 protected $printerName;
10
11 /**
12 * Return general data to be encoded for testing
13 * @return array See self::testGeneralEncoding
14 * @throws Exception
15 */
16 public static function provideGeneralEncoding() {
17 throw new Exception( 'Subclass must implement ' . __METHOD__ );
18 }
19
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
27 */
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 );
34 }
35 $result = $main->getResult();
36 foreach ( $data as $k => $v ) {
37 $result->addValue( null, $k, $v );
38 }
39
40 $printer = $main->createPrinterByName( $this->printerName );
41 $printer->initPrinter();
42 $printer->execute();
43 ob_start();
44 try {
45 $printer->closePrinter();
46 return ob_get_clean();
47 } catch ( Exception $ex ) {
48 ob_end_clean();
49 throw $ex;
50 }
51 }
52
53 /**
54 * @dataProvider provideGeneralEncoding
55 */
56 public function testGeneralEncoding( array $data, $expect, array $params = array() ) {
57 if ( isset( $params['SKIP'] ) ) {
58 $this->markTestSkipped( $expect );
59 }
60 $this->assertSame( $expect, $this->encodeData( $params, $data ) );
61 }
62
63 }