Test: Introduce basic dashboard test cases

This commit is contained in:
Yonas Habteab 2022-06-01 14:57:35 +02:00
parent c773ad3ac7
commit 8560b2ec3f
4 changed files with 415 additions and 0 deletions

View File

@ -0,0 +1,134 @@
<?php
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */
namespace Tests\Icinga\Web\Dashboard;
use Icinga\Exception\ProgrammingError;
use Icinga\Test\BaseDashboardTestCase;
class BaseDashboardTestCaseTest extends BaseDashboardTestCase
{
public function testWhetherAddEntryAddsAnEntry()
{
$this->dashboard->addEntry($this->getTestHome());
$this->assertTrue(
$this->dashboard->hasEntry(self::TEST_HOME),
'DashboardEntries::addEntry() could not add a Dashboard entry'
);
}
/**
* @depends testWhetherAddEntryAddsAnEntry
*/
public function testWhetherAddEntryAddsDifferentEntries()
{
$this->dashboard->addEntry($this->getTestHome());
$this->dashboard->addEntry($this->getTestHome('Second Home'));
$this->dashboard->addEntry($this->getTestHome('Third Home'));
$this->assertCount(
3,
$this->dashboard->getEntries(),
'DashboardEntries::addEntry() could not add different Dashboard entries'
);
}
/**
* @depends testWhetherAddEntryAddsDifferentEntries
*/
public function testMergeEntryWithSameEntryName()
{
$this->dashboard->addEntry($this->getTestHome());
$this->dashboard->addEntry($this->getTestHome('Second Home'));
$this->dashboard->addEntry($this->getTestHome('Second Home'));
$this->assertCount(
2,
$this->dashboard->getEntries(),
'DashboardEntries::addEntry() could not merge same Dashboard entries'
);
}
/**
* @depends testMergeEntryWithSameEntryName
*/
public function testWhetherGetEntriesReturnsExpectedEntries()
{
$this->dashboard->addEntry($this->getTestHome());
$this->assertCount(
1,
$this->dashboard->getEntries(),
'DashboardEntries::getEntries() returns unexpected dashboard entries'
);
}
public function testeWhetherGetEntryThrowsAnExceptionOnNotExistentEntryName()
{
$this->expectException(ProgrammingError::class);
$this->dashboard->getEntry('test');
}
/**
* @depends testeWhetherGetEntryThrowsAnExceptionOnNotExistentEntryName
*/
public function testWhetherGetEntryGetsAnEntryByName()
{
$this->dashboard->addEntry($this->getTestHome());
$this->assertEquals(
self::TEST_HOME,
$this->dashboard->getEntry(self::TEST_HOME)->getName(),
'DashboardEntries:getEntry() could not return Dashboard entry by name'
);
}
/**
* @depends testMergeEntryWithSameEntryName
*/
public function testWhetherHasEntriesHasNoEntries()
{
$this->assertFalse(
$this->dashboard->hasEntries(),
'DashboardEntries::hasEntries() has Dashboard entries but should not'
);
}
/**
* @depends testWhetherHasEntriesHasNoEntries
*/
public function testWhetherHasEntriesHasEntries()
{
$this->dashboard->addEntry($this->getTestHome());
$this->assertTrue(
$this->dashboard->hasEntries(),
'DashboardEntries::hasEntries() could not return valid expectation'
);
}
/**
* @depends testWhetherHasEntriesHasEntries
*/
public function testWhetherGetEntryKeyTitleArrayReturnFormedArray()
{
$this->dashboard->addEntry(($this->getTestHome())->setTitle('First Home'));
$this->dashboard->addEntry(($this->getTestHome('Test2')->setTitle('Second Home')));
$this->dashboard->addEntry(($this->getTestHome('Test3')->setTitle('Third Home')));
$expected = [
self::TEST_HOME => 'First Home',
'Test2' => 'Second Home',
'Test3' => 'Third Home'
];
$this->assertEquals(
$expected,
$this->dashboard->getEntryKeyTitleArr(),
'DashboardEntries::getEntryKeyTitleArray() could not return valid expectation'
);
}
}

View File

@ -0,0 +1,12 @@
<?php
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */
namespace Tests\Icinga\Web\Dashboard;
use Icinga\Test\BaseDashboardTestCase;
class DashletTest extends BaseDashboardTestCase
{
}

View File

@ -0,0 +1,156 @@
<?php
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */
namespace Tests\Icinga\Web\Dashboard;
use Icinga\Exception\Http\HttpNotFoundException;
use Icinga\Exception\ProgrammingError;
use Icinga\Test\BaseDashboardTestCase;
class HomeTest extends BaseDashboardTestCase
{
public function testWhetherManageEntryManagesANewHomeEntry()
{
$this->dashboard->manageEntry($this->getTestHome());
$this->dashboard->load(self::TEST_HOME);
$this->assertCount(
1,
$this->dashboard->getEntries(),
'Dashboard::manageEntry() could not manage a new Dashboard Home'
);
}
/**
* @depends testWhetherManageEntryManagesANewHomeEntry
*/
public function testWhetherManageEntryUpdatesExistingHomeEntry()
{
$this->dashboard->manageEntry($this->getTestHome());
$this->dashboard->load(self::TEST_HOME);
$home = $this->dashboard->getEntry(self::TEST_HOME);
$home->setTitle('Hello');
$this->dashboard->manageEntry($home);
$this->dashboard->load(self::TEST_HOME);
$home = $this->dashboard->getEntry(self::TEST_HOME);
$this->assertEquals(
'Hello',
$home->getTitle(),
'Dashboard::manageEntry() could not update existing Dashboard Home'
);
}
/**
* @depends testWhetherManageEntryUpdatesExistingHomeEntry
*/
public function testWhetherRemoveEntryThrowsAnExceptionIfNotExists()
{
$this->expectException(ProgrammingError::class);
$this->dashboard->removeEntry('test');
}
/**
* @depends testWhetherRemoveEntryThrowsAnExceptionIfNotExists
*/
public function testWhetherRemoveEntryRemovesExpectedHomeEntry()
{
$this->dashboard->manageEntry($this->getTestHome('Second Home'));
$this->dashboard->load();
$this->dashboard->removeEntry('Second Home');
$this->dashboard->load();
$this->assertFalse(
$this->dashboard->hasEntry('Second Home'),
'Dashboard::removeEntry() could not remove expected Dashboard Home entry'
);
}
/**
* @depends testWhetherRemoveEntryRemovesExpectedHomeEntry
*/
public function testWhetherRemoveEntriesRemovesAllHomeEntries()
{
$this->dashboard->manageEntry($this->getTestHome('Second Home'));
$this->dashboard->load();
$this->dashboard->removeEntries();
$this->dashboard->load();
$this->assertTrue(
$this->dashboard->hasEntries(),
'Dashboard::removeEntries() could not remove all Dashboard Homes'
);
}
/**
* @depends testWhetherRemoveEntriesRemovesAllHomeEntries
*/
public function testWhetherLoadHomesLoadsNullHomes()
{
$this->dashboard->load();
$this->assertFalse(
$this->dashboard->hasEntries(),
'Dashboard::load() has loaded Dashboard Homes but should not'
);
}
public function testWhetherLoadHomeByNameThrowsAnExceptionIfNotExists()
{
$this->expectException(HttpNotFoundException::class);
$this->dashboard->load('test');
}
public function testWhetherLoadHomesActivatesFirstHome()
{
$this->dashboard->manageEntry([$this->getTestHome(), $this->getTestHome('Second Home')]);
$this->dashboard->load();
$this->assertEquals(
self::TEST_HOME,
$this->dashboard->getActiveHome()->getName(),
'Dashboard::load() could not activate expected Dashboard Home'
);
}
/**
* @depends testWhetherLoadHomesActivatesFirstHome
*/
public function testWhetherActivateHomeActivatesAHomeEntry()
{
$this->dashboard->manageEntry([$this->getTestHome(), $this->getTestHome('Second Home')]);
$this->dashboard->load();
$active = $this->dashboard->getEntry('Second Home');
$this->dashboard->activateHome($active);
$this->assertTrue($active->getActive(), 'Dashboard::activateHome() could not activate expected Dashboard Home');
}
/**
* @depends testWhetherActivateHomeActivatesAHomeEntry
*/
public function testWhetherGetActiveHomeGetsExpectedHome()
{
$this->dashboard->addEntry($this->getTestHome());
$this->dashboard->addEntry($this->getTestHome('Second Home'));
$active = $this->dashboard->getEntry(self::TEST_HOME);
$this->dashboard->activateHome($active);
$this->assertEquals(
self::TEST_HOME,
$this->dashboard->getActiveHome()->getName(),
'Dashboard::getActiveHome() could not return expected Dashboard Home'
);
}
}

View File

@ -0,0 +1,113 @@
<?php
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */
namespace Tests\Icinga\Web\Dashboard;
// Necessary as some of these tests disable phpunit's preservation
// of the global state (e.g. autoloaders are in the global state)
require_once realpath(dirname(__FILE__) . '/../../../../bootstrap.php');
use Icinga\Exception\ProgrammingError;
use Icinga\Test\BaseDashboardTestCase;
use Icinga\Web\Dashboard\Dashboard;
use Icinga\Web\Dashboard\Pane;
use Icinga\Web\Widget\Tab;
use Mockery;
class DashboardWithPredefinableActiveName extends Dashboard
{
public $activeName = '';
public function getTabs()
{
$activeTab = $this->activeName ? new Tab(['name' => $this->activeName]) : null;
return Mockery::mock('ipl\Web\Widget\Tabs')
->shouldReceive('getActiveTab')->andReturn($activeTab)
->shouldReceive('activate')
->getMock();
}
}
class PaneTest extends BaseDashboardTestCase
{
public function testWhetherDetermineActivePaneThrowsAnExceptionIfCouldNotDetermine()
{
$this->expectException(\Icinga\Exception\ConfigurationError::class);
$home = $this->getTestHome();
$home->determineActivePane($this->dashboard->getTabs());
}
/**
* @runInSeparateProcess
* @preserveGlobalState
*/
public function testWhetherDetermineActivePaneThrowsAnExceptionIfCouldNotDetermineInvalidPane()
{
$this->expectException(ProgrammingError::class);
Mockery::mock('alias:ipl\Web\Url')->shouldReceive('fromRequest->getParam')->andReturn('test');
$dashboard = new DashboardWithPredefinableActiveName();
$home = $this->getTestHome();
$home->determineActivePane($dashboard->getTabs());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testWhetherDetermineActivePaneDeterminesActiveValidPane()
{
Mockery::mock('alias:ipl\Web\Url')->shouldReceive('fromRequest->getParam')->andReturn('test2');
$home = $this->getTestHome();
$home->addEntry(new Pane('test1'));
$home->addEntry(new Pane('test2'));
$dashboard = new DashboardWithPredefinableActiveName();
$activePane = $home->determineActivePane($dashboard->getTabs());
$this->assertEquals(
'test2',
$activePane->getName(),
'DashboardHome::determineActivePane() could not determine valid active pane'
);
}
public function testWhetherDetermineActivePaneActivatesTheFirstPane()
{
$home = $this->getTestHome();
$home->addEntry(new Pane('test1'));
$home->addEntry(new Pane('test2'));
$this->dashboard->addEntry($home)->activateHome($home);
$activePane = $home->determineActivePane($this->dashboard->getTabs());
$this->assertEquals(
'test1',
$activePane->getName(),
'DashboardHome::determineActivePane() could not determine/activate the first pane'
);
}
public function testWhetherDetermineActivePaneDeterminesActivePane()
{
$dashboard = new DashboardWithPredefinableActiveName();
$dashboard->activeName = 'test2';
$home = $this->getTestHome();
$home->addEntry(new Pane('test1'));
$home->addEntry(new Pane('test2'));
$activePane = $home->determineActivePane($dashboard->getTabs());
$this->assertEquals(
'test2',
$activePane->getName(),
'DashboardHome::determineActivePane() could not determine active pane'
);
}
}