Hashar has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/92328


Change subject: tests for includes/exception.php
......................................................................

tests for includes/exception.php

Tests for MWExceptionHandler::jsonSerializeException introduced by Ori
Livneh in https://gerrit.wikimedia.org/r/#/c/76304

Tests result:

  OK (17 tests, 29 assertions)

Change-Id: I37f2a837e2d26bf9780e56edc7ec039e8e447525
---
A tests/phpunit/includes/ExceptionTest.php
1 file changed, 107 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/28/92328/1

diff --git a/tests/phpunit/includes/ExceptionTest.php 
b/tests/phpunit/includes/ExceptionTest.php
new file mode 100644
index 0000000..cff6471
--- /dev/null
+++ b/tests/phpunit/includes/ExceptionTest.php
@@ -0,0 +1,107 @@
+<?php
+/**
+ * Tests for includes/Exception.php.
+ *
+ * @author Antoine Musso
+ * @copyright Copyright © 2013, Antoine Musso
+ * @copyright Copyright © 2013, Wikimedia Foundation Inc.
+ * @file
+ */
+
+class ExceptionTest extends MediaWikiTestCase {
+
+       /**
+        * @expectedException MWException
+        */
+       function testMwexceptionThrowing() {
+               throw new MWException();
+       }
+
+       /**
+        * @covers MWExceptionHandler::jsonSerializeException
+        */
+       function testJsonSerializeException() {
+               $json = MWExceptionHandler::jsonSerializeException(
+                       new MWException()
+               );
+               $this->assertNotEquals( false, $json,
+                       "The exception should be JSON serializable, got false." 
);
+       }
+
+       /**
+        * Lame JSON schema validation.
+        *
+        * @covers MWExceptionHandler::jsonSerializeException
+        *
+        * @param $expectedKeyType String Type expected as returned by gettype()
+        * @param $exClass String An exception class (ie: Exception, 
MWException)
+        * @param $key String Name of the key to validate in the serialized JSON
+        * @dataProvider provideJsonSerializedKeys
+        */
+       function testJsonserializeexceptionKeyss($expectedKeyType, $exClass, 
$key) {
+
+               # Make sure we log a backtrace:
+               $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) 
);
+
+               $json = json_decode(
+                       MWExceptionHandler::jsonSerializeException( new 
$exClass())
+               );
+               $this->assertObjectHasAttribute( $key, $json,
+                       "JSON serialized exception is missing key $key"
+               );
+               $this->assertInternalType( $expectedKeyType, $json->$key,
+                       "JSON serialized key '$key' of type " . 
gettype($json->$key)
+                       . " should be of type $expectedKeyType"
+               );
+       }
+
+       /**
+        * Returns test cases: exception class, key name, gettype()
+        */
+       function provideJsonSerializedKeys() {
+               $testCases = array();
+               foreach( array( 'Exception', 'MWException' ) as $exClass ) {
+                       $exTests = array(
+                               array( 'string',  $exClass,  'id' ),
+                               array( 'string',  $exClass,  'file' ),
+                               array( 'integer', $exClass,  'line' ),
+                               array( 'string',  $exClass,  'message' ),
+                               array( 'boolean', $exClass,  'url' ),
+                               # Backtrace only enabled with 
wgLogExceptionBacktrace = true
+                               array( 'array',   $exClass,  'backtrace' ),
+                       );
+                       $testCases = array_merge($testCases, $exTests);
+               }
+               return $testCases;
+       }
+
+       /**
+        * Given wgLogExceptionBacktrace is true
+        * then serialized exception SHOULD have a backtrace
+        *
+        * @covers MWExceptionHandler::jsonSerializeException
+        */
+       function testJsonserializeexceptionBacktracingEnabled() {
+               $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) 
);
+               $json = json_decode(
+                       MWExceptionHandler::jsonSerializeException( new 
Exception() )
+               );
+               $this->assertObjectHasAttribute( 'backtrace', $json );
+       }
+
+       /**
+        * Given wgLogExceptionBacktrace is false
+        * then serialized exception SHOULD NOT have a backtrace
+        *
+        * @covers MWExceptionHandler::jsonSerializeException
+        */
+       function testJsonserializeexceptionBacktracingDisabled() {
+               $this->setMwGlobals( array( 'wgLogExceptionBacktrace' => false 
) );
+               $json = json_decode(
+                       MWExceptionHandler::jsonSerializeException( new 
Exception() )
+               );
+               $this->assertObjectNotHasAttribute( 'backtrace', $json );
+
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I37f2a837e2d26bf9780e56edc7ec039e8e447525
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Hashar <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to