Fix 'Tags' padding to keep it farther from the edge and document the source of the...
[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 BadMethodCallException
15 */
16 public static function provideGeneralEncoding() {
17 throw new BadMethodCallException( static::class . ' 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 array $options Options. If passed a string, the string is treated
25 * as the 'class' option.
26 * - name: Format name, rather than $this->printerName
27 * - class: If set, register 'name' with this class (and 'factory', if that's set)
28 * - factory: Used with 'class' to register at runtime
29 * - returnPrinter: Return the printer object
30 * @param callable|null $factory Factory to use instead of the normal one
31 * @return string|array The string if $options['returnPrinter'] isn't set, or an array if it is:
32 * - text: Output text string
33 * - printer: ApiFormatBase
34 * @throws Exception
35 */
36 protected function encodeData( array $params, array $data, $options = [] ) {
37 if ( is_string( $options ) ) {
38 $options = [ 'class' => $options ];
39 }
40 $printerName = isset( $options['name'] ) ? $options['name'] : $this->printerName;
41
42 $context = new RequestContext;
43 $context->setRequest( new FauxRequest( $params, true ) );
44 $main = new ApiMain( $context );
45 if ( isset( $options['class'] ) ) {
46 $factory = isset( $options['factory'] ) ? $options['factory'] : null;
47 $main->getModuleManager()->addModule( $printerName, 'format', $options['class'], $factory );
48 }
49 $result = $main->getResult();
50 $result->addArrayType( null, 'default' );
51 foreach ( $data as $k => $v ) {
52 $result->addValue( null, $k, $v );
53 }
54
55 $ret = [];
56 $printer = $main->createPrinterByName( $printerName );
57 $printer->initPrinter();
58 $printer->execute();
59 ob_start();
60 try {
61 $printer->closePrinter();
62 $ret['text'] = ob_get_clean();
63 } catch ( Exception $ex ) {
64 ob_end_clean();
65 throw $ex;
66 }
67
68 if ( !empty( $options['returnPrinter'] ) ) {
69 $ret['printer'] = $printer;
70 }
71
72 return count( $ret ) === 1 ? $ret['text'] : $ret;
73 }
74
75 /**
76 * @dataProvider provideGeneralEncoding
77 * @param array $data Data to be encoded
78 * @param string|Exception $expect String to expect, or exception expected to be thrown
79 * @param array $params Query parameters to set in the FauxRequest
80 * @param array $options Options to pass to self::encodeData()
81 */
82 public function testGeneralEncoding(
83 array $data, $expect, array $params = [], array $options = []
84 ) {
85 if ( $expect instanceof Exception ) {
86 $this->setExpectedException( get_class( $expect ), $expect->getMessage() );
87 $this->encodeData( $params, $data, $options ); // Should throw
88 } else {
89 $this->assertSame( $expect, $this->encodeData( $params, $data, $options ) );
90 }
91 }
92
93 }