[MediaWiki-commits] [Gerrit] mediawiki/core[master]: Add tests for ApiFormatRaw

2018-01-04 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/401405 )

Change subject: Add tests for ApiFormatRaw
..


Add tests for ApiFormatRaw

Bug: T183767
Change-Id: I63ce42dd61f6e47f7278c436cad1e4f05e287b04
---
A tests/phpunit/includes/api/format/ApiFormatRawTest.php
1 file changed, 120 insertions(+), 0 deletions(-)

Approvals:
  Legoktm: Looks good to me, approved
  jenkins-bot: Verified
  Anomie: Looks good to me, but someone else must approve



diff --git a/tests/phpunit/includes/api/format/ApiFormatRawTest.php 
b/tests/phpunit/includes/api/format/ApiFormatRawTest.php
new file mode 100644
index 000..0d3e63f
--- /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 

[MediaWiki-commits] [Gerrit] mediawiki/core[master]: Add tests for ApiFormatRaw

2018-01-01 Thread Phantom42 (Code Review)
Phantom42 has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/401405 )

Change subject: Add tests for ApiFormatRaw
..

Add tests for ApiFormatRaw

Bug: T183767
Change-Id: I63ce42dd61f6e47f7278c436cad1e4f05e287b04
---
A tests/phpunit/includes/api/format/ApiFormatRawTest.php
1 file changed, 110 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/05/401405/1

diff --git a/tests/phpunit/includes/api/format/ApiFormatRawTest.php 
b/tests/phpunit/includes/api/format/ApiFormatRawTest.php
new file mode 100644
index 000..9017926
--- /dev/null
+++ b/tests/phpunit/includes/api/format/ApiFormatRawTest.php
@@ -0,0 +1,110 @@
+setRequest( new FauxRequest( $params, true ) );
+   $main = new ApiMain( $context );
+
+   $result = $main->getResult();
+   $result->addValue( null, 'mime', 'text/plain' );
+   $result->addArrayType( null, 'default' );
+   $result->addValue( null, 'text', $data[0] );
+
+   $printer = new ApiFormatRaw( $main );
+   $printer->initPrinter();
+   $printer->execute();
+   ob_start();
+   try {
+   $printer->closePrinter();
+   return ob_get_clean();
+   } catch ( Exception $ex ) {
+   ob_end_clean();
+   throw $ex;
+   }
+   }
+
+   /**
+* Check that ApiFormatRaw throws exception if mime type is not set
+* @expectedException MWException
+*/
+   public function testMissingMimeError() {
+   $context = new RequestContext;
+   $context->setRequest( new FauxRequest( [], true ) );
+   $main = new ApiMain( $context );
+
+   $result = $main->getResult();
+   $result->addArrayType( null, 'default' );
+   $result->addValue( null, 'text', 'some text' );
+
+   $printer = new ApiFormatRaw( $main );
+   $printer->initPrinter();
+   $printer->execute();
+   ob_start();
+   try {
+   $printer->closePrinter();
+   } catch ( Exception $ex ) {
+   throw $ex;
+   }
+   ob_end_clean();
+   }
+
+   /**
+* Check that ApiFormatRaw throws exception if no text is given
+* @expectedException MWException
+*/
+   public function testNoTextError() {
+   $context = new RequestContext;
+   $context->setRequest( new FauxRequest( [], true ) );
+   $main = new ApiMain( $context );
+
+   $result = $main->getResult();
+   $result->addValue( null, 'mime', 'text/plain' );
+   $result->addArrayType( null, 'default' );
+   $result->addValue( null, 'some key', 'some value' );
+
+   $printer = new ApiFormatRaw( $main );
+   $printer->initPrinter();
+   $printer->execute();
+   ob_start();
+   try {
+   $printer->closePrinter();
+   } catch ( Exception $ex ) {
+   throw $ex;
+   }
+   ob_end_clean();
+   }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/401405
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I63ce42dd61f6e47f7278c436cad1e4f05e287b04
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Phantom42 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits