some tests for MWDebug
authorAntoine Musso <hashar@users.mediawiki.org>
Fri, 13 Jan 2012 23:07:52 +0000 (23:07 +0000)
committerAntoine Musso <hashar@users.mediawiki.org>
Fri, 13 Jan 2012 23:07:52 +0000 (23:07 +0000)
Really incomplete. We need better testing.

Added two new public methods so we can get or clear the internal logs.

includes/debug/Debug.php
tests/phpunit/includes/debug/MWDebugTest.php [new file with mode: 0644]

index 0107459..d56b65b 100644 (file)
@@ -84,6 +84,21 @@ class MWDebug {
                );
        }
 
+       /**
+        * Returns internal log array
+        */
+       public static function getLog() {
+               return self::$log;
+       }
+
+       /**
+        * Clears internal log array and deprecation tracking
+        */
+       public static function clearLog() {
+               self::$log = array();
+               self::$deprecationWarnings = array();
+       }
+
        /**
         * Adds a warning entry to the log
         *
@@ -274,4 +289,4 @@ class MWDebug {
 
                return $html;
        }
-}
\ No newline at end of file
+}
diff --git a/tests/phpunit/includes/debug/MWDebugTest.php b/tests/phpunit/includes/debug/MWDebugTest.php
new file mode 100644 (file)
index 0000000..e36e42f
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+
+class MWDebugTest extends MediaWikiTestCase {
+
+       function tearDown() {
+               /** Clear log before each test */
+               MWDebug::clearLog();
+       }
+
+       function testAddLog() {
+               MWDebug::log( 'logging a string' );
+               $this->assertEquals( array( array(
+                       'msg' => 'logging a string',
+                       'type' => 'log',
+                       'caller' => __METHOD__ ,
+                       ) ),
+                       MWDebug::getLog()
+               );
+       }
+
+       function testAddWarning() {
+               MWDebug::warning( 'Warning message' );
+               $this->assertEquals( array( array(
+                       'msg' => 'Warning message',
+                       'type' => 'warn',
+                       'caller' => 'MWDebug::warning',
+                       ) ),
+                       MWDebug::getLog()
+               );
+       }
+
+       function testAvoidDuplicateDeprecations() {
+               MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
+               MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
+
+               $this->assertCount( 1,
+                       MWDebug::getLog(),
+                       "Only one deprecated warning per function should be kept"
+               );
+       }
+
+       function testAvoidNonConsecutivesDuplicateDeprecations() {
+               MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
+               MWDebug::warning( 'some warning' );
+               MWDebug::log( 'we could have logged something too' );
+               // Another deprecation
+               MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
+
+               $this->assertCount( 3,
+                       MWDebug::getLog(),
+                       "Only one deprecated warning per function should be kept"
+               );
+       }
+}