Skip to content

Commit f451e4d

Browse files
committed
Begin unit tests for repositories
1 parent 72735c2 commit f451e4d

File tree

18 files changed

+734
-43
lines changed

18 files changed

+734
-43
lines changed

app/Contracts/Repository/DatabaseHostRepositoryInterface.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,4 @@ public function getWithViewDetails();
4242
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
4343
*/
4444
public function getWithServers($id);
45-
46-
/**
47-
* Delete a database host from the DB if there are no databases using it.
48-
*
49-
* @param int $id
50-
* @return bool|null
51-
*
52-
* @throws \Pterodactyl\Exceptions\DisplayException
53-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
54-
*/
55-
public function deleteIfNoDatabases($id);
5645
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 Pterodactyl\Exceptions\Repository;
26+
27+
use Pterodactyl\Exceptions\DisplayException;
28+
29+
class DuplicateDatabaseNameException extends DisplayException
30+
{
31+
}

app/Http/Controllers/Admin/LocationController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public function index()
8383
*
8484
* @param int $id
8585
* @return \Illuminate\View\View
86+
*
87+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
8688
*/
8789
public function view($id)
8890
{

app/Http/Controllers/Admin/ServersController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ public function setContainer(Request $request, Server $server)
434434
*
435435
* @throws \Pterodactyl\Exceptions\DisplayException
436436
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
437+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
437438
*/
438439
public function toggleInstall(Server $server)
439440
{
@@ -493,6 +494,7 @@ public function rebuildContainer(Server $server)
493494
*
494495
* @throws \Pterodactyl\Exceptions\DisplayException
495496
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
497+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
496498
*/
497499
public function manageSuspension(Request $request, Server $server)
498500
{
@@ -533,6 +535,7 @@ public function updateBuild(Request $request, Server $server)
533535
* @return \Illuminate\Http\RedirectResponse
534536
*
535537
* @throws \Pterodactyl\Exceptions\DisplayException
538+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
536539
*/
537540
public function delete(Request $request, Server $server)
538541
{
@@ -551,6 +554,7 @@ public function delete(Request $request, Server $server)
551554
*
552555
* @throws \Pterodactyl\Exceptions\DisplayException
553556
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
557+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
554558
*/
555559
public function saveStartup(Request $request, Server $server)
556560
{

app/Providers/RepositoryServiceProvider.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@
2525
namespace Pterodactyl\Providers;
2626

2727
use Illuminate\Support\ServiceProvider;
28+
use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface;
29+
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
30+
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
2831
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
32+
use Pterodactyl\Repositories\Daemon\CommandRepository;
33+
use Pterodactyl\Repositories\Daemon\FileRepository;
34+
use Pterodactyl\Repositories\Daemon\PowerRepository;
2935
use Pterodactyl\Repositories\Eloquent\NodeRepository;
3036
use Pterodactyl\Repositories\Eloquent\PackRepository;
3137
use Pterodactyl\Repositories\Eloquent\UserRepository;
@@ -71,8 +77,8 @@ public function register()
7177
$this->app->bind(AllocationRepositoryInterface::class, AllocationRepository::class);
7278
$this->app->bind(ApiKeyRepositoryInterface::class, ApiKeyRepository::class);
7379
$this->app->bind(ApiPermissionRepositoryInterface::class, ApiPermissionRepository::class);
74-
$this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class);
7580
$this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class);
81+
$this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class);
7682
$this->app->bind(LocationRepositoryInterface::class, LocationRepository::class);
7783
$this->app->bind(NodeRepositoryInterface::class, NodeRepository::class);
7884
$this->app->bind(OptionVariableRepositoryInterface::class, OptionVariableRepository::class);
@@ -86,6 +92,9 @@ public function register()
8692

8793
// Daemon Repositories
8894
$this->app->bind(ConfigurationRepositoryInterface::class, ConfigurationRepository::class);
95+
$this->app->bind(CommandRepositoryInterface::class, CommandRepository::class);
8996
$this->app->bind(DaemonServerRepositoryInterface::class, DaemonServerRepository::class);
97+
$this->app->bind(FileRepositoryInterface::class, FileRepository::class);
98+
$this->app->bind(PowerRepositoryInterface::class, PowerRepository::class);
9099
}
91100
}

app/Repositories/Eloquent/DatabaseHostRepository.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
use Webmozart\Assert\Assert;
2828
use Pterodactyl\Models\DatabaseHost;
29-
use Pterodactyl\Exceptions\DisplayException;
3029
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
3130
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
3231

@@ -48,6 +47,9 @@ public function getWithViewDetails()
4847
return $this->getBuilder()->withCount('databases')->with('node')->get();
4948
}
5049

50+
/**
51+
* {@inheritdoc}
52+
*/
5153
public function getWithServers($id)
5254
{
5355
Assert::numeric($id, 'First argument passed to getWithServers must be numeric, recieved %s.');
@@ -59,23 +61,4 @@ public function getWithServers($id)
5961

6062
return $instance;
6163
}
62-
63-
/**
64-
* {@inheritdoc}
65-
*/
66-
public function deleteIfNoDatabases($id)
67-
{
68-
Assert::numeric($id, 'First argument passed to deleteIfNoDatabases must be numeric, recieved %s.');
69-
70-
$instance = $this->getBuilder()->withCount('databases')->find($id);
71-
if (! $instance) {
72-
throw new RecordNotFoundException();
73-
}
74-
75-
if ($instance->databases_count > 0) {
76-
throw new DisplayException('Cannot delete a database host that has active databases attached to it.');
77-
}
78-
79-
return $instance->delete();
80-
}
8164
}

app/Repositories/Eloquent/DatabaseRepository.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424

2525
namespace Pterodactyl\Repositories\Eloquent;
2626

27+
use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
2728
use Pterodactyl\Models\Database;
2829
use Illuminate\Foundation\Application;
2930
use Illuminate\Database\DatabaseManager;
30-
use Pterodactyl\Exceptions\DisplayException;
3131
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
3232

3333
class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface
@@ -67,13 +67,13 @@ public function model()
6767
public function createIfNotExists(array $data)
6868
{
6969
$instance = $this->getBuilder()->where([
70-
['server_id', $data['server_id']],
71-
['database_host_id', $data['database_host_id']],
72-
['database', $data['database']],
70+
['server_id', '=', array_get($data, 'server_id')],
71+
['database_host_id', '=', array_get($data, 'database_host_id')],
72+
['database', '=', array_get($data, 'database')],
7373
])->count();
7474

7575
if ($instance > 0) {
76-
throw new DisplayException('A database with those details already exists for the specified server.');
76+
throw new DuplicateDatabaseNameException('A database with those details already exists for the specified server.');
7777
}
7878

7979
return $this->create($data);

app/Repositories/Eloquent/LocationRepository.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function model()
4949

5050
/**
5151
* {@inheritdoc}
52+
* @todo remove this, do logic in service
5253
*/
5354
public function deleteIfNoNodes($id)
5455
{

app/Services/Database/DatabaseHostService.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
use Illuminate\Database\DatabaseManager;
2828
use Illuminate\Contracts\Encryption\Encrypter;
29+
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
30+
use Pterodactyl\Exceptions\DisplayException;
2931
use Pterodactyl\Extensions\DynamicDatabaseConnection;
3032
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
3133

@@ -36,6 +38,11 @@ class DatabaseHostService
3638
*/
3739
protected $database;
3840

41+
/**
42+
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
43+
*/
44+
protected $databaseRepository;
45+
3946
/**
4047
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
4148
*/
@@ -55,17 +62,20 @@ class DatabaseHostService
5562
* DatabaseHostService constructor.
5663
*
5764
* @param \Illuminate\Database\DatabaseManager $database
65+
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
5866
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
5967
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
6068
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
6169
*/
6270
public function __construct(
6371
DatabaseManager $database,
72+
DatabaseRepositoryInterface $databaseRepository,
6473
DatabaseHostRepositoryInterface $repository,
6574
DynamicDatabaseConnection $dynamic,
6675
Encrypter $encrypter
6776
) {
6877
$this->database = $database;
78+
$this->databaseRepository = $databaseRepository;
6979
$this->dynamic = $dynamic;
7080
$this->encrypter = $encrypter;
7181
$this->repository = $repository;
@@ -111,6 +121,7 @@ public function create(array $data)
111121
* @return mixed
112122
*
113123
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
124+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
114125
*/
115126
public function update($id, array $data)
116127
{
@@ -142,6 +153,11 @@ public function update($id, array $data)
142153
*/
143154
public function delete($id)
144155
{
145-
return $this->repository->deleteIfNoDatabases($id);
156+
$count = $this->databaseRepository->findCountWhere([['database_host_id', '=', $id]]);
157+
if ($count > 0) {
158+
throw new DisplayException(trans('admin/exceptions.databases.delete_has_databases'));
159+
}
160+
161+
return $this->repository->delete($id);
146162
}
147163
}

database/factories/ModelFactory.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191

9292
$factory->define(Pterodactyl\Models\Service::class, function (Faker\Generator $faker) {
9393
return [
94+
'id' => $faker->unique()->randomNumber(),
9495
'author' => $faker->unique()->uuid,
9596
'name' => $faker->word,
9697
'description' => null,
@@ -155,3 +156,24 @@
155156
'daemonSecret' => $faker->unique()->uuid,
156157
];
157158
});
159+
160+
$factory->define(Pterodactyl\Models\Allocation::class, function (Faker\Generator $faker) {
161+
return [
162+
'id' => $faker->unique()->randomNumber(),
163+
'node_id' => $faker->randomNumber(),
164+
'ip' => $faker->ipv4,
165+
'port' => $faker->randomNumber(5),
166+
];
167+
});
168+
169+
$factory->define(Pterodactyl\Models\DatabaseHost::class, function (Faker\Generator $faker) {
170+
return [
171+
'id' => $faker->unique()->randomNumber(),
172+
'name' => $faker->colorName,
173+
'host' => $faker->unique()->ipv4,
174+
'port' => 3306,
175+
'username' => $faker->colorName,
176+
'password' => Crypt::encrypt($faker->word),
177+
'node_id' => $faker->randomNumber(),
178+
];
179+
});

0 commit comments

Comments
 (0)