aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/exception/MWExceptionTest.php
diff options
context:
space:
mode:
authoraddshore <addshorewiki@gmail.com>2014-02-24 21:13:49 +0100
committeraddshore <addshorewiki@gmail.com>2014-02-24 21:17:59 +0100
commit557af130868dacfb981e916baa4dcbcb866e16a1 (patch)
tree5f5508f797f1cb67bd1eae6fd84066e22dd91844 /tests/phpunit/includes/exception/MWExceptionTest.php
parent3b3a7766d3558e1ae66c69bd0b5ae56e7b747967 (diff)
downloadmediawikicore-557af130868dacfb981e916baa4dcbcb866e16a1.tar.gz
mediawikicore-557af130868dacfb981e916baa4dcbcb866e16a1.zip
Split Exception.php
Change-Id: I8273b342f8814887b65227457d0a461d7cd31e75
Diffstat (limited to 'tests/phpunit/includes/exception/MWExceptionTest.php')
-rw-r--r--tests/phpunit/includes/exception/MWExceptionTest.php115
1 files changed, 115 insertions, 0 deletions
diff --git a/tests/phpunit/includes/exception/MWExceptionTest.php b/tests/phpunit/includes/exception/MWExceptionTest.php
new file mode 100644
index 000000000000..5f001c7ea68a
--- /dev/null
+++ b/tests/phpunit/includes/exception/MWExceptionTest.php
@@ -0,0 +1,115 @@
+<?php
+/**
+ * @author Antoine Musso
+ * @copyright Copyright © 2013, Antoine Musso
+ * @copyright Copyright © 2013, Wikimedia Foundation Inc.
+ * @file
+ */
+
+class MWExceptionTest 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 );
+
+ }
+
+}