Merge "Drop cache interwiki"
[mediawiki.git] / tests / phpunit / includes / api / format / ApiFormatTestBase.php
blob9bae105e3fdd51868402b9cf65de875e4909f583
1 <?php
3 namespace MediaWiki\Tests\Api\Format;
5 use BadMethodCallException;
6 use Exception;
7 use MediaWiki\Api\ApiMain;
8 use MediaWiki\Context\RequestContext;
9 use MediaWiki\Request\FauxRequest;
10 use MediaWikiIntegrationTestCase;
12 abstract class ApiFormatTestBase extends MediaWikiIntegrationTestCase {
14 protected function setUp(): void {
15 parent::setUp();
16 // These tests cover page rendering end-to-end, and run lots of extension hooks
17 // that don't expect to be executed in tests.
18 $this->clearHooks();
21 /**
22 * Name of the formatter being tested
23 * @var string
25 protected $printerName;
27 /**
28 * Return general data to be encoded for testing
29 * @return array See self::testGeneralEncoding
31 public static function provideGeneralEncoding() {
32 throw new BadMethodCallException( static::class . ' must implement ' . __METHOD__ );
35 /**
36 * Get the formatter output for the given input data
37 * @param array $params Query parameters
38 * @param array $data Data to encode
39 * @param array $options Options. If passed a string, the string is treated
40 * as the 'class' option.
41 * - name: Format name, rather than $this->printerName
42 * - class: If set, register 'name' with this class (and 'factory', if that's set)
43 * - factory: Used with 'class' to register at runtime
44 * - returnPrinter: Return the printer object
45 * @return string|array The string if $options['returnPrinter'] isn't set, or an array if it is:
46 * - text: Output text string
47 * - printer: ApiFormatBase
48 * @throws Exception
50 protected function encodeData( array $params, array $data, $options = [] ) {
51 if ( is_string( $options ) ) {
52 $options = [ 'class' => $options ];
54 $printerName = $options['name'] ?? $this->printerName;
55 $flags = $options['flags'] ?? 0;
57 $context = new RequestContext;
58 $fauxRequest = new FauxRequest( $params, true );
59 $fauxRequest->setRequestURL( 'https://' );
60 $context->setRequest( $fauxRequest );
61 $main = new ApiMain( $context );
62 if ( isset( $options['class'] ) ) {
63 $spec = [
64 'class' => $options['class']
67 if ( isset( $options['factory'] ) ) {
68 $spec['factory'] = $options['factory'];
71 $main->getModuleManager()->addModule( $printerName, 'format', $spec );
73 $result = $main->getResult();
74 $result->addArrayType( null, 'default' );
75 foreach ( $data as $k => $v ) {
76 $result->addValue( null, $k, $v, $flags );
79 $ret = [];
80 $printer = $main->createPrinterByName( $printerName );
81 $printer->initPrinter();
82 $printer->execute();
83 ob_start();
84 try {
85 $printer->closePrinter();
86 $ret['text'] = ob_get_clean();
87 } catch ( Exception $ex ) {
88 ob_end_clean();
89 throw $ex;
92 if ( !empty( $options['returnPrinter'] ) ) {
93 $ret['printer'] = $printer;
96 return count( $ret ) === 1 ? $ret['text'] : $ret;
99 /**
100 * @dataProvider provideGeneralEncoding
101 * @param array $data Data to be encoded
102 * @param string|Exception $expect String to expect, or exception expected to be thrown
103 * @param array $params Query parameters to set in the MediaWiki\Request\FauxRequest
104 * @param array $options Options to pass to self::encodeData()
106 public function testGeneralEncoding(
107 array $data, $expect, array $params = [], array $options = []
109 if ( $expect instanceof Exception ) {
110 $this->expectException( get_class( $expect ) );
111 $this->expectExceptionMessage( $expect->getMessage() );
112 $this->encodeData( $params, $data, $options ); // Should throw
113 } else {
114 $this->assertSame( $expect, $this->encodeData( $params, $data, $options ) );