Use ObjectFactory to create API modules
[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 * @return string|array The string if $options['returnPrinter'] isn't set, or an array if it is:
31 * - text: Output text string
32 * - printer: ApiFormatBase
33 * @throws Exception
34 */
35 protected function encodeData( array $params, array $data, $options = [] ) {
36 if ( is_string( $options ) ) {
37 $options = [ 'class' => $options ];
38 }
39 $printerName = $options['name'] ?? $this->printerName;
40 $flags = $options['flags'] ?? 0;
41
42 $context = new RequestContext;
43 $context->setRequest( new FauxRequest( $params, true ) );
44 $main = new ApiMain( $context );
45 if ( isset( $options['class'] ) ) {
46 $spec = [
47 'class' => $options['class']
48 ];
49
50 if ( isset( $options['factory'] ) ) {
51 $spec['factory'] = $options['factory'];
52 }
53
54 $main->getModuleManager()->addModule( $printerName, 'format', $spec );
55 }
56 $result = $main->getResult();
57 $result->addArrayType( null, 'default' );
58 foreach ( $data as $k => $v ) {
59 $result->addValue( null, $k, $v, $flags );
60 }
61
62 $ret = [];
63 $printer = $main->createPrinterByName( $printerName );
64 $printer->initPrinter();
65 $printer->execute();
66 ob_start();
67 try {
68 $printer->closePrinter();
69 $ret['text'] = ob_get_clean();
70 } catch ( Exception $ex ) {
71 ob_end_clean();
72 throw $ex;
73 }
74
75 if ( !empty( $options['returnPrinter'] ) ) {
76 $ret['printer'] = $printer;
77 }
78
79 return count( $ret ) === 1 ? $ret['text'] : $ret;
80 }
81
82 /**
83 * @dataProvider provideGeneralEncoding
84 * @param array $data Data to be encoded
85 * @param string|Exception $expect String to expect, or exception expected to be thrown
86 * @param array $params Query parameters to set in the FauxRequest
87 * @param array $options Options to pass to self::encodeData()
88 */
89 public function testGeneralEncoding(
90 array $data, $expect, array $params = [], array $options = []
91 ) {
92 if ( $expect instanceof Exception ) {
93 $this->setExpectedException( get_class( $expect ), $expect->getMessage() );
94 $this->encodeData( $params, $data, $options ); // Should throw
95 } else {
96 $this->assertSame( $expect, $this->encodeData( $params, $data, $options ) );
97 }
98 }
99
100 }