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
This commit is contained in:
parent
0e926ba05e
commit
3a5415f7c8
|
@ -2,23 +2,38 @@
|
|||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
namespace Icinga\Web\Hook;
|
||||
|
||||
use Icinga\Web\Hook;
|
||||
|
||||
class TestHook extends Hook {}
|
||||
|
||||
namespace Tests\Icinga\Web;
|
||||
|
||||
use Mockery;
|
||||
use Exception;
|
||||
use Icinga\Web\Hook;
|
||||
use Icinga\Test\BaseTestCase;
|
||||
use Icinga\Web\Hook;
|
||||
use Icinga\Web\Hook\TestHook;
|
||||
use Exception;
|
||||
|
||||
class ErrorProneHookImplementation
|
||||
class NoHook {}
|
||||
class MyHook extends TestHook {}
|
||||
class AnotherHook extends TestHook {}
|
||||
class FailingHook extends TestHook
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
throw new Exception();
|
||||
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();
|
||||
|
@ -31,83 +46,28 @@ class HookTest extends BaseTestCase
|
|||
Hook::clean();
|
||||
}
|
||||
|
||||
public function testWhetherHasReturnsTrueIfGivenAKnownHook()
|
||||
public function testKnowsWhichHooksAreRegistered()
|
||||
{
|
||||
Hook::registerClass('TestHook', __FUNCTION__, get_class(Mockery::mock(Hook::$BASE_NS . 'TestHook')));
|
||||
|
||||
$this->assertTrue(Hook::has('TestHook'), 'Hook::has does not return true if given a known hook');
|
||||
Hook::register('test', __FUNCTION__, $this->validHook);
|
||||
$this->assertTrue(Hook::has('test'));
|
||||
$this->assertFalse(Hook::has('no_such_hook'));
|
||||
}
|
||||
|
||||
public function testWhetherHasReturnsFalseIfGivenAnUnknownHook()
|
||||
public function testCorrectlyHandlesMultipleInstances()
|
||||
{
|
||||
$this->assertFalse(Hook::has('not_existing'), 'Hook::has does not return false if given an unknown hook');
|
||||
}
|
||||
|
||||
public function testWhetherHooksCanBeRegisteredWithRegisterClass()
|
||||
{
|
||||
Hook::registerClass('TestHook', __FUNCTION__, get_class(Mockery::mock(Hook::$BASE_NS . 'TestHook')));
|
||||
|
||||
$this->assertTrue(Hook::has('TestHook'), 'Hook::registerClass does not properly register a given hook');
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testWhetherHooksCanBeRegisteredWithRegisterClass
|
||||
*/
|
||||
public function testWhetherMultipleHooksOfTheSameTypeCanBeRegisteredWithRegisterClass()
|
||||
{
|
||||
$firstHook = Mockery::mock(Hook::$BASE_NS . 'TestHook');
|
||||
$secondHook = Mockery::mock('overload:' . get_class($firstHook));
|
||||
|
||||
Hook::registerClass('TestHook', 'one', get_class($firstHook));
|
||||
Hook::registerClass('TestHook', 'two', get_class($secondHook));
|
||||
Hook::register('test', 'one', $this->validHook);
|
||||
Hook::register('test', 'two', $this->anotherHook);
|
||||
$this->assertInstanceOf(
|
||||
get_class($secondHook),
|
||||
Hook::createInstance('TestHook', 'two'),
|
||||
'Hook::registerClass is not able to register different hooks of the same type'
|
||||
$this->anotherHook,
|
||||
Hook::createInstance('test', 'two')
|
||||
);
|
||||
$this->assertInstanceOf(
|
||||
$this->validHook,
|
||||
Hook::createInstance('test', 'one')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Icinga\Exception\ProgrammingError
|
||||
*/
|
||||
public function testWhetherOnlyClassesCanBeRegisteredAsHookWithRegisterClass()
|
||||
{
|
||||
Hook::registerClass('TestHook', __FUNCTION__, 'nope');
|
||||
}
|
||||
|
||||
public function testWhetherHooksCanBeRegisteredWithRegisterObject()
|
||||
{
|
||||
Hook::registerObject('TestHook', __FUNCTION__, Mockery::mock(Hook::$BASE_NS . 'TestHook'));
|
||||
|
||||
$this->assertTrue(Hook::has('TestHook'), 'Hook::registerObject does not properly register a given hook');
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testWhetherHooksCanBeRegisteredWithRegisterObject
|
||||
*/
|
||||
public function testWhetherMultipleHooksOfTheSameTypeCanBeRegisteredWithRegisterObject()
|
||||
{
|
||||
$firstHook = Mockery::mock(Hook::$BASE_NS . 'TestHook');
|
||||
$secondHook = Mockery::mock('overload:' . get_class($firstHook));
|
||||
|
||||
Hook::registerObject('TestHook', 'one', $firstHook);
|
||||
Hook::registerObject('TestHook', 'two', $secondHook);
|
||||
$this->assertInstanceOf(
|
||||
get_class($secondHook),
|
||||
Hook::createInstance('TestHook', 'two'),
|
||||
'Hook::registerObject is not able to register different hooks of the same type'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Icinga\Exception\ProgrammingError
|
||||
*/
|
||||
public function testWhetherOnlyObjectsCanBeRegisteredAsHookWithRegisterObject()
|
||||
{
|
||||
Hook::registerObject('TestHook', __FUNCTION__, 'nope');
|
||||
}
|
||||
|
||||
public function testWhetherCreateInstanceReturnsNullIfGivenAnUnknownHookName()
|
||||
public function testReturnsNullForInvalidHooks()
|
||||
{
|
||||
$this->assertNull(
|
||||
Hook::createInstance('not_existing', __FUNCTION__),
|
||||
|
@ -115,98 +75,41 @@ class HookTest extends BaseTestCase
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testWhetherHooksCanBeRegisteredWithRegisterClass
|
||||
*/
|
||||
public function testWhetherCreateInstanceInitializesHooksInheritingFromAPredefinedAbstractHook()
|
||||
public function testReturnsNullForFailingHook()
|
||||
{
|
||||
$baseHook = Mockery::mock(Hook::$BASE_NS . 'TestHook');
|
||||
Hook::registerClass(
|
||||
'TestHook',
|
||||
__FUNCTION__,
|
||||
get_class(Mockery::mock('overload:' . get_class($baseHook)))
|
||||
);
|
||||
Hook::register('test', __FUNCTION__, $this->failingHook);
|
||||
$this->assertNull(Hook::createInstance('test', __FUNCTION__));
|
||||
}
|
||||
|
||||
public function testChecksWhetherCreatedInstancesInheritBaseClasses()
|
||||
{
|
||||
Hook::register('test', __FUNCTION__, $this->validHook);
|
||||
$this->assertInstanceOf(
|
||||
get_class($baseHook),
|
||||
Hook::createInstance('TestHook', __FUNCTION__),
|
||||
'Hook::createInstance does not initialize hooks inheriting from a predefined abstract hook'
|
||||
$this->testBaseClass,
|
||||
Hook::createInstance('test', __FUNCTION__)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testWhetherHooksCanBeRegisteredWithRegisterClass
|
||||
*/
|
||||
public function testWhetherCreateInstanceDoesNotInitializeMultipleHooksForASpecificIdentifier()
|
||||
{
|
||||
Hook::registerClass('TestHook', __FUNCTION__, get_class(Mockery::mock(Hook::$BASE_NS . 'TestHook')));
|
||||
$secondHook = Hook::createInstance('TestHook', __FUNCTION__);
|
||||
$thirdHook = Hook::createInstance('TestHook', __FUNCTION__);
|
||||
|
||||
$this->assertSame(
|
||||
$secondHook,
|
||||
$thirdHook,
|
||||
'Hook::createInstance initializes multiple hooks for a specific identifier'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testWhetherHooksCanBeRegisteredWithRegisterClass
|
||||
*/
|
||||
public function testWhetherCreateInstanceReturnsNullIfHookCannotBeInitialized()
|
||||
{
|
||||
Hook::registerClass('TestHook', __FUNCTION__, 'Tests\Icinga\Web\ErrorProneHookImplementation');
|
||||
|
||||
$this->assertNull(Hook::createInstance('TestHook', __FUNCTION__));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Icinga\Exception\ProgrammingError
|
||||
* @depends testWhetherHooksCanBeRegisteredWithRegisterClass
|
||||
*/
|
||||
public function testWhetherCreateInstanceThrowsAnErrorIfGivenAHookNotInheritingFromAPredefinedAbstractHook()
|
||||
public function testThrowsErrorsForInstancesNotInheritingBaseClasses()
|
||||
{
|
||||
Mockery::mock(Hook::$BASE_NS . 'TestHook');
|
||||
Hook::registerClass('TestHook', __FUNCTION__, get_class(Mockery::mock('TestHook')));
|
||||
|
||||
Hook::createInstance('TestHook', __FUNCTION__);
|
||||
Hook::register('test', __FUNCTION__, $this->invalidHook);
|
||||
Hook::createInstance('test', __FUNCTION__);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testWhetherHooksCanBeRegisteredWithRegisterObject
|
||||
*/
|
||||
public function testWhetherAllReturnsAllRegisteredHooks()
|
||||
public function testCreatesIdenticalInstancesOnlyOnce()
|
||||
{
|
||||
$hook = Mockery::mock(Hook::$BASE_NS . 'TestHook');
|
||||
Hook::registerObject('TestHook', 'one', $hook);
|
||||
Hook::registerObject('TestHook', 'two', $hook);
|
||||
Hook::registerObject('TestHook', 'three', $hook);
|
||||
Hook::register('test', __FUNCTION__, $this->validHook);
|
||||
$first = Hook::createInstance('test', __FUNCTION__);
|
||||
$second = Hook::createInstance('test', __FUNCTION__);
|
||||
|
||||
$this->assertCount(3, Hook::all('TestHook'), 'Hook::all does not return all registered hooks');
|
||||
$this->assertSame($first, $second);
|
||||
}
|
||||
|
||||
public function testWhetherAllReturnsNothingIfGivenAnUnknownHookName()
|
||||
public function testReturnsAnEmptyArrayWithNoRegisteredHook()
|
||||
{
|
||||
$this->assertEmpty(
|
||||
Hook::all('not_existing'),
|
||||
'Hook::all does not return an empty array if given an unknown hook'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testWhetherHooksCanBeRegisteredWithRegisterObject
|
||||
*/
|
||||
public function testWhetherFirstReturnsTheFirstRegisteredHook()
|
||||
{
|
||||
$firstHook = Mockery::mock(Hook::$BASE_NS . 'TestHook');
|
||||
$secondHook = Mockery::mock(Hook::$BASE_NS . 'TestHook');
|
||||
Hook::registerObject('TestHook', 'first', $firstHook);
|
||||
Hook::registerObject('TestHook', 'second', $secondHook);
|
||||
|
||||
$this->assertSame(
|
||||
$firstHook,
|
||||
Hook::first('TestHook'),
|
||||
'Hook::first does not return the first registered hook'
|
||||
);
|
||||
$this->assertEquals(array(), Hook::all('not_existing'));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue