2013-06-03 15:34:57 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-06-03 15:34:57 +02:00
|
|
|
|
2014-09-12 09:07:27 +02:00
|
|
|
namespace Icinga\Web\Hook;
|
2013-06-03 15:34:57 +02:00
|
|
|
|
2014-04-09 14:18:14 +02:00
|
|
|
use Icinga\Web\Hook;
|
2014-09-12 09:07:27 +02:00
|
|
|
|
|
|
|
class TestHook extends Hook {}
|
|
|
|
|
|
|
|
namespace Tests\Icinga\Web;
|
|
|
|
|
2014-04-10 10:32:50 +02:00
|
|
|
use Icinga\Test\BaseTestCase;
|
2014-09-12 09:07:27 +02:00
|
|
|
use Icinga\Web\Hook;
|
|
|
|
use Icinga\Web\Hook\TestHook;
|
|
|
|
use Exception;
|
2013-06-27 13:42:48 +02:00
|
|
|
|
2014-09-12 09:07:27 +02:00
|
|
|
class NoHook {}
|
|
|
|
class MyHook extends TestHook {}
|
|
|
|
class AnotherHook extends TestHook {}
|
|
|
|
class FailingHook extends TestHook
|
2013-06-10 12:37:27 +02:00
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
2014-09-12 09:07:27 +02:00
|
|
|
throw new Exception("I'm failing");
|
2013-06-10 12:37:27 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-03 15:34:57 +02:00
|
|
|
|
2014-04-10 10:32:50 +02:00
|
|
|
class HookTest extends BaseTestCase
|
2013-06-03 15:34:57 +02:00
|
|
|
{
|
2014-09-12 09:07:27 +02:00
|
|
|
protected $invalidHook = '\\Tests\\Icinga\\Web\\NoHook';
|
|
|
|
protected $validHook = '\\Tests\\Icinga\\Web\\MyHook';
|
|
|
|
protected $anotherHook = '\\Tests\\Icinga\\Web\\AnotherHook';
|
|
|
|
protected $failingHook = '\\Tests\\Icinga\\Web\\FailingHook';
|
|
|
|
protected $testBaseClass = '\\Icinga\\Web\\Hook\\TestHook';
|
|
|
|
|
2014-04-11 15:31:29 +02:00
|
|
|
public function setUp()
|
2013-06-27 13:42:48 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
Hook::clean();
|
|
|
|
}
|
|
|
|
|
2014-04-11 15:31:29 +02:00
|
|
|
public function tearDown()
|
2013-06-27 13:42:48 +02:00
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
Hook::clean();
|
|
|
|
}
|
|
|
|
|
2014-09-12 09:07:27 +02:00
|
|
|
public function testKnowsWhichHooksAreRegistered()
|
2013-06-03 15:34:57 +02:00
|
|
|
{
|
2014-09-12 09:07:27 +02:00
|
|
|
Hook::register('test', __FUNCTION__, $this->validHook);
|
|
|
|
$this->assertTrue(Hook::has('test'));
|
|
|
|
$this->assertFalse(Hook::has('no_such_hook'));
|
2013-06-03 15:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 09:07:27 +02:00
|
|
|
public function testCorrectlyHandlesMultipleInstances()
|
2014-04-24 14:40:49 +02:00
|
|
|
{
|
2014-09-12 09:07:27 +02:00
|
|
|
Hook::register('test', 'one', $this->validHook);
|
|
|
|
Hook::register('test', 'two', $this->anotherHook);
|
2014-04-24 14:40:49 +02:00
|
|
|
$this->assertInstanceOf(
|
2014-09-12 09:07:27 +02:00
|
|
|
$this->anotherHook,
|
|
|
|
Hook::createInstance('test', 'two')
|
2014-04-24 14:40:49 +02:00
|
|
|
);
|
|
|
|
$this->assertInstanceOf(
|
2014-09-12 09:07:27 +02:00
|
|
|
$this->validHook,
|
|
|
|
Hook::createInstance('test', 'one')
|
2014-04-24 14:40:49 +02:00
|
|
|
);
|
2013-06-03 15:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 09:07:27 +02:00
|
|
|
public function testReturnsNullForInvalidHooks()
|
2014-04-24 14:40:49 +02:00
|
|
|
{
|
|
|
|
$this->assertNull(
|
|
|
|
Hook::createInstance('not_existing', __FUNCTION__),
|
|
|
|
'Hook::createInstance does not return null if given an unknown hook'
|
|
|
|
);
|
2013-06-27 13:42:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 09:07:27 +02:00
|
|
|
public function testReturnsNullForFailingHook()
|
2013-06-27 13:42:48 +02:00
|
|
|
{
|
2014-09-12 09:07:27 +02:00
|
|
|
Hook::register('test', __FUNCTION__, $this->failingHook);
|
|
|
|
$this->assertNull(Hook::createInstance('test', __FUNCTION__));
|
2014-04-24 14:40:49 +02:00
|
|
|
}
|
2013-06-27 13:42:48 +02:00
|
|
|
|
2014-09-12 09:07:27 +02:00
|
|
|
public function testChecksWhetherCreatedInstancesInheritBaseClasses()
|
2014-04-24 14:40:49 +02:00
|
|
|
{
|
2014-09-12 09:07:27 +02:00
|
|
|
Hook::register('test', __FUNCTION__, $this->validHook);
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
$this->testBaseClass,
|
|
|
|
Hook::createInstance('test', __FUNCTION__)
|
2014-04-24 14:40:49 +02:00
|
|
|
);
|
|
|
|
}
|
2013-06-27 13:42:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Icinga\Exception\ProgrammingError
|
|
|
|
*/
|
2014-09-12 09:07:27 +02:00
|
|
|
public function testThrowsErrorsForInstancesNotInheritingBaseClasses()
|
2013-06-27 13:42:48 +02:00
|
|
|
{
|
2014-09-12 09:07:27 +02:00
|
|
|
Hook::register('test', __FUNCTION__, $this->invalidHook);
|
|
|
|
Hook::createInstance('test', __FUNCTION__);
|
2014-04-24 14:40:49 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 09:07:27 +02:00
|
|
|
public function testCreatesIdenticalInstancesOnlyOnce()
|
2014-04-24 14:40:49 +02:00
|
|
|
{
|
2014-09-12 09:07:27 +02:00
|
|
|
Hook::register('test', __FUNCTION__, $this->validHook);
|
|
|
|
$first = Hook::createInstance('test', __FUNCTION__);
|
|
|
|
$second = Hook::createInstance('test', __FUNCTION__);
|
2014-04-24 14:40:49 +02:00
|
|
|
|
2014-09-12 09:07:27 +02:00
|
|
|
$this->assertSame($first, $second);
|
2013-06-27 13:42:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 09:07:27 +02:00
|
|
|
public function testReturnsAnEmptyArrayWithNoRegisteredHook()
|
2013-06-27 13:42:48 +02:00
|
|
|
{
|
2014-09-12 09:07:27 +02:00
|
|
|
$this->assertEquals(array(), Hook::all('not_existing'));
|
2013-06-03 15:34:57 +02:00
|
|
|
}
|
|
|
|
}
|