summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/api/format/ApiFormatTestBase.php
blob: cabf62b18a3205691071bce99f82995beebfb0bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?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
	 * @throws Exception
	 */
	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
	 * @return string
	 * @throws Exception
	 */
	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();
		$result->addArrayType( null, 'default' );
		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 ) );
	}

}