Browse Source

Class_Grapher: accept multiple paths in demo. Logger improvements.

- debugLevel() method replaces debugLevel property.
- PHPUnit tests for Logger.
Frederic G. MARAND 12 years ago
parent
commit
1e30ed1687

+ 29 - 4
OSInet/Class_Grapher/Logger.php

@@ -7,10 +7,35 @@
 namespace OSInet\Class_Grapher;
 
 class Logger {
-  var $debugLevel;
+  public $fDebugLevel;
 
-  public function __construct($debugLevel) {
-    $this->debugLevel = $debugLevel;
+  public function __construct($debugLevel = LOG_WARNING) {
+    $this->debugLevel($debugLevel);
+  }
+
+  /**
+   * Get or Set the current debug level.
+   *
+   * @param int $level
+   *   LOG_EMERG to LOG_DEBUG. If NULL, do not set it.
+   *
+   * @return
+   *   The value of the debug level before this function was called.
+   */
+  public function debugLevel($level = NULL) {
+    $ret = $this->fDebugLevel;
+
+    if (isset($level)) {
+      if ($level < LOG_EMERG) {
+        $level = LOG_EMERG;
+      }
+      if ($level > LOG_DEBUG) {
+        $level = LOG_DEBUG;
+      }
+      $this->fDebugLevel = $level;
+    }
+
+    return $ret;
   }
 
   /**
@@ -23,7 +48,7 @@ class Logger {
    *   syslog constants in PHP.
    */
   public function debug($message = "\n", $level = LOG_INFO) {
-    if ($level <= $this->debugLevel) {
+    if ($level <= $this->fDebugLevel) {
       fputs(STDERR, $message);
       fflush(STDERR);
     }

+ 68 - 0
OSInet/Class_Grapher/Tests/LoggerTest.php

@@ -0,0 +1,68 @@
+<?php
+namespace OSInet\Class_Grapher;
+
+require_once 'OSInet/Class_Grapher/Logger.php';
+require_once 'PHPUnit/Framework/TestCase.php';
+
+/**
+ * Logger test case.
+ */
+class LoggerTest extends \PHPUnit_Framework_TestCase {
+
+  /**
+   *
+   * @var Logger
+   */
+  private $Logger;
+
+  /**
+   * Prepares the environment before running a test.
+   */
+  protected function setUp() {
+    parent::setUp();
+    $this->Logger = new Logger();
+  }
+
+  /**
+   * Cleans up the environment after running a test.
+   */
+  protected function tearDown() {
+    $this->Logger = null;
+    parent::tearDown();
+  }
+
+  /**
+   * Tests Logger->__construct()
+   */
+  public function test__construct() {
+    $this->Logger->__construct(LOG_DEBUG);
+    $this->assertEquals($this->Logger->debugLevel(), LOG_DEBUG);
+
+    $this->Logger->__construct(LOG_EMERG - 1);
+    $this->assertEquals($this->Logger->debugLevel(), LOG_EMERG);
+
+    $this->Logger->__construct(LOG_DEBUG + 1);
+    $this->assertEquals($this->Logger->debugLevel(), LOG_DEBUG);
+
+    $this->Logger->__construct();
+    $this->assertEquals($this->Logger->debugLevel(), LOG_WARNING);
+  }
+
+  public function testDebugLevel() {
+    $this->assertEquals(LOG_WARNING, $this->Logger->debugLevel());
+
+    $level = $this->Logger->debugLevel(LOG_EMERG);
+    $this->assertEquals(LOG_WARNING, $level);
+
+    $level = $this->Logger->debugLevel();
+    $this->assertEquals(LOG_EMERG, $level);
+  }
+
+  /**
+   * Tests Logger->debug()
+   */
+  public function testDebug() {
+    $this->markTestSkipped("STDERR capture not available. Cannot test.");
+  }
+}
+

+ 3 - 1
apps/class_grapher/class_grapher_demo.php

@@ -41,7 +41,9 @@ foreach ($gpFiles as $file) {
 spl_autoload_register('psr0_autoload', TRUE);
 
 // Suggestion: look at Drupal 8 core/lib/Drupal/Core/Database directory
-$base = $argv[1];
+$base = $argv;
+// Ignore script itself.
+array_shift($base);
 
 $logger = new Logger(LOG_DEBUG);
 $imager = 'dot';