Added a test suite for the Notification package. Coverage is about 90%
authorGunnar Wrobel <p@rdus.de>
Tue, 27 Oct 2009 16:47:49 +0000 (17:47 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 27 Oct 2009 16:47:49 +0000 (17:47 +0100)
and primarily excludes sections that rely heavily on global scope
(Horde_Registry, Horde_Nls, Horde_Auth, Horde_Alarm).

framework/Notification/package.xml
framework/Notification/test/Horde/Notification/AllTests.php [new file with mode: 0644]
framework/Notification/test/Horde/Notification/Autoload.php [new file with mode: 0644]
framework/Notification/test/Horde/Notification/Class/Notification/EventTest.php [new file with mode: 0644]
framework/Notification/test/Horde/Notification/Class/Notification/Listener/AudioTest.php [new file with mode: 0644]
framework/Notification/test/Horde/Notification/Class/Notification/Listener/JavascriptTest.php [new file with mode: 0644]
framework/Notification/test/Horde/Notification/Class/Notification/Listener/MobileTest.php [new file with mode: 0644]
framework/Notification/test/Horde/Notification/Class/Notification/Listener/StatusTest.php [new file with mode: 0644]
framework/Notification/test/Horde/Notification/Class/Notification/ListenerTest.php [new file with mode: 0644]
framework/Notification/test/Horde/Notification/Class/NotificationTest.php [new file with mode: 0644]
framework/Notification/test/Horde/Notification/phpunit.xml [new file with mode: 0644]

index 6ed8279..6522780 100644 (file)
@@ -24,7 +24,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Added Horde_Notification::replace().
+ <notes>* Added unit tests.
+ * Added Horde_Notification::replace().
  * Initial Horde 4 package.
  </notes>
  <contents>
@@ -44,6 +45,28 @@ http://pear.php.net/dtd/package-2.0.xsd">
      <file name="Notification.php" role="php" />
     </dir> <!-- /lib/Horde -->
    </dir> <!-- /lib -->
+   <dir name="test">
+    <dir name="Horde">
+     <dir name="Notification">
+      <file name="AllTests.php" role="test" />
+      <file name="Autoload.php" role="test" />
+      <dir name="Class">
+       <file name="NotificationTest.php" role="test" />
+       <dir name="Notification">
+        <file name="EventTest.php" role="test" />
+        <file name="ListenerTest.php" role="test" />
+        <dir name="Listener">
+         <file name="AudioTest.php" role="test" />
+         <file name="JavascriptTest.php" role="test" />
+         <file name="MobileTest.php" role="test" />
+         <file name="StatusTest.php" role="test" />
+        </dir> <!-- /test/Horde/Notification/Class/Notification/Listener -->
+       </dir> <!-- /test/Horde/Notification/Class/Notification -->
+      </dir> <!-- /test/Horde/Notification/Class -->
+      <file name="phpunit.xml" role="test" />
+     </dir> <!-- /test/Horde/Notification -->
+    </dir> <!-- /test/Horde -->
+   </dir> <!-- /test -->
   </dir> <!-- / -->
  </contents>
  <dependencies>
@@ -78,6 +101,16 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Notification/Event.php" as="Horde/Notification/Event.php" />
    <install name="lib/Horde/Notification/Listener.php" as="Horde/Notification/Listener.php" />
    <install name="lib/Horde/Notification.php" as="Horde/Notification.php" />
+   <install name="test/Horde/Notification/AllTests.php" as="Horde/Notification/AllTests.php" />
+   <install name="test/Horde/Notification/Autoload.php" as="Horde/Notification/Autoload.php" />
+   <install name="test/Horde/Notification/Class/NotificationTest.php" as="Horde/Notification/Class/NotificationTest.php" />
+   <install name="test/Horde/Notification/Class/Notification/EventTest.php" as="Horde/Notification/Class/Notification/EventTest.php" />
+   <install name="test/Horde/Notification/Class/Notification/ListenerTest.php" as="Horde/Notification/Class/Notification/ListenerTest.php" />
+   <install name="test/Horde/Notification/Class/Notification/Listener/AudioTest.php" as="Horde/Notification/Class/Notification/Listener/AudioTest.php" />
+   <install name="test/Horde/Notification/Class/Notification/Listener/JavascriptTest.php" as="Horde/Notification/Class/Notification/Listener/JavascriptTest.php" />
+   <install name="test/Horde/Notification/Class/Notification/Listener/MobileTest.php" as="Horde/Notification/Class/Notification/Listener/MobileTest.php" />
+   <install name="test/Horde/Notification/Class/Notification/Listener/StatusTest.php" as="Horde/Notification/Class/Notification/Listener/StatusTest.php" />
+   <install name="test/Horde/Notification/phpunit.xml" as="Horde/Notification/phpunit.xml" />
   </filelist>
  </phprelease>
  <changelog>
diff --git a/framework/Notification/test/Horde/Notification/AllTests.php b/framework/Notification/test/Horde/Notification/AllTests.php
new file mode 100644 (file)
index 0000000..3aaf09b
--- /dev/null
@@ -0,0 +1,85 @@
+<?php
+/**
+ * All tests for the Horde_Kolab_Session:: package.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+
+/**
+ * Define the main method
+ */
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Horde_Kolab_Session_AllTests::main');
+}
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/Autoload.php';
+
+/**
+ * Combine the tests for this package.
+ *
+ * Copyright 2007-2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+class Horde_Notification_AllTests
+{
+
+    /**
+     * Main entry point for running the suite.
+     *
+     * @return NULL
+     */
+    public static function main()
+    {
+        PHPUnit_TextUI_TestRunner::run(self::suite());
+    }
+
+    /**
+     * Collect the unit tests of this directory into a new suite.
+     *
+     * @return PHPUnit_Framework_TestSuite The test suite.
+     */
+    public static function suite()
+    {
+        $suite = new PHPUnit_Framework_TestSuite('Horde Framework - Notification');
+
+        $basedir    = dirname(__FILE__);
+        $baseregexp = preg_quote($basedir . DIRECTORY_SEPARATOR, '/');
+
+        foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($basedir)) as $file) {
+            if ($file->isFile() && preg_match('/Test.php$/', $file->getFilename())) {
+                $pathname = $file->getPathname();
+                require $pathname;
+
+                $class = str_replace(
+                    DIRECTORY_SEPARATOR, '_',
+                    preg_replace("/^$baseregexp(.*)\.php/", '\\1', $pathname)
+                );
+                $suite->addTestSuite('Horde_Notification_' . $class);
+            }
+        }
+
+        return $suite;
+    }
+
+}
+
+if (PHPUnit_MAIN_METHOD == 'Horde_Notification_AllTests::main') {
+    Horde_Notification_AllTests::main();
+}
diff --git a/framework/Notification/test/Horde/Notification/Autoload.php b/framework/Notification/test/Horde/Notification/Autoload.php
new file mode 100644 (file)
index 0000000..491bbee
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * Setup autoloading for the tests.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+
+if (!spl_autoload_functions()) {
+    spl_autoload_register(
+        create_function(
+            '$class', 
+            '$filename = str_replace(array(\'::\', \'_\'), \'/\', $class);'
+            . '$err_mask = E_ALL ^ E_WARNING;'
+            . '$oldErrorReporting = error_reporting($err_mask);'
+            . 'include "$filename.php";'
+            . 'error_reporting($oldErrorReporting);'
+        )
+    );
+}
+
+/** Catch strict standards */
+error_reporting(E_ALL | E_STRICT);
diff --git a/framework/Notification/test/Horde/Notification/Class/Notification/EventTest.php b/framework/Notification/test/Horde/Notification/Class/Notification/EventTest.php
new file mode 100644 (file)
index 0000000..a899fc7
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Test the basic event class.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/../../Autoload.php';
+
+/**
+ * Test the basic event class.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+class Horde_Notification_Class_Notification_EventTest extends PHPUnit_Framework_TestCase
+{
+    public function testMethodConstructHasPostconditionThatTheGivenMessageWasSavedIfItWasNotNull()
+    {
+        $event = new Horde_Notification_Event('test');
+        $this->assertEquals('test', $event->getMessage());
+    }
+
+    public function testMethodSetmessageHasPostconditionThatTheGivenMessageWasSaved()
+    {
+        $event = new Horde_Notification_Event();
+        $event->setMessage('test');
+        $this->assertEquals('test', $event->getMessage());
+    }
+
+    public function testMethodGetmessageHasResultStringTheStoredMessage()
+    {
+        $event = new Horde_Notification_Event();
+        $event->setMessage('test');
+        $this->assertEquals('test', $event->getMessage());
+    }
+
+    public function testMethodGetmessageHasResultStringEmptyIfNoMessageWasStored()
+    {
+        $event = new Horde_Notification_Event();
+        $this->assertEquals('', $event->getMessage());
+    }
+}
\ No newline at end of file
diff --git a/framework/Notification/test/Horde/Notification/Class/Notification/Listener/AudioTest.php b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/AudioTest.php
new file mode 100644 (file)
index 0000000..d0330ff
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+/**
+ * Test the audio listener class.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/../../../Autoload.php';
+
+/**
+ * Test the audio listener class.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+class Horde_Notification_Class_Notification_Listener_AudioTest extends PHPUnit_Extensions_OutputTestCase
+{
+    public function testMethodHandleHasResultBooleanTrueForAudioMessages()
+    {
+        $listener = new Horde_Notification_Listener_Audio();
+        $this->assertTrue($listener->handles('audio'));
+    }
+
+    public function testMethodGetnameHasResultStringAudio()
+    {
+        $listener = new Horde_Notification_Listener_Audio();
+        $this->assertEquals('audio', $listener->getName());
+    }
+
+    public function testMethodNotifyHasOutputEventMessage()
+    {
+        $listener = new Horde_Notification_Listener_Audio();
+        $event = new Horde_Notification_Event('test');
+        $messages = array(
+            array(
+                'class' => 'Horde_Notification_Event',
+                'event' => serialize($event)
+            )
+        );
+        $this->expectOutputString(
+            '<embed src="test" width="0" height="0" autostart="true" />'
+        );
+        $listener->notify($messages);
+    }
+}
diff --git a/framework/Notification/test/Horde/Notification/Class/Notification/Listener/JavascriptTest.php b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/JavascriptTest.php
new file mode 100644 (file)
index 0000000..b80a8e0
--- /dev/null
@@ -0,0 +1,105 @@
+<?php
+/**
+ * Test the javascript listener class.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/../../../Autoload.php';
+
+/**
+ * Test the javascript listener class.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+class Horde_Notification_Class_Notification_Listener_JavascriptTest extends PHPUnit_Extensions_OutputTestCase
+{
+    public function testMethodHandleHasResultBooleanTrueForjavascriptMessages()
+    {
+        $listener = new Horde_Notification_Listener_Javascript();
+        $this->assertTrue($listener->handles('javascript'));
+    }
+
+    public function testMethodGetnameHasResultStringJavascript()
+    {
+        $listener = new Horde_Notification_Listener_Javascript();
+        $this->assertEquals('javascript', $listener->getName());
+    }
+
+    public function testMethodNotifyHasNoOutputIfTheMessageStackIsEmpty()
+    {
+        $listener = new Horde_Notification_Listener_Javascript();
+        $messages = array();
+        $listener->notify($messages);
+    }
+
+    public function testMethodNotifyHasOutputEventMessageEmbeddedInScriptElement()
+    {
+        $listener = new Horde_Notification_Listener_Javascript();
+        $event = new Horde_Notification_Event('test');
+        $messages = array(
+            array(
+                'class' => 'Horde_Notification_Event',
+                'event' => serialize($event),
+                'type'  => 'javascript'
+            )
+        );
+        $this->expectOutputString(
+            '<script type="text/javascript">//<![CDATA['
+            . "\n" . 'test' . "\n" . '//]]></script>' . "\n"
+        );
+        $listener->notify($messages);
+    }
+
+    public function testMethodNotifyHasOutputEventMessageNotEmbeddedIfEmbeddingIsDeactivated()
+    {
+        $listener = new Horde_Notification_Listener_Javascript();
+        $event = new Horde_Notification_Event('test');
+        $messages = array(
+            array(
+                'class' => 'Horde_Notification_Event',
+                'event' => serialize($event),
+                'type'  => 'javascript'
+            )
+        );
+        $this->expectOutputString('test' . "\n");
+        $listener->notify($messages, array('noscript' => true));
+    }
+
+    public function testMethodNotifyHasOutputJavaScriptFileLinkIfTheEventContainedSuchAFileLink()
+    {
+        $listener = new Horde_Notification_Listener_Javascript();
+        $event = new Horde_Notification_Event('test');
+        $messages = array(
+            array(
+                'class' => 'Horde_Notification_Event',
+                'event' => serialize($event),
+                'type'  => 'javascript-file'
+            )
+        );
+        $this->expectOutputString(
+            '<script type="text/javascript">//<![CDATA['
+            . "\n" . '//]]></script>' . "\n" .
+            '<script type="text/javascript" src="test"></script>' . "\n"
+        );
+        $listener->notify($messages);
+    }
+}
diff --git a/framework/Notification/test/Horde/Notification/Class/Notification/Listener/MobileTest.php b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/MobileTest.php
new file mode 100644 (file)
index 0000000..7304948
--- /dev/null
@@ -0,0 +1,120 @@
+<?php
+/**
+ * Test the mobile listener class.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/../../../Autoload.php';
+
+/**
+ * Test the mobile listener class.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+class Horde_Notification_Class_Notification_Listener_MobileTest extends PHPUnit_Extensions_OutputTestCase
+{
+    public function setUp()
+    {
+        /** Loading Horde/Registry.php requires the PERMS_* constants */
+        require_once 'Horde/Perms.php';
+        /**
+         * The listener pulls the registry from global scope to get the image
+         * directory.
+         */
+        $GLOBALS['registry'] = $this->getMock(
+            'Horde_Registry', array(), array(), '', false, false
+        );
+
+        $this->mobile = $this->getMock(
+            'Horde_Mobile', array(), array(), '', false, false
+        );
+    }
+
+    public function testMethodHandleHasResultBooleanTrueForHordeMessages()
+    {
+        $listener = new Horde_Notification_Listener_Mobile();
+        $this->assertTrue($listener->handles('horde.message'));
+    }
+
+    public function testMethodGetnameHasResultStringStatus()
+    {
+        $listener = new Horde_Notification_Listener_Mobile();
+        $this->assertEquals('status', $listener->getName());
+    }
+
+    public function testMethodNotifyHasNoOutputIfTheMessageStackIsEmpty()
+    {
+        $listener = new Horde_Notification_Listener_Mobile();
+        $messages = array();
+        $listener->setMobileObject($this->mobile);
+        $listener->notify($messages);
+    }
+
+    public function testMethodNotifyHasSameOutputAsTheStatusListenerIfNoMobileObjectWasSet()
+    {
+        $this->markTestIncomplete('This is untestable without mocking half of the Horde framework.');
+        $listener = new Horde_Notification_Listener_Mobile();
+        $event = new Horde_Notification_Event('test');
+        $messages = array(
+            array(
+                'class' => 'Horde_Notification_Event',
+                'event' => serialize($event),
+                'type'  => 'horde.message'
+            )
+        );
+        $this->expectOutputString(
+            '<ul class="notices"><li>test</li></ul>'
+        );
+        $listener->notify($messages);
+    }
+
+    public function testMethodNotifyHasPostconditionThatTheMobileObjectReceivedTheNotifications()
+    {
+        $element = $this->getMock('Horde_Mobile_element');
+        $this->mobile->expects($this->exactly(2))
+            ->method('add')
+            ->with(
+                $this->logicalOr(
+                    $this->logicalAnd(
+                        $this->isInstanceOf('Horde_Mobile_Text'),
+                        $this->attributeEqualTo('_text', 'MSG: test')
+                    ),
+                    $this->logicalAnd(
+                        $this->isInstanceOf('Horde_Mobile_Text'),
+                        $this->attributeEqualTo('_text', "\n")
+                    )
+                )
+            )
+            ->will($this->returnValue($element));
+        $listener = new Horde_Notification_Listener_Mobile();
+        $listener->setMobileObject($this->mobile);
+        $event = new Horde_Notification_Event('test');
+        $messages = array(
+            array(
+                'class' => 'Horde_Notification_Event',
+                'event' => serialize($event),
+                'type'  => 'horde.message',
+            )
+        );
+        $listener->notify($messages);
+    }
+}
diff --git a/framework/Notification/test/Horde/Notification/Class/Notification/Listener/StatusTest.php b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/StatusTest.php
new file mode 100644 (file)
index 0000000..5ce9c10
--- /dev/null
@@ -0,0 +1,126 @@
+<?php
+/**
+ * Test the status listener class.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/../../../Autoload.php';
+
+/**
+ * Test the status listener class.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+class Horde_Notification_Class_Notification_Listener_StatusTest extends PHPUnit_Extensions_OutputTestCase
+{
+    public function setUp()
+    {
+        /** Loading Horde/Registry.php requires the PERMS_* constants */
+        require_once 'Horde/Perms.php';
+        /**
+         * The listener pulls the registry from global scope to get the image
+         * directory.
+         */
+        $GLOBALS['registry'] = $this->getMock(
+            'Horde_Registry', array(), array(), '', false, false
+        );
+    }
+
+    public function testMethodHandleHasResultBooleanTrueForHordeMessages()
+    {
+        $listener = new Horde_Notification_Listener_Status();
+        $this->assertTrue($listener->handles('horde.message'));
+    }
+
+    public function testMethodGetnameHasResultStringStatus()
+    {
+        $listener = new Horde_Notification_Listener_Status();
+        $this->assertEquals('status', $listener->getName());
+    }
+
+    public function testMethodNotifyHasNoOutputIfTheMessageStackIsEmpty()
+    {
+        $listener = new Horde_Notification_Listener_Status();
+        $messages = array();
+        $listener->notify($messages);
+    }
+
+    public function testMethodNotifyHasOutputEventMessagesEmbeddedInUlElement()
+    {
+        $this->markTestIncomplete('This is untestable without mocking half of the Horde framework.');
+        $listener = new Horde_Notification_Listener_Status();
+        $event = new Horde_Notification_Event('test');
+        $messages = array(
+            array(
+                'class' => 'Horde_Notification_Event',
+                'event' => serialize($event),
+                'type'  => 'horde.message'
+            )
+        );
+        $this->expectOutputString(
+            '<ul class="notices"><li>test</li></ul>'
+        );
+        $listener->notify($messages);
+    }
+
+    public function testMethodGetstackHasNoOutputIfNotifyWasAskedToAvoidDirectOutput()
+    {
+        $listener = new Horde_Notification_Listener_Status();
+        $event = new Horde_Notification_Event('test');
+        $flags = array('content.raw' => true);
+        $messages = array(
+            array(
+                'class' => 'Horde_Notification_Event',
+                'event' => serialize($event),
+                'type'  => 'horde.message',
+                'flags' => serialize($flags)
+            )
+        );
+        $listener->notify($messages, array('store' => true));
+        $this->expectOutputString('');
+    }
+
+    public function testMethodGetstackHasOutputEventMessagesIfNotifyWasAskedToAvoidDirectOutput()
+    {
+        $listener = new Horde_Notification_Listener_Status();
+        $event = new Horde_Notification_Event('test');
+        $flags = array('content.raw' => true);
+        $messages = array(
+            array(
+                'class' => 'Horde_Notification_Event',
+                'event' => serialize($event),
+                'type'  => 'horde.message',
+                'flags' => serialize($flags)
+            )
+        );
+        $listener->notify($messages, array('store' => true));
+        $this->assertEquals(
+            array(
+                array(
+                    'message' => 'test',
+                    'type' => 'horde.message'
+                )
+            ),
+            $listener->getStack()
+        );
+    }
+}
diff --git a/framework/Notification/test/Horde/Notification/Class/Notification/ListenerTest.php b/framework/Notification/test/Horde/Notification/Class/Notification/ListenerTest.php
new file mode 100644 (file)
index 0000000..5887f5f
--- /dev/null
@@ -0,0 +1,225 @@
+<?php
+/**
+ * Test the basic listener class.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/../../Autoload.php';
+
+/**
+ * Test the basic listener class.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+class Horde_Notification_Class_Notification_ListenerTest extends PHPUnit_Framework_TestCase
+{
+    public function setUp()
+    {
+        @include_once 'PEAR.php';
+        if (!class_exists('PEAR_Error')) {
+            $this->markTestSkipped('The PEAR_Error class is not available!');
+        }
+    }
+
+    public function testMethodHandleHasResultBooleanFalse()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $this->assertFalse($listener->handles('test'));
+    }
+
+    public function testMethodGetnameHasResultStringTheNameOfTheListener()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $this->assertEquals('mock', $listener->getName());
+    }
+
+    public function testMethodGeteventHasResultNotificationeventTheUnserializedMessageEvent()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $event = new Horde_Notification_Event('test');
+        $message = array(
+            'class' => 'Horde_Notification_Event',
+            'event' => serialize($event)
+        );
+        $this->assertType(
+            'Horde_Notification_Event',
+            $listener->getEvent($message)
+        );
+    }
+
+    public function testMethodGeteventHasResultNotificationeventTheUnserializedMessageEventIfTheClassInformationInTheMessageIsInvalid()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $event = new Horde_Notification_Event('test');
+        $message = array(
+            'class' => 'Does_Not_Exist',
+            'event' => serialize($event)
+        );
+        $this->assertType(
+            'Horde_Notification_Event',
+            $listener->getEvent($message)
+        );
+    }
+
+    public function testMethodGeteventHasResultNotificationeventTheUnserializedMessageIfTheUnserializedObjectHasAnAttributeMessage()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $event = new stdClass;
+        $event->_message = 'test';
+        $message = array(
+            'class' => '',
+            'event' => serialize($event)
+        );
+        $this->assertType(
+            'Horde_Notification_Event',
+            $listener->getEvent($message)
+        );
+    }
+
+    public function testMethodGeteventHasResultPearerrorIfTheMessageCouldNotBeUnserialized()
+    {
+        $this->markTestIncomplete('Fails because of strict standards (PEAR::raiseError()).');
+        $listener = new Horde_Notification_Listener_Mock();
+        $message = array(
+            'class' => '',
+            'event' => 'unserializable'
+        );
+        $this->assertType(
+            'PEAR_Error',
+            $listener->getEvent($message)
+        );
+    }
+
+    public function testMethodGeteventHasResultPearerrorIfTheMessageContainedAPearerror()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $event = new PEAR_Error();
+        $message = array(
+            'class' => '',
+            'event' => serialize($event)
+        );
+        $this->assertType(
+            'PEAR_Error',
+            $listener->getEvent($message)
+        );
+    }
+
+    public function testMethodGeteventHasResultPearerrorWithHiddenAttributeMessageIfTheMessageContainedAPearerrorWithUserInfo()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $event = new PEAR_Error('message', null, null, null, 'test');
+        $message = array(
+            'class' => '',
+            'event' => serialize($event)
+        );
+        $result = $listener->getEvent($message);
+        $this->assertEquals('message : test', $result->_message);
+    }
+
+    public function testMethodGeteventHasResultPearerrorWithHiddenAttributeMessageComposedOfArrayElementsIfTheMessageContainedAPearerrorWithAnArrayOfUserInfo()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $user_info = array('1', '2');
+        $event = new PEAR_Error('message', null, null, null, $user_info);
+        $message = array(
+            'class' => '',
+            'event' => serialize($event)
+        );
+        $result = $listener->getEvent($message);
+        $this->assertEquals('message : 1, 2', $result->_message);
+    }
+
+    public function testMethodGeteventHasResultPearerrorWithHiddenAttributeMessageComposedOfArrayElementsIfTheMessageContainedAPearerrorWithAnArrayOfUserInfoErrors()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $user_info = array(new PEAR_Error('a'), new PEAR_Error('b'));
+        $event = new PEAR_Error('message', null, null, null, $user_info);
+        $message = array(
+            'class' => '',
+            'event' => serialize($event)
+        );
+        $result = $listener->getEvent($message);
+        $this->assertEquals('message : a, b', $result->_message);
+    }
+
+    public function testMethodGeteventHasResultPearerrorWithHiddenAttributeMessageComposedOfArrayElementsIfTheMessageContainedAPearerrorWithAnArrayOfUserInfoObjectThatImplementGetmessageButNotTostring()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $user_info = array(new Message('a'), new Message('b'));
+        $event = new PEAR_Error('message', null, null, null, $user_info);
+        $message = array(
+            'class' => '',
+            'event' => serialize($event)
+        );
+        $result = $listener->getEvent($message);
+        $this->assertEquals('message : a, b', $result->_message);
+    }
+
+    public function testMethodGetflagsHasResultArrayEmptyIfTheGivenMessageHasNoFlags()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $message = array();
+        $this->assertEquals(array(), $listener->getFlags($message));
+    }
+
+    public function testMethodGetflagsHasResultArrayEmptyIfTheFlagsCouldNotBeUnserialized()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $message = array('flags' => 'unserializable');
+        $this->assertEquals(array(), $listener->getFlags($message));
+    }
+
+    public function testMethodGetflagsHasResultArrayMessageFlags()
+    {
+        $listener = new Horde_Notification_Listener_Mock();
+        $message = array('flags' => serialize(array('a' => 'a')));
+        $this->assertEquals(array('a' => 'a'), $listener->getFlags($message));
+    }
+}
+
+class Horde_Notification_Listener_Mock extends Horde_Notification_Listener
+{
+    protected $_name = 'mock';
+
+    public function notify(&$messageStacks, $options = array())
+    {
+    }
+
+    public function getMessage($message, $options = array())
+    {
+    }
+}
+
+class Message
+{
+    private $_message;
+
+    public function __construct($message)
+    {
+        $this->_message = $message;
+    }
+
+    public function getMessage()
+    {
+        return $this->_message;
+    }
+}
diff --git a/framework/Notification/test/Horde/Notification/Class/NotificationTest.php b/framework/Notification/test/Horde/Notification/Class/NotificationTest.php
new file mode 100644 (file)
index 0000000..f8d9fef
--- /dev/null
@@ -0,0 +1,254 @@
+<?php
+/**
+ * Test the notification class.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/../Autoload.php';
+
+/**
+ * Test the notification class.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+
+class Horde_Notification_Class_NotificationTest extends PHPUnit_Framework_TestCase
+{
+    public function setUp()
+    {
+        @include_once 'Log.php';
+        if (!defined('PEAR_LOG_DEBUG')) {
+            $this->markTestSkipped('The PEAR_LOG_DEBUG constant is not available!');
+        }
+    }
+
+    public function testMethodSingletonAlwaysReturnsTheSameInstanceForTheSameStackName()
+    {
+        $notification1 = Horde_Notification::singleton('test');
+        $notification2 = Horde_Notification::singleton('test');
+        $this->assertSame($notification1, $notification2);
+    }
+
+    public function testMethodConstructHasPostconditionThatTheSessionStackGotInitializedAsArray()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $this->assertEquals(array(), $_SESSION['test']);
+    }
+
+    public function testMethodAttachHasResultNotificationlistener()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $this->assertType(
+            'Horde_Notification_Listener_Audio',
+            $notification->attach('audio')
+        );
+    }
+
+    public function testMethodAttachHasResultNotificationlistenerClassAsSpecifiedInParameterClass()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $this->assertType(
+            'Horde_Notification_Listener_Audio',
+            $notification->attach(
+                'MyAudio', array(), 'Horde_Notification_Listener_Audio'
+            )
+        );
+    }
+
+    public function testMethodAttachHasPostconditionThatTheListenerGotInitializedWithTheProvidedParmeters()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $listener = $notification->attach('dummy', array('test'));
+        $this->assertEquals(array('test'), $listener->params);
+    }
+
+    public function testMethodAttachHasPostconditionThatTheListenerStackGotInitializedAsArray()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $notification->attach('audio');
+        $this->assertEquals(array(), $_SESSION['test']['audio']);
+    }
+
+    public function testMethodAttachThrowsExceptionIfTheListenerTypeIsUnkown()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        try {
+            $notification->attach('MyAudio');
+            $this->fail('No exception!');
+        } catch (Horde_Exception $e) {
+            $this->assertEquals(
+                'Notification listener Horde_Notification_Listener_Myaudio not found.',
+                $e->getMessage()
+            );
+        }
+    }
+
+    public function testMethodReplaceHasResultNotificationlistener()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $notification->attach(
+            'test', array(), 'Horde_Notification_Listener_Audio'
+        );
+        $this->assertType(
+            'Horde_Notification_Listener_Dummy',
+            $notification->replace(
+                'test', array(), 'Horde_Notification_Listener_Dummy'
+            )
+        );
+    }
+
+    public function testMethodDetachHasPostconditionThatTheListenerStackGotUnset()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $notification->attach('audio');
+        $notification->detach('audio');
+        $this->assertFalse(isset($_SESSION['test']['audio']));
+    }
+
+    public function testMethodDetachThrowsExceptionIfTheListenerIsUnset()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        try {
+            $notification->detach('MyAudio');
+            $this->fail('No exception!');
+        } catch (Horde_Exception $e) {
+            $this->assertEquals(
+                'Notification listener myaudio not found.',
+                $e->getMessage()
+            );
+        }
+    }
+
+    public function testMethodPushHasPostconditionThatTheEventGotSavedInAllAttachedListenerStacksHandlingTheEvent()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $event = new Horde_Notification_Event('test');
+        $flags= array();
+        $notification->attach('audio');
+        $notification->push('test', 'audio');
+        $result = array_shift($_SESSION['test']['audio']);
+        $this->assertEquals('Horde_Notification_Event', $result['class']);
+        $this->assertEquals(serialize($event), $result['event']);
+        $this->assertEquals(serialize($flags), $result['flags']);
+        $this->assertEquals('audio', $result['type']);
+    }
+
+    public function testMethodPushHasPostconditionThatAnExceptionGetsMarkedAsTypeErrorIfTheTypeWasUnset()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $notification->attach('dummy');
+        $notification->push(new Exception('test'));
+        $result = array_shift($_SESSION['test']['dummy']);
+        $this->assertEquals('horde.error', $result['type']);
+    }
+
+    public function testMethodPushHasPostconditionThatEventsWithoutTypeGetMarkedAsTypeMessage()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $notification->attach('dummy');
+        $notification->push('test');
+        $result = array_shift($_SESSION['test']['dummy']);
+        $this->assertEquals('horde.message', $result['type']);
+    }
+
+    public function testMethodNotifyHasPostconditionThatAllListenersWereNotified()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $event = new Horde_Notification_Event('test');
+        $dummy = $notification->attach('dummy');
+        $flags= array();
+        $notification->push('test');
+        $notification->notify();
+        $result = array_shift($dummy->notifications);
+        $this->assertEquals('Horde_Notification_Event', $result['class']);
+        $this->assertEquals(serialize($event), $result['event']);
+        $this->assertEquals(serialize($flags), $result['flags']);
+        $this->assertEquals('horde.message', $result['type']);
+    }
+
+    public function testMethodNotifyHasPostconditionThatTheSpecifiedListenersWereNotified()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $event = new Horde_Notification_Event('test');
+        $dummy = $notification->attach('dummy');
+        $flags= array();
+        $notification->push('test');
+        $notification->notify(array('listeners' => 'dummy'));
+        $result = array_shift($dummy->notifications);
+        $this->assertEquals(serialize($event), $result['event']);
+    }
+
+    public function testMethodCountHasResultTheTotalNumberOfEventsInTheStack()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $notification->attach('audio');
+        $notification->attach('dummy');
+        $notification->push('test', 'audio');
+        $this->assertEquals(2, $notification->count());
+    }
+
+    public function testMethodCountHasResultTheEventNumberForASpecificListenerIfTheListenerHasBeenSpecified()
+    {
+        $notification = Horde_Notification_Instance::newInstance('test');
+        $notification->attach('audio');
+        $notification->attach('dummy');
+        $notification->push('test', 'audio');
+        $this->assertEquals(1, $notification->count('audio'));
+    }
+
+}
+
+class Horde_Notification_Instance extends Horde_Notification
+{
+    static public function newInstance($stack)
+    {
+        $instance = new Horde_Notification($stack);
+        return $instance;
+    }
+}
+
+class Horde_Notification_Listener_Dummy extends Horde_Notification_Listener
+{
+    public $params;
+
+    public $notifications;
+
+    public function __construct($params)
+    {
+        $this->params = $params;
+        $this->_name = 'dummy';
+        $this->_handles = array(
+            'audio' => '',
+            'horde.error' => '',
+            'horde.message' => '',
+        );
+    }
+
+    public function notify(&$messageStacks, $options = array())
+    {
+        $this->notifications = $messageStacks;
+    }
+
+    public function getMessage($message, $options = array())
+    {
+    }
+}
\ No newline at end of file
diff --git a/framework/Notification/test/Horde/Notification/phpunit.xml b/framework/Notification/test/Horde/Notification/phpunit.xml
new file mode 100644 (file)
index 0000000..502d3c9
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit>
+  <filter>
+    <whitelist>
+      <directory suffix=".php">../../../lib</directory>
+    </whitelist>
+  </filter>
+</phpunit>