| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api\Format; |
| 4 |
|
| 5 |
use BadMethodCallException; |
| 6 |
use Exception; |
| 7 |
use MediaWiki\Api\ApiMain; |
| 8 |
use MediaWiki\Context\RequestContext; |
| 9 |
use MediaWiki\Request\FauxRequest; |
| 10 |
use MediaWikiIntegrationTestCase; |
| 11 |
|
| 12 |
abstract class ApiFormatTestBase extends MediaWikiIntegrationTestCase { |
| 13 |
|
| 14 |
protected function setUp(): void { |
| 15 |
parent::setUp(); |
| 16 |
|
| 17 |
|
| 18 |
$this->clearHooks(); |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
protected $printerName; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public static function provideGeneralEncoding() { |
| 32 |
throw new BadMethodCallException( static::class . ' must implement ' . __METHOD__ ); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
protected function encodeData( array $params, array $data, $options = [] ) { |
| 51 |
if ( is_string( $options ) ) { |
| 52 |
$options = [ 'class' => $options ]; |
| 53 |
} |
| 54 |
$printerName = $options['name'] ?? $this->printerName; |
| 55 |
$flags = $options['flags'] ?? 0; |
| 56 |
|
| 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'] |
| 65 |
]; |
| 66 |
|
| 67 |
if ( isset( $options['factory'] ) ) { |
| 68 |
$spec['factory'] = $options['factory']; |
| 69 |
} |
| 70 |
|
| 71 |
$main->getModuleManager()->addModule( $printerName, 'format', $spec ); |
| 72 |
} |
| 73 |
$result = $main->getResult(); |
| 74 |
$result->addArrayType( null, 'default' ); |
| 75 |
foreach ( $data as $k => $v ) { |
| 76 |
$result->addValue( null, $k, $v, $flags ); |
| 77 |
} |
| 78 |
|
| 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; |
| 90 |
} |
| 91 |
|
| 92 |
if ( !empty( $options['returnPrinter'] ) ) { |
| 93 |
$ret['printer'] = $printer; |
| 94 |
} |
| 95 |
|
| 96 |
return count( $ret ) === 1 ? $ret['text'] : $ret; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function testGeneralEncoding( |
| 107 |
array $data, $expect, array $params = [], array $options = [] |
| 108 |
) { |
| 109 |
if ( $expect instanceof Exception ) { |
| 110 |
$this->expectException( get_class( $expect ) ); |
| 111 |
$this->expectExceptionMessage( $expect->getMessage() ); |
| 112 |
$this->encodeData( $params, $data, $options ); |
| 113 |
} else { |
| 114 |
$this->assertSame( $expect, $this->encodeData( $params, $data, $options ) ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
} |