From: Gunnar Wrobel
Date: Tue, 27 Oct 2009 16:47:49 +0000 (+0100)
Subject: Added a test suite for the Notification package. Coverage is about 90%
X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=b79ad0b268c3cddb46fec53529feda2e1d128c87;p=horde.git
Added a test suite for the Notification package. Coverage is about 90%
and primarily excludes sections that rely heavily on global scope
(Horde_Registry, Horde_Nls, Horde_Auth, Horde_Alarm).
---
diff --git a/framework/Notification/package.xml b/framework/Notification/package.xml
index 6ed82799a..65227808b 100644
--- a/framework/Notification/package.xml
+++ b/framework/Notification/package.xml
@@ -24,7 +24,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
beta
LGPL
- * Added Horde_Notification::replace().
+ * Added unit tests.
+ * Added Horde_Notification::replace().
* Initial Horde 4 package.
@@ -44,6 +45,28 @@ http://pear.php.net/dtd/package-2.0.xsd">
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -78,6 +101,16 @@ http://pear.php.net/dtd/package-2.0.xsd">
+
+
+
+
+
+
+
+
+
+
diff --git a/framework/Notification/test/Horde/Notification/AllTests.php b/framework/Notification/test/Horde/Notification/AllTests.php
new file mode 100644
index 000000000..3aaf09bc6
--- /dev/null
+++ b/framework/Notification/test/Horde/Notification/AllTests.php
@@ -0,0 +1,85 @@
+
+ * @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
+ * @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
index 000000000..491bbee4b
--- /dev/null
+++ b/framework/Notification/test/Horde/Notification/Autoload.php
@@ -0,0 +1,28 @@
+
+ * @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
index 000000000..a899fc7bb
--- /dev/null
+++ b/framework/Notification/test/Horde/Notification/Class/Notification/EventTest.php
@@ -0,0 +1,60 @@
+
+ * @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
+ * @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
index 000000000..d0330ff76
--- /dev/null
+++ b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/AudioTest.php
@@ -0,0 +1,62 @@
+
+ * @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
+ * @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(
+ ''
+ );
+ $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
index 000000000..b80a8e00d
--- /dev/null
+++ b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/JavascriptTest.php
@@ -0,0 +1,105 @@
+
+ * @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
+ * @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(
+ '' . "\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(
+ '' . "\n" .
+ '' . "\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
index 000000000..730494824
--- /dev/null
+++ b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/MobileTest.php
@@ -0,0 +1,120 @@
+
+ * @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
+ * @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(
+ ''
+ );
+ $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
index 000000000..5ce9c101f
--- /dev/null
+++ b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/StatusTest.php
@@ -0,0 +1,126 @@
+
+ * @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
+ * @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(
+ ''
+ );
+ $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
index 000000000..5887f5f02
--- /dev/null
+++ b/framework/Notification/test/Horde/Notification/Class/Notification/ListenerTest.php
@@ -0,0 +1,225 @@
+
+ * @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
+ * @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
index 000000000..f8d9fefd3
--- /dev/null
+++ b/framework/Notification/test/Horde/Notification/Class/NotificationTest.php
@@ -0,0 +1,254 @@
+
+ * @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
+ * @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
index 000000000..502d3c9b8
--- /dev/null
+++ b/framework/Notification/test/Horde/Notification/phpunit.xml
@@ -0,0 +1,8 @@
+
+
+
+
+ ../../../lib
+
+
+