From: Phantom42 Date: Mon, 1 Jan 2018 19:36:09 +0000 (+0200) Subject: Add tests for ApiFormatRaw X-Git-Tag: 1.31.0-rc.0~1000^2 X-Git-Url: https://git.heureux-cyclage.org/index.php?a=commitdiff_plain;h=92e249c4dcc5b1ae648d23e843d72447fc4ef4f0;p=lhc%2Fweb%2Fwiklou.git Add tests for ApiFormatRaw Bug: T183767 Change-Id: I63ce42dd61f6e47f7278c436cad1e4f05e287b04 --- diff --git a/tests/phpunit/includes/api/format/ApiFormatRawTest.php b/tests/phpunit/includes/api/format/ApiFormatRawTest.php new file mode 100644 index 0000000000..0d3e63f98a --- /dev/null +++ b/tests/phpunit/includes/api/format/ApiFormatRawTest.php @@ -0,0 +1,120 @@ + 'ApiFormatRaw', + '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', + '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() ); + } + +}