Skip to content

Commit d826647

Browse files
committed
Tests: Avoid mocking ipl\Web\Url & add some additional dashboard tests
1 parent 6fd8e05 commit d826647

File tree

2 files changed

+153
-62
lines changed

2 files changed

+153
-62
lines changed

test/php/library/Icinga/Web/Dashboard/HomeTest.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public function testWhetherManageEntryUpdatesExistingHomeEntry()
3030
$this->dashboard->manageEntry($this->getTestHome());
3131
$this->dashboard->load(self::TEST_HOME);
3232

33-
$home = $this->dashboard->getEntry(self::TEST_HOME);
33+
$home = $this->dashboard->getActiveHome();
3434
$home->setTitle('Hello');
3535

3636
$this->dashboard->manageEntry($home);
3737
$this->dashboard->load(self::TEST_HOME);
3838

39-
$home = $this->dashboard->getEntry(self::TEST_HOME);
39+
$home = $this->dashboard->getActiveHome();
4040

4141
$this->assertEquals(
4242
'Hello',
@@ -109,6 +109,34 @@ public function testWhetherLoadHomeByNameThrowsAnExceptionIfNotExists()
109109
$this->dashboard->load('test');
110110
}
111111

112+
/**
113+
* @depends testWhetherActivateHomeActivatesAHomeEntry
114+
*/
115+
public function testWhetherLoadHomesByNameAndLoadAllParamSetLoadsAllHomesAndActivatesTheExpectedHome()
116+
{
117+
$this->dashboard->manageEntry([$this->getTestHome(), $this->getTestHome('Second Home')]);
118+
$this->dashboard->load('Second Home', null, true);
119+
120+
$this->assertCount(
121+
2,
122+
$this->dashboard->getEntries(),
123+
'Dashboard::load() could not all expected Dashboard Homes'
124+
);
125+
126+
$this->assertEquals(
127+
'Second Home',
128+
$this->dashboard->getActiveHome()->getName(),
129+
'Dashboard::load() could not load all expected Dashboard Homes and activate expected Dashboard Home'
130+
);
131+
}
132+
133+
public function testWhetherActivateHomeThrowsAnExceptionIfNotExists()
134+
{
135+
$this->expectException(ProgrammingError::class);
136+
137+
$this->dashboard->activateHome($this->getTestHome('Activate Home'));
138+
}
139+
112140
public function testWhetherLoadHomesActivatesFirstHome()
113141
{
114142
$this->dashboard->manageEntry([$this->getTestHome(), $this->getTestHome('Second Home')]);
@@ -133,7 +161,7 @@ public function testWhetherActivateHomeActivatesAHomeEntry()
133161
$active = $this->dashboard->getEntry('Second Home');
134162
$this->dashboard->activateHome($active);
135163

136-
$this->assertTrue($active->getActive(), 'Dashboard::activateHome() could not activate expected Dashboard Home');
164+
$this->assertTrue($active->isActive(), 'Dashboard::activateHome() could not activate expected Dashboard Home');
137165
}
138166

139167
/**

test/php/library/Icinga/Web/Dashboard/PaneTest.php

Lines changed: 122 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,110 +4,173 @@
44

55
namespace Tests\Icinga\Web\Dashboard;
66

7-
// Necessary as some of these tests disable phpunit's preservation
8-
// of the global state (e.g. autoloaders are in the global state)
9-
require_once realpath(dirname(__FILE__) . '/../../../../bootstrap.php');
10-
117
use Icinga\Exception\ProgrammingError;
128
use Icinga\Test\BaseDashboardTestCase;
13-
use Icinga\Web\Dashboard\Dashboard;
9+
use Icinga\Web\Dashboard\DashboardHome;
1410
use Icinga\Web\Dashboard\Pane;
15-
use Icinga\Web\Widget\Tab;
16-
use Mockery;
1711

18-
class DashboardWithPredefinableActiveName extends Dashboard
12+
class PaneTest extends BaseDashboardTestCase
1913
{
20-
public $activeName = '';
14+
const TEST_PANE = 'Test Pane';
2115

22-
public function getTabs()
16+
protected function getTestPane(string $name = self::TEST_PANE): Pane
2317
{
24-
$activeTab = $this->activeName ? new Tab(['name' => $this->activeName]) : null;
25-
26-
return Mockery::mock('ipl\Web\Widget\Tabs')
27-
->shouldReceive('getActiveTab')->andReturn($activeTab)
28-
->shouldReceive('activate')
29-
->getMock();
18+
return new Pane($name);
3019
}
31-
}
3220

33-
class PaneTest extends BaseDashboardTestCase
34-
{
35-
public function testWhetherDetermineActivePaneThrowsAnExceptionIfCouldNotDetermine()
21+
public function testWhetherActivatePaneThrowsAnExceptionIfNotExists()
3622
{
37-
$this->expectException(\Icinga\Exception\ConfigurationError::class);
23+
$this->expectException(ProgrammingError::class);
3824

3925
$home = $this->getTestHome();
40-
$home->determineActivePane($this->dashboard->getTabs());
26+
$home->activatePane(new Pane(self::TEST_PANE));
27+
}
28+
29+
public function testWhetherActivatePaneActivatesExpectedPane()
30+
{
31+
$home = $this->getTestHome();
32+
$home->addEntry($this->getTestPane());
33+
34+
$home->activatePane($home->getEntry(self::TEST_PANE));
35+
36+
$this->assertEquals(
37+
self::TEST_PANE,
38+
$home->getActivePane()->getName(),
39+
'DashboardHome::activatePane() could not activate expected Dashboard Pane'
40+
);
4141
}
4242

4343
/**
44-
* @runInSeparateProcess
45-
* @preserveGlobalState
44+
* @depends testWhetherActivatePaneActivatesExpectedPane
4645
*/
47-
public function testWhetherDetermineActivePaneThrowsAnExceptionIfCouldNotDetermineInvalidPane()
46+
public function testWhetherLoadDashboardEntriesActivatesFirstPane()
4847
{
49-
$this->expectException(ProgrammingError::class);
48+
$home = $this->getTestHome();
49+
$this->dashboard->manageEntry($home);
5050

51-
Mockery::mock('alias:ipl\Web\Url')->shouldReceive('fromRequest->getParam')->andReturn('test');
51+
$home->manageEntry($this->getTestPane());
52+
$home->manageEntry($this->getTestPane('Test Me'));
5253

53-
$dashboard = new DashboardWithPredefinableActiveName();
54-
$home = $this->getTestHome();
54+
$this->dashboard->load();
55+
$home = $this->dashboard->getActiveHome();
5556

56-
$home->determineActivePane($dashboard->getTabs());
57+
$this->assertEquals(
58+
self::TEST_PANE,
59+
$home->getActivePane()->getName(),
60+
'DashboardHome::loadDashboardEntries() could not activate expected Dashboard Pane'
61+
);
5762
}
5863

5964
/**
60-
* @runInSeparateProcess
61-
* @preserveGlobalState disabled
65+
* @depends testWhetherLoadDashboardEntriesActivatesFirstPane
6266
*/
63-
public function testWhetherDetermineActivePaneDeterminesActiveValidPane()
67+
public function testWhetherActivatePaneActivatesAPaneEntry()
6468
{
65-
Mockery::mock('alias:ipl\Web\Url')->shouldReceive('fromRequest->getParam')->andReturn('test2');
66-
6769
$home = $this->getTestHome();
68-
$home->addEntry(new Pane('test1'));
69-
$home->addEntry(new Pane('test2'));
70+
$this->dashboard->manageEntry($home);
71+
72+
$home->manageEntry(new Pane(self::TEST_PANE));
7073

71-
$dashboard = new DashboardWithPredefinableActiveName();
72-
$activePane = $home->determineActivePane($dashboard->getTabs());
74+
$this->dashboard->load(self::TEST_HOME, self::TEST_PANE);
75+
$home = $this->dashboard->getActiveHome();
7376

7477
$this->assertEquals(
75-
'test2',
76-
$activePane->getName(),
77-
'DashboardHome::determineActivePane() could not determine valid active pane'
78+
self::TEST_PANE,
79+
$home->getActivePane()->getName(),
80+
'DashboardHome::loadDashboardEntries() could not load and activate expected Dashboard Pane'
7881
);
7982
}
8083

81-
public function testWhetherDetermineActivePaneActivatesTheFirstPane()
84+
/**
85+
* @depends testWhetherActivatePaneActivatesAPaneEntry
86+
*/
87+
public function testWhetherGetActivePaneGetsExpectedPane()
8288
{
8389
$home = $this->getTestHome();
84-
$home->addEntry(new Pane('test1'));
85-
$home->addEntry(new Pane('test2'));
90+
$home->addEntry($this->getTestPane());
91+
$home->addEntry($this->getTestPane('Test Me'));
8692

87-
$this->dashboard->addEntry($home)->activateHome($home);
93+
$home->activatePane($home->getEntry('Test Me'));
8894

89-
$activePane = $home->determineActivePane($this->dashboard->getTabs());
9095
$this->assertEquals(
91-
'test1',
92-
$activePane->getName(),
93-
'DashboardHome::determineActivePane() could not determine/activate the first pane'
96+
'Test Me',
97+
$home->getActivePane()->getName(),
98+
'DashboardHome::getActivePane() could not determine valid active pane'
9499
);
95100
}
96101

97-
public function testWhetherDetermineActivePaneDeterminesActivePane()
102+
/**
103+
* @depends testWhetherActivatePaneActivatesAPaneEntry
104+
*/
105+
public function testWhetherManageEntryManagesANewPaneEntry()
98106
{
99-
$dashboard = new DashboardWithPredefinableActiveName();
100-
$dashboard->activeName = 'test2';
107+
$home = $this->getTestHome();
108+
$this->dashboard->manageEntry($home);
109+
110+
$home->manageEntry($this->getTestPane());
101111

112+
$this->dashboard->load(self::TEST_HOME);
113+
$home = $this->dashboard->getActiveHome();
114+
115+
$this->assertCount(
116+
1,
117+
$home->getEntries(),
118+
'DashboardHome::manageEntry() could not manage a new Dashboard Pane'
119+
);
120+
}
121+
122+
/**
123+
* @depends testWhetherManageEntryManagesANewPaneEntry
124+
*/
125+
public function testWhetherManageEntryUpdatesExistingPaneEntry()
126+
{
102127
$home = $this->getTestHome();
103-
$home->addEntry(new Pane('test1'));
104-
$home->addEntry(new Pane('test2'));
128+
$this->dashboard->manageEntry($home);
129+
130+
$home->manageEntry($this->getTestPane());
131+
132+
$this->dashboard->load(self::TEST_HOME);
133+
134+
$home = $this->dashboard->getActiveHome();
135+
$home->getActivePane()->setTitle('Hello');
136+
137+
$home->manageEntry($home->getEntries());
138+
$this->dashboard->load(self::TEST_HOME);
139+
140+
$home = $this->dashboard->getActiveHome();
105141

106-
$activePane = $home->determineActivePane($dashboard->getTabs());
107142
$this->assertEquals(
108-
'test2',
109-
$activePane->getName(),
110-
'DashboardHome::determineActivePane() could not determine active pane'
143+
'Hello',
144+
$home->getActivePane()->getTitle(),
145+
'DashboardHome::manageEntry() could not update existing Dashboard Pane'
146+
);
147+
}
148+
149+
/**
150+
* @depends testWhetherManageEntryUpdatesExistingPaneEntry
151+
*/
152+
public function testWhetherManageEntryMovesAPaneToAnotherExistingHome()
153+
{
154+
$home = $this->getTestHome('Second Home');
155+
$this->dashboard->manageEntry([$this->getTestHome(), $home]);
156+
157+
$home->manageEntry([$this->getTestPane(), $this->getTestPane('Test Me')]);
158+
159+
$this->dashboard->load('Second Home', null, true);
160+
161+
$home = $this->dashboard->getActiveHome();
162+
/** @var DashboardHome $default */
163+
$default = $this->dashboard->getEntry(self::TEST_HOME);
164+
165+
$default->manageEntry($home->getEntry(self::TEST_PANE), $home);
166+
$this->dashboard->load(self::TEST_HOME);
167+
168+
$default = $this->dashboard->getActiveHome();
169+
170+
$this->assertCount(
171+
1,
172+
$default->getEntries(),
173+
'DashboardHome::manageEntry() could not move a Dashboard Pane to another existing Dashboard Home'
111174
);
112175
}
113176
}

0 commit comments

Comments
 (0)