Skip to content

Commit 6f0b3fb

Browse files
committed
Test: Introduce BaseDashboardTestCase class
1 parent 7c2ec7f commit 6f0b3fb

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */
4+
5+
namespace Icinga\Test;
6+
7+
use Icinga\Authentication\Role;
8+
use Icinga\User;
9+
use Icinga\Util\DBUtils;
10+
use Icinga\Web\Dashboard\Dashboard;
11+
use Icinga\Web\Dashboard\DashboardHome;
12+
use ipl\Sql\Connection;
13+
use Mockery;
14+
use PDO;
15+
16+
class BaseDashboardTestCase extends BaseTestCase
17+
{
18+
const TEST_HOME = 'Test Home';
19+
20+
/** @var Dashboard */
21+
protected $dashboard;
22+
23+
/** @var User A test user for the dashboards */
24+
protected $user;
25+
26+
public function setUp(): void
27+
{
28+
parent::setUp();
29+
30+
DBUtils::setConn($this->getDBConnection());
31+
$this->dashboard = new Dashboard();
32+
$this->dashboard->setUser($this->getUser());
33+
}
34+
35+
protected function tearDown(): void
36+
{
37+
parent::tearDown();
38+
Mockery::close(); // Necessary because some tests run in a separate process
39+
}
40+
41+
protected function setupIcingaMock()
42+
{
43+
$moduleMock = Mockery::mock('Icinga\Application\Modules\Module');
44+
$searchUrl = (object) array(
45+
'title' => 'Hosts',
46+
'url' => 'monitoring/list/hosts?sort=host_severity&limit=10'
47+
);
48+
$moduleMock->shouldReceive('getSearchUrls')->andReturn(array(
49+
$searchUrl
50+
));
51+
$moduleMock->shouldReceive('getName')->andReturn('test');
52+
$moduleMock->shouldReceive('getDashboard')->andReturn([]);
53+
$moduleMock->shouldReceive('getDashlets')->andReturn([]);
54+
55+
$moduleManagerMock = Mockery::mock('Icinga\Application\Modules\Manager');
56+
$moduleManagerMock->shouldReceive('getLoadedModules')->andReturn(array(
57+
'test-module' => $moduleMock
58+
));
59+
60+
$bootstrapMock = parent::setupIcingaMock();
61+
$bootstrapMock->shouldReceive('getModuleManager')->andReturn($moduleManagerMock);
62+
63+
return $bootstrapMock;
64+
}
65+
66+
protected function getDBConnection(): Connection
67+
{
68+
$conn = new Connection([
69+
'db' => 'sqlite',
70+
'dbname' => ':memory:',
71+
'options' => [
72+
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
73+
]
74+
]);
75+
76+
$fixtures = @file_get_contents(__DIR__ . '/fixtures.sql');
77+
$conn->exec($fixtures);
78+
79+
return $conn;
80+
}
81+
82+
protected function getUser(): User
83+
{
84+
if ($this->user === null) {
85+
$role = new Role();
86+
$role->setPermissions(['*']);
87+
88+
$this->user = new User('test');
89+
$this->user->setRoles([$role]);
90+
}
91+
92+
return $this->user;
93+
}
94+
95+
protected function getTestHome(string $name = self::TEST_HOME)
96+
{
97+
return new DashboardHome($name);
98+
}
99+
}

0 commit comments

Comments
 (0)