2013-06-03 15:34:57 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Icinga\Web;
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Test class for Hook
|
|
|
|
* Created Fri, 22 Mar 2013 09:44:40 +0000
|
|
|
|
*
|
|
|
|
**/
|
2013-06-03 17:02:08 +02:00
|
|
|
require_once("../../library/Icinga/Exception/ProgrammingError.php");
|
|
|
|
require_once("../../library/Icinga/Web/Hook.php");
|
2013-06-03 15:34:57 +02:00
|
|
|
|
2013-06-10 12:37:27 +02:00
|
|
|
require_once("Zend/Log.php");
|
|
|
|
require_once("../../library/Icinga/Application/Logger.php");
|
|
|
|
|
2013-06-03 15:34:57 +02:00
|
|
|
use Icinga\Web\Hook as Hook;
|
|
|
|
class Base
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestHookImplementation extends Base
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestBadHookImplementation
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-10 12:37:27 +02:00
|
|
|
class ErrorProneHookImplementation
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
throw new \Exception("HOOK ERROR");
|
|
|
|
}
|
|
|
|
}
|
2013-06-03 15:34:57 +02:00
|
|
|
|
|
|
|
class HookTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for Hook::Has()
|
|
|
|
* Note: This method is static!
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
public function testHas()
|
|
|
|
{
|
|
|
|
Hook::clean();
|
|
|
|
$this->assertFalse(Hook::has("a"));
|
|
|
|
$this->assertFalse(Hook::has("a","b"));
|
|
|
|
|
|
|
|
Hook::register("a","b","c");
|
|
|
|
$this->assertTrue(Hook::has("a"));
|
|
|
|
$this->assertTrue(Hook::has("a","b"));
|
|
|
|
Hook::clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for Hook::CreateInstance()
|
|
|
|
* Note: This method is static!
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
public function testCreateInstance()
|
|
|
|
{
|
|
|
|
Hook::clean();
|
|
|
|
Hook::$BASE_NS = "Tests\\Icinga\\Web\\";
|
|
|
|
Hook::register("Base","b","Tests\\Icinga\\Web\\TestHookImplementation");
|
|
|
|
$this->assertInstanceOf("Tests\\Icinga\\Web\\TestHookImplementation",Hook::createInstance("Base","b"));
|
|
|
|
Hook::clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for Hook::CreateInstance()
|
|
|
|
* Note: This method is static!
|
|
|
|
*
|
|
|
|
*
|
|
|
|
**/
|
2013-06-10 12:37:27 +02:00
|
|
|
public function testCreateInvalidInstance1()
|
2013-06-03 15:34:57 +02:00
|
|
|
{
|
|
|
|
$this->setExpectedException('\Icinga\Exception\ProgrammingError');
|
|
|
|
Hook::clean();
|
|
|
|
Hook::$BASE_NS = "Tests\\Icinga\\Web\\";
|
|
|
|
Hook::register("Base","b","Tests\\Icinga\\Web\\TestBadHookImplementation");
|
|
|
|
Hook::createInstance("Base","b");
|
|
|
|
Hook::clean();
|
|
|
|
}
|
|
|
|
|
2013-06-10 12:37:27 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-06-03 15:34:57 +02:00
|
|
|
/**
|
|
|
|
* Test for Hook::All()
|
|
|
|
* Note: This method is static!
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
public function testAll()
|
|
|
|
{
|
|
|
|
Hook::clean();
|
|
|
|
Hook::$BASE_NS = "Tests\\Icinga\\Web\\";
|
|
|
|
Hook::register("Base","a","Tests\\Icinga\\Web\\TestHookImplementation");
|
|
|
|
Hook::register("Base","b","Tests\\Icinga\\Web\\TestHookImplementation");
|
|
|
|
Hook::register("Base","c","Tests\\Icinga\\Web\\TestHookImplementation");
|
|
|
|
$this->assertCount(3,Hook::all("Base"));
|
|
|
|
foreach(Hook::all("Base") as $instance) {
|
|
|
|
$this->assertInstanceOf("Tests\\Icinga\\Web\\TestHookImplementation",$instance);
|
|
|
|
}
|
|
|
|
Hook::clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for Hook::First()
|
|
|
|
* Note: This method is static!
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
public function testFirst()
|
|
|
|
{
|
|
|
|
Hook::clean();
|
|
|
|
Hook::$BASE_NS = "Tests\\Icinga\\Web\\";
|
|
|
|
Hook::register("Base","a","Tests\\Icinga\\Web\\TestHookImplementation");
|
|
|
|
Hook::register("Base","b","Tests\\Icinga\\Web\\TestHookImplementation");
|
|
|
|
Hook::register("Base","c","Tests\\Icinga\\Web\\TestHookImplementation");
|
|
|
|
|
|
|
|
$this->assertInstanceOf("Tests\\Icinga\\Web\\TestHookImplementation",Hook::first("Base"));
|
|
|
|
Hook::clean();
|
|
|
|
}
|
|
|
|
}
|