MediaWiki REL1_31
ApiFormatTestBase.php
Go to the documentation of this file.
1<?php
2
3abstract class ApiFormatTestBase extends MediaWikiTestCase {
4
9 protected $printerName;
10
16 public static function provideGeneralEncoding() {
17 throw new BadMethodCallException( static::class . ' must implement ' . __METHOD__ );
18 }
19
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
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
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}
string $printerName
Name of the formatter being tested.
static provideGeneralEncoding()
Return general data to be encoded for testing.
encodeData(array $params, array $data, $options=[])
Get the formatter output for the given input data.
testGeneralEncoding(array $data, $expect, array $params=[], array $options=[])
provideGeneralEncoding
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:43
WebRequest clone which takes values from a provided array.
Group all the pieces relevant to the context of a request into one instance.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2811
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2005
$params