ApiFormatRawTest.php 3.1 KB

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