Evaluate, Test and Move the items und library/Icinga/Web to the source tree

Modify test for hook, add test for notification

refs #4256
This commit is contained in:
Marius Hein 2013-06-10 12:37:27 +02:00 committed by Jannis Moßhammer
parent 09302f6fb9
commit ccd5564a37
3 changed files with 222 additions and 16 deletions

View File

@ -1,4 +1,6 @@
<?php <?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web; namespace Icinga\Web;
@ -7,34 +9,78 @@ use Icinga\Application\Platform;
use Icinga\Application\Logger as Log; use Icinga\Application\Logger as Log;
use Zend_Session_Namespace as SessionNamespace; use Zend_Session_Namespace as SessionNamespace;
/**
* Class Notification
* @package Icinga\Web
*/
class Notification class Notification
{ {
protected static $instance; /**
protected $is_cli = false; * @var Notification
*/
private static $instance;
/**
* @var bool
*/
private $cliFlag = false;
/**
* @param boolean $cliFlag
*/
public function setCliFlag($cliFlag)
{
$this->cliFlag = $cliFlag;
}
/**
* @return boolean
*/
public function getCliFlag()
{
return $this->cliFlag;
}
/**
* @param $msg
*/
public static function info($msg) public static function info($msg)
{ {
self::getInstance()->addMessage($msg, 'info'); self::getInstance()->addMessage($msg, 'info');
} }
/**
* @param $msg
*/
public static function success($msg) public static function success($msg)
{ {
self::getInstance()->addMessage($msg, 'success'); self::getInstance()->addMessage($msg, 'success');
} }
/**
* @param $msg
*/
public static function warning($msg) public static function warning($msg)
{ {
self::getInstance()->addMessage($msg, 'warning'); self::getInstance()->addMessage($msg, 'warning');
} }
/**
* @param $msg
*/
public static function error($msg) public static function error($msg)
{ {
self::getInstance()->addMessage($msg, 'error'); self::getInstance()->addMessage($msg, 'error');
} }
protected function addMessage($message, $type = 'info') /**
* @param $message
* @param string $type
* @throws \Icinga\Exception\ProgrammingError
*/
public function addMessage($message, $type = 'info')
{ {
if (! in_array( if (!in_array(
$type, $type,
array( array(
'info', 'info',
@ -42,7 +88,8 @@ class Notification
'warning', 'warning',
'success' 'success'
) )
)) { )
) {
throw new ProgrammingError( throw new ProgrammingError(
sprintf( sprintf(
'"%s" is not a valid notification type', '"%s" is not a valid notification type',
@ -51,7 +98,7 @@ class Notification
); );
} }
if ($this->is_cli) { if ($this->cliFlag) {
$msg = sprintf('[%s] %s', $type, $message); $msg = sprintf('[%s] %s', $type, $message);
switch ($type) { switch ($type) {
case 'info': case 'info':
@ -68,8 +115,8 @@ class Notification
return; return;
} }
$mo = (object) array( $mo = (object)array(
'type' => $type, 'type' => $type,
'message' => $message, 'message' => $message,
); );
@ -79,11 +126,17 @@ class Notification
$this->session->messages = $msgs; $this->session->messages = $msgs;
} }
/**
* @return bool
*/
public function hasMessages() public function hasMessages()
{ {
return ! empty($this->session->messages); return !empty($this->session->messages);
} }
/**
* @return mixed
*/
public function getMessages() public function getMessages()
{ {
$msgs = $this->session->messages; $msgs = $this->session->messages;
@ -91,18 +144,24 @@ class Notification
return $msgs; return $msgs;
} }
/**
* Create a new Notification object
*/
final private function __construct() final private function __construct()
{ {
$this->session = new SessionNamespace('IcingaNotification'); $this->session = new SessionNamespace('IcingaNotification');
if (! is_array($this->session->messages)) { if (!is_array($this->session->messages)) {
$this->session->messages = array(); $this->session->messages = array();
} }
if (Platform::isCli()) { if (Platform::isCli()) {
$this->is_cli = true; $this->cliFlag = true;
} }
} }
/**
* @return Notification
*/
public static function getInstance() public static function getInstance()
{ {
if (self::$instance === null) { if (self::$instance === null) {

View File

@ -10,6 +10,9 @@ namespace Tests\Icinga\Web;
require_once("../../library/Icinga/Exception/ProgrammingError.php"); require_once("../../library/Icinga/Exception/ProgrammingError.php");
require_once("../../library/Icinga/Web/Hook.php"); require_once("../../library/Icinga/Web/Hook.php");
require_once("Zend/Log.php");
require_once("../../library/Icinga/Application/Logger.php");
use Icinga\Web\Hook as Hook; use Icinga\Web\Hook as Hook;
class Base class Base
{ {
@ -23,6 +26,13 @@ class TestBadHookImplementation
{ {
} }
class ErrorProneHookImplementation
{
public function __construct()
{
throw new \Exception("HOOK ERROR");
}
}
class HookTest extends \PHPUnit_Framework_TestCase class HookTest extends \PHPUnit_Framework_TestCase
{ {
@ -64,7 +74,7 @@ class HookTest extends \PHPUnit_Framework_TestCase
* *
* *
**/ **/
public function testCreateInvalidInstance() public function testCreateInvalidInstance1()
{ {
$this->setExpectedException('\Icinga\Exception\ProgrammingError'); $this->setExpectedException('\Icinga\Exception\ProgrammingError');
Hook::clean(); Hook::clean();
@ -74,6 +84,25 @@ class HookTest extends \PHPUnit_Framework_TestCase
Hook::clean(); Hook::clean();
} }
public function testCreateInvalidInstance2()
{
Hook::clean();
Hook::$BASE_NS = "Tests\\Icinga\\Web\\";
$test = Hook::createInstance("Base","NOTEXIST");
$this->assertNull($test);
Hook::clean();
}
public function testCreateInvalidInstance3()
{
Hook::clean();
Hook::$BASE_NS = "Tests\\Icinga\\Web\\";
Hook::register("Base","ErrorProne","Tests\\Icinga\\Web\\ErrorProneHookImplementation");
$test = Hook::createInstance("Base","ErrorProne");
$this->assertNull($test);
Hook::clean();
}
/** /**
* Test for Hook::All() * Test for Hook::All()
* Note: This method is static! * Note: This method is static!
@ -109,5 +138,4 @@ class HookTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf("Tests\\Icinga\\Web\\TestHookImplementation",Hook::first("Base")); $this->assertInstanceOf("Tests\\Icinga\\Web\\TestHookImplementation",Hook::first("Base"));
Hook::clean(); Hook::clean();
} }
} }

View File

@ -0,0 +1,119 @@
<?php
namespace Tests\Icinga\Web;
require_once "../../library/Icinga/Exception/ProgrammingError.php";
require_once "../../library/Icinga/Web/Notification.php";
require_once "../../library/Icinga/Application/Platform.php";
require_once "../../library/Icinga/Application/Logger.php";
require_once "Zend/Session/Namespace.php";
require_once "Zend/Config.php";
require_once "Zend/Log.php";
require_once "Zend/Session.php";
require_once "Zend/Log/Writer/Abstract.php";
require_once "Zend/Log/Writer/Stream.php";
use Icinga\Application\Logger;
use Icinga\Web\Notification;
class NotificationTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Logger
*/
private $logger = null;
/**
* @var string
*/
private $loggerPath = null;
protected function setUp()
{
\Zend_Session::$_unitTestEnabled = true;
$this->loggerPath = "/tmp/icinga2-web-notify-test";
$this->dropLog();
$logConfig = new \Zend_Config(array(
"debug" => array(
"enable" => 0,
"type"=>"mock",
"target"=>"target3"
),
"type" => "stream",
"target" => $this->loggerPath
));
$this->logger = new Logger($logConfig);
$this->notification = Notification::getInstance();
}
protected function dropLog()
{
if (file_exists($this->loggerPath)) {
unlink($this->loggerPath);
}
}
public function testAddMessage1()
{
$notify = Notification::getInstance();
$notify->setCliFlag(true);
$notify->error('OK1');
$notify->warning('OK2');
$notify->info('OK3');
$this->logger->flushQueue();
$content = file_get_contents($this->loggerPath);
$this->assertContains('[error] OK1', $content);
$this->assertContains('[warning] OK2', $content);
$this->assertNotContains('[info] OK3', $content);
$this->dropLog();
}
public function testAddMessage2()
{
$notify = Notification::getInstance();
$notify->setCliFlag(false);
$notify->success('test123');
$notify->error('test456');
$this->assertTrue($notify->hasMessages());
$messages = $notify->getMessages();
$this->assertInternalType('array', $messages);
$this->assertEquals('test123', $messages[0]->message);
$this->assertEquals('success', $messages[0]->type);
$this->assertEquals('test456', $messages[1]->message);
$this->assertEquals('error', $messages[1]->type);
}
/**
* @expectedException Icinga\Exception\ProgrammingError
* @expectedExceptionMessage "NOT_EXIST_123" is not a valid notification type
*/
public function testWrongType1()
{
$notify = Notification::getInstance();
$notify->addMessage('test', 'NOT_EXIST_123');
}
public function testSetterAndGetter1()
{
$notify = Notification::getInstance();
$notify->setCliFlag(true);
$this->assertTrue($notify->getCliFlag());
$notify->setCliFlag(false);
$this->assertFalse($notify->getCliFlag());
}
}