Thomas Gelf 3a5415f7c8 test/Web/Hook: refactor without mocks
Mocks made no sense here. Also introduced a "new" way of naming test
functions. Output now looks as follows:

s\Icinga\Web\Hook
 [x] Knows which hooks are registered
 [x] Correctly handles multiple instances
 [x] Returns null for invalid hooks
 [x] Returns null for failing hook
 [x] Checks whether created instances inherit base classes
 [x] Throws errors for instances not inheriting base classes
 [x] Creates identical instances only once
 [x] Returns an empty array with no registered hook
2014-09-12 09:07:27 +02:00

116 lines
3.1 KiB
PHP

<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Hook;
use Icinga\Web\Hook;
class TestHook extends Hook {}
namespace Tests\Icinga\Web;
use Icinga\Test\BaseTestCase;
use Icinga\Web\Hook;
use Icinga\Web\Hook\TestHook;
use Exception;
class NoHook {}
class MyHook extends TestHook {}
class AnotherHook extends TestHook {}
class FailingHook extends TestHook
{
public function __construct()
{
throw new Exception("I'm failing");
}
}
class HookTest extends BaseTestCase
{
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';
public function setUp()
{
parent::setUp();
Hook::clean();
}
public function tearDown()
{
parent::tearDown();
Hook::clean();
}
public function testKnowsWhichHooksAreRegistered()
{
Hook::register('test', __FUNCTION__, $this->validHook);
$this->assertTrue(Hook::has('test'));
$this->assertFalse(Hook::has('no_such_hook'));
}
public function testCorrectlyHandlesMultipleInstances()
{
Hook::register('test', 'one', $this->validHook);
Hook::register('test', 'two', $this->anotherHook);
$this->assertInstanceOf(
$this->anotherHook,
Hook::createInstance('test', 'two')
);
$this->assertInstanceOf(
$this->validHook,
Hook::createInstance('test', 'one')
);
}
public function testReturnsNullForInvalidHooks()
{
$this->assertNull(
Hook::createInstance('not_existing', __FUNCTION__),
'Hook::createInstance does not return null if given an unknown hook'
);
}
public function testReturnsNullForFailingHook()
{
Hook::register('test', __FUNCTION__, $this->failingHook);
$this->assertNull(Hook::createInstance('test', __FUNCTION__));
}
public function testChecksWhetherCreatedInstancesInheritBaseClasses()
{
Hook::register('test', __FUNCTION__, $this->validHook);
$this->assertInstanceOf(
$this->testBaseClass,
Hook::createInstance('test', __FUNCTION__)
);
}
/**
* @expectedException Icinga\Exception\ProgrammingError
*/
public function testThrowsErrorsForInstancesNotInheritingBaseClasses()
{
Hook::register('test', __FUNCTION__, $this->invalidHook);
Hook::createInstance('test', __FUNCTION__);
}
public function testCreatesIdenticalInstancesOnlyOnce()
{
Hook::register('test', __FUNCTION__, $this->validHook);
$first = Hook::createInstance('test', __FUNCTION__);
$second = Hook::createInstance('test', __FUNCTION__);
$this->assertSame($first, $second);
}
public function testReturnsAnEmptyArrayWithNoRegisteredHook()
{
$this->assertEquals(array(), Hook::all('not_existing'));
}
}