jenkins-bot has submitted this change and it was merged.
Change subject: Tests for MWExceptionHandler::jsonSerializeException
......................................................................
Tests for MWExceptionHandler::jsonSerializeException
MWExceptionHandler::jsonSerializeException was introduced in Iacda90fb4. This
patch add tests that verify that JSON-serialized exceptions have the right set
of keys with the right types of values.
Change-Id: I37f2a837e2d26bf9780e56edc7ec039e8e447525
---
A tests/phpunit/includes/ExceptionTest.php
1 file changed, 118 insertions(+), 0 deletions(-)
Approvals:
Ori.livneh: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/phpunit/includes/ExceptionTest.php
b/tests/phpunit/includes/ExceptionTest.php
new file mode 100644
index 0000000..9e76045
--- /dev/null
+++ b/tests/phpunit/includes/ExceptionTest.php
@@ -0,0 +1,118 @@
+<?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();
+ }
+
+ /**
+ * Verify the exception classes are JSON serializabe.
+ *
+ * @covers MWExceptionHandler::jsonSerializeException
+ * @dataProvider provideExceptionClasses
+ */
+ function testJsonSerializeExceptions( $exception_class ) {
+ $json = MWExceptionHandler::jsonSerializeException(
+ new $exception_class()
+ );
+ $this->assertNotEquals( false, $json,
+ "The $exception_class exception should be JSON
serializable, got false." );
+ }
+
+ function provideExceptionClasses() {
+ return array(
+ array( 'Exception' ),
+ array( 'MWException' ),
+ );
+ }
+
+
+ /**
+ * 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 testJsonserializeexceptionKeys($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' has type " .
gettype($json->$key)
+ . " (expected: $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( 'null', $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: merged
Gerrit-Change-Id: I37f2a837e2d26bf9780e56edc7ec039e8e447525
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Hashar <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: Hashar <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits