MediaWiki  REL1_31
ApiFormatRawTest.php
Go to the documentation of this file.
1 <?php
2 
8 
9  protected $printerName = 'raw';
10 
15  public static function provideGeneralEncoding() {
16  $options = [
17  'class' => ApiFormatRaw::class,
18  'factory' => function ( ApiMain $main ) {
19  return new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) );
20  }
21  ];
22 
23  return [
24  [
25  [ 'mime' => 'text/plain', 'text' => 'foo' ],
26  'foo',
27  [],
28  $options
29  ],
30  [
31  [ 'mime' => 'text/plain', 'text' => 'fóo' ],
32  'fóo',
33  [],
34  $options
35  ],
36  [
37  [ 'text' => 'some text' ],
38  new MWException( 'No MIME type set for raw formatter' ),
39  [],
40  $options
41  ],
42  [
43  [ 'mime' => 'text/plain' ],
44  new MWException( 'No text given for raw formatter' ),
45  [],
46  $options
47  ],
48  'test error fallback' => [
49  [ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
50  '{"mime":"text/plain","text":"some text","error":"some error"}',
51  [],
52  $options
53  ]
54  ];
55  }
56 
60  public function testFilename() {
61  $printer = new ApiFormatRaw( new ApiMain );
62  $printer->getResult()->addValue( null, 'filename', 'whatever.raw' );
63  $this->assertSame( 'whatever.raw', $printer->getFilename() );
64  }
65 
69  public function testErrorFallbackFilename() {
70  $apiMain = new ApiMain;
71  $printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) );
72  $printer->getResult()->addValue( null, 'error', 'some error' );
73  $printer->getResult()->addValue( null, 'filename', 'whatever.raw' );
74  $this->assertSame( 'api-result.json', $printer->getFilename() );
75  }
76 
80  public function testMime() {
81  $printer = new ApiFormatRaw( new ApiMain );
82  $printer->getResult()->addValue( null, 'mime', 'text/plain' );
83  $this->assertSame( 'text/plain', $printer->getMimeType() );
84  }
85 
89  public function testErrorFallbackMime() {
90  $apiMain = new ApiMain;
91  $printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) );
92  $printer->getResult()->addValue( null, 'error', 'some error' );
93  $printer->getResult()->addValue( null, 'mime', 'text/plain' );
94  $this->assertSame( 'application/json', $printer->getMimeType() );
95  }
96 
100  public function testFailWithHTTPError() {
101  $apiMain = null;
102 
103  $this->testGeneralEncoding(
104  [ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
105  '{"mime":"text/plain","text":"some text","error":"some error"}',
106  [],
107  [
108  'class' => ApiFormatRaw::class,
109  'factory' => function ( ApiMain $main ) use ( &$apiMain ) {
110  $apiMain = $main;
111  $printer = new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) );
112  $printer->setFailWithHTTPError( true );
113  return $printer;
114  }
115  ]
116  );
117  $this->assertEquals( 400, $apiMain->getRequest()->response()->getStatusCode() );
118  }
119 
120 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:43
ApiFormatRawTest\testErrorFallbackMime
testErrorFallbackMime()
Test specifying mime with error fallback printer.
Definition: ApiFormatRawTest.php:89
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
ApiFormatRawTest
API ApiFormatRaw.
Definition: ApiFormatRawTest.php:7
ApiFormatRawTest\testMime
testMime()
Test specifying mime.
Definition: ApiFormatRawTest.php:80
ApiFormatTestBase\testGeneralEncoding
testGeneralEncoding(array $data, $expect, array $params=[], array $options=[])
provideGeneralEncoding
Definition: ApiFormatTestBase.php:82
ApiFormatRawTest\$printerName
$printerName
Definition: ApiFormatRawTest.php:9
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
ApiFormatJson
API JSON output formatter.
Definition: ApiFormatJson.php:27
ApiFormatTestBase
Definition: ApiFormatTestBase.php:3
MWException
MediaWiki exception.
Definition: MWException.php:26
ApiFormatRaw
Formatter that spits out anything you like with any desired MIME type.
Definition: ApiFormatRaw.php:27
$options
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
ApiFormatRawTest\testErrorFallbackFilename
testErrorFallbackFilename()
Test specifying filename with error fallback printer.
Definition: ApiFormatRawTest.php:69
ApiFormatRawTest\provideGeneralEncoding
static provideGeneralEncoding()
Test basic encoding and missing mime and text exceptions.
Definition: ApiFormatRawTest.php:15
ApiFormatRawTest\testFailWithHTTPError
testFailWithHTTPError()
Check that setting failWithHTTPError to true will result in 400 response status code.
Definition: ApiFormatRawTest.php:100
ApiFormatRawTest\testFilename
testFilename()
Test specifying filename.
Definition: ApiFormatRawTest.php:60
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56