1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
<?php
/**
* @group API
* @covers ApiFormatRaw
*/
class ApiFormatRawTest extends ApiFormatTestBase {
protected $printerName = 'raw';
/**
* Test basic encoding and missing mime and text exceptions
* @return array datasets
*/
public static function provideGeneralEncoding() {
$options = [
'class' => ApiFormatRaw::class,
'factory' => function ( ApiMain $main ) {
return new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) );
}
];
return [
[
[ 'mime' => 'text/plain', 'text' => 'foo' ],
'foo',
[],
$options
],
[
[ 'mime' => 'text/plain', 'text' => 'fóo' ],
'fóo',
[],
$options
],
[
[ 'text' => 'some text' ],
new MWException( 'No MIME type set for raw formatter' ),
[],
$options
],
[
[ 'mime' => 'text/plain' ],
new MWException( 'No text given for raw formatter' ),
[],
$options
],
'test error fallback' => [
[ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
'{"mime":"text/plain","text":"some text","error":"some error"}',
[],
$options
]
];
}
/**
* Test specifying filename
*/
public function testFilename() {
$printer = new ApiFormatRaw( new ApiMain );
$printer->getResult()->addValue( null, 'filename', 'whatever.raw' );
$this->assertSame( 'whatever.raw', $printer->getFilename() );
}
/**
* Test specifying filename with error fallback printer
*/
public function testErrorFallbackFilename() {
$apiMain = new ApiMain;
$printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) );
$printer->getResult()->addValue( null, 'error', 'some error' );
$printer->getResult()->addValue( null, 'filename', 'whatever.raw' );
$this->assertSame( 'api-result.json', $printer->getFilename() );
}
/**
* Test specifying mime
*/
public function testMime() {
$printer = new ApiFormatRaw( new ApiMain );
$printer->getResult()->addValue( null, 'mime', 'text/plain' );
$this->assertSame( 'text/plain', $printer->getMimeType() );
}
/**
* Test specifying mime with error fallback printer
*/
public function testErrorFallbackMime() {
$apiMain = new ApiMain;
$printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) );
$printer->getResult()->addValue( null, 'error', 'some error' );
$printer->getResult()->addValue( null, 'mime', 'text/plain' );
$this->assertSame( 'application/json', $printer->getMimeType() );
}
/**
* Check that setting failWithHTTPError to true will result in 400 response status code
*/
public function testFailWithHTTPError() {
$apiMain = null;
$this->testGeneralEncoding(
[ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
'{"mime":"text/plain","text":"some text","error":"some error"}',
[],
[
'class' => ApiFormatRaw::class,
'factory' => function ( ApiMain $main ) use ( &$apiMain ) {
$apiMain = $main;
$printer = new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) );
$printer->setFailWithHTTPError( true );
return $printer;
}
]
);
$this->assertEquals( 400, $apiMain->getRequest()->response()->getStatusCode() );
}
}
|