Skip to content

Commit 1260a83

Browse files
committed
Initial implementation of controller unit tests.
1 parent 90bbe57 commit 1260a83

File tree

5 files changed

+231
-6
lines changed

5 files changed

+231
-6
lines changed

app/Contracts/Repository/DatabaseHostRepositoryInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ interface DatabaseHostRepositoryInterface extends RepositoryInterface
3333
*/
3434
public function getWithViewDetails();
3535

36+
/**
37+
* Return a database host with the databases and associated servers that are attached to said databases.
38+
*
39+
* @param int $id
40+
* @return mixed
41+
*
42+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
43+
*/
44+
public function getWithServers($id);
45+
3646
/**
3747
* Delete a database host from the DB if there are no databases using it.
3848
*

app/Http/Controllers/Admin/DatabaseController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ public function index()
9090
/**
9191
* Display database host to user.
9292
*
93-
* @param \Pterodactyl\Models\DatabaseHost $host
93+
* @param int $host
9494
* @return \Illuminate\View\View
95+
*
96+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
9597
*/
96-
public function view(DatabaseHost $host)
98+
public function view($host)
9799
{
98-
$host->load('databases.server');
99-
100100
return view('admin.databases.view', [
101101
'locations' => $this->locationRepository->getAllWithNodes(),
102-
'host' => $host,
102+
'host' => $this->repository->getWithServers($host),
103103
]);
104104
}
105105

app/Repositories/Eloquent/DatabaseHostRepository.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
namespace Pterodactyl\Repositories\Eloquent;
2626

27+
use Webmozart\Assert\Assert;
2728
use Pterodactyl\Models\DatabaseHost;
2829
use Pterodactyl\Exceptions\DisplayException;
2930
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
@@ -47,13 +48,26 @@ public function getWithViewDetails()
4748
return $this->getBuilder()->withCount('databases')->with('node')->get();
4849
}
4950

51+
public function getWithServers($id)
52+
{
53+
Assert::numeric($id, 'First argument passed to getWithServers must be numeric, recieved %s.');
54+
55+
$instance = $this->getBuilder()->with('databases.server')->find($id, $this->getColumns());
56+
if (! $instance) {
57+
throw new RecordNotFoundException();
58+
}
59+
60+
return $instance;
61+
}
62+
5063
/**
5164
* {@inheritdoc}
5265
*/
5366
public function deleteIfNoDatabases($id)
5467
{
55-
$instance = $this->getBuilder()->withCount('databases')->find($id);
68+
Assert::numeric($id, 'First argument passed to deleteIfNoDatabases must be numeric, recieved %s.');
5669

70+
$instance = $this->getBuilder()->withCount('databases')->find($id);
5771
if (! $instance) {
5872
throw new RecordNotFoundException();
5973
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Tests\Assertions;
26+
27+
use PHPUnit_Framework_Assert;
28+
29+
trait ControllerAssertionsTrait
30+
{
31+
/**
32+
* Assert that a view name equals the passed name.
33+
*
34+
* @param string $name
35+
* @param \Illuminate\View\View $view
36+
*/
37+
public function assertViewNameEquals($name, $view)
38+
{
39+
PHPUnit_Framework_Assert::assertEquals($name, $view->getName());
40+
}
41+
42+
/**
43+
* Assert that a view name does not equal a provided name.
44+
*
45+
* @param string $name
46+
* @param \Illuminate\View\View $view
47+
*/
48+
public function assertViewNameNotEquals($name, $view)
49+
{
50+
PHPUnit_Framework_Assert::assertNotEquals($name, $view->getName());
51+
}
52+
53+
/**
54+
* Assert that a view has an attribute passed into it.
55+
*
56+
* @param string $attribute
57+
* @param \Illuminate\View\View $view
58+
*/
59+
public function assertViewHasKey($attribute, $view)
60+
{
61+
PHPUnit_Framework_Assert::assertArrayHasKey($attribute, $view->getData());
62+
}
63+
64+
/**
65+
* Assert that a view does not have a specific attribute passed in.
66+
*
67+
* @param string $attribute
68+
* @param \Illuminate\View\View $view
69+
*/
70+
public function assertViewNotHasKey($attribute, $view)
71+
{
72+
PHPUnit_Framework_Assert::assertArrayNotHasKey($attribute, $view->getData());
73+
}
74+
75+
/**
76+
* Assert that a view attribute equals a given parameter.
77+
*
78+
* @param string $attribute
79+
* @param mixed $value
80+
* @param \Illuminate\View\View $view
81+
*/
82+
public function assertViewKeyEquals($attribute, $value, $view)
83+
{
84+
PHPUnit_Framework_Assert::assertEquals($value, array_get($view->getData(), $attribute));
85+
}
86+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Tests\Unit\Http\Controllers\Admin;
26+
27+
use Mockery as m;
28+
use Prologue\Alerts\AlertsMessageBag;
29+
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
30+
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
31+
use Pterodactyl\Http\Controllers\Admin\DatabaseController;
32+
use Pterodactyl\Services\Database\DatabaseHostService;
33+
use Tests\Assertions\ControllerAssertionsTrait;
34+
use Tests\TestCase;
35+
36+
class DatabaseControllerTest extends TestCase
37+
{
38+
use ControllerAssertionsTrait;
39+
40+
/**
41+
* @var \Prologue\Alerts\AlertsMessageBag
42+
*/
43+
protected $alert;
44+
45+
/**
46+
* @var \Pterodactyl\Http\Controllers\Admin\DatabaseController
47+
*/
48+
protected $controller;
49+
50+
/**
51+
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
52+
*/
53+
protected $locationRepository;
54+
55+
/**
56+
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
57+
*/
58+
protected $repository;
59+
60+
/**
61+
* @var \Pterodactyl\Services\Database\DatabaseHostService
62+
*/
63+
protected $service;
64+
65+
/**
66+
* Setup tests.
67+
*/
68+
public function setUp()
69+
{
70+
parent::setUp();
71+
72+
$this->alert = m::mock(AlertsMessageBag::class);
73+
$this->locationRepository = m::mock(LocationRepositoryInterface::class);
74+
$this->repository = m::mock(DatabaseHostRepositoryInterface::class);
75+
$this->service = m::mock(DatabaseHostService::class);
76+
77+
$this->controller = new DatabaseController(
78+
$this->alert,
79+
$this->repository,
80+
$this->service,
81+
$this->locationRepository
82+
);
83+
}
84+
85+
/**
86+
* Test the index controller.
87+
*/
88+
public function testIndexController()
89+
{
90+
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn('getAllWithNodes');
91+
$this->repository->shouldReceive('getWithViewDetails')->withNoArgs()->once()->andReturn('getWithViewDetails');
92+
93+
$view = $this->controller->index();
94+
95+
$this->assertViewNameEquals('admin.databases.index', $view);
96+
$this->assertViewHasKey('locations', $view);
97+
$this->assertViewHasKey('hosts', $view);
98+
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $view);
99+
$this->assertViewKeyEquals('hosts', 'getWithViewDetails', $view);
100+
}
101+
102+
public function testViewController()
103+
{
104+
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn('getAllWithNodes');
105+
$this->repository->shouldReceive('getWithServers')->with(1)->once()->andReturn('getWithServers');
106+
107+
$view = $this->controller->view(1);
108+
109+
$this->assertViewNameEquals('admin.databases.view', $view);
110+
$this->assertViewHasKey('locations', $view);
111+
$this->assertViewHasKey('host', $view);
112+
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $view);
113+
$this->assertViewKeyEquals('host', 'getWithServers', $view);
114+
}
115+
}

0 commit comments

Comments
 (0)