Begin unit tests for repositories
This commit is contained in:
parent
72735c24f7
commit
f451e4dc47
18 changed files with 734 additions and 43 deletions
|
@ -42,15 +42,4 @@ interface DatabaseHostRepositoryInterface extends RepositoryInterface
|
|||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function getWithServers($id);
|
||||
|
||||
/**
|
||||
* Delete a database host from the DB if there are no databases using it.
|
||||
*
|
||||
* @param int $id
|
||||
* @return bool|null
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function deleteIfNoDatabases($id);
|
||||
}
|
||||
|
|
31
app/Exceptions/Repository/DuplicateDatabaseNameException.php
Normal file
31
app/Exceptions/Repository/DuplicateDatabaseNameException.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Pterodactyl\Exceptions\Repository;
|
||||
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
|
||||
class DuplicateDatabaseNameException extends DisplayException
|
||||
{
|
||||
}
|
|
@ -83,6 +83,8 @@ class LocationController extends Controller
|
|||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\View\View
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function view($id)
|
||||
{
|
||||
|
|
|
@ -434,6 +434,7 @@ class ServersController extends Controller
|
|||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function toggleInstall(Server $server)
|
||||
{
|
||||
|
@ -493,6 +494,7 @@ class ServersController extends Controller
|
|||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function manageSuspension(Request $request, Server $server)
|
||||
{
|
||||
|
@ -533,6 +535,7 @@ class ServersController extends Controller
|
|||
* @return \Illuminate\Http\RedirectResponse
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function delete(Request $request, Server $server)
|
||||
{
|
||||
|
@ -551,6 +554,7 @@ class ServersController extends Controller
|
|||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function saveStartup(Request $request, Server $server)
|
||||
{
|
||||
|
|
|
@ -25,7 +25,13 @@
|
|||
namespace Pterodactyl\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
|
||||
use Pterodactyl\Repositories\Daemon\CommandRepository;
|
||||
use Pterodactyl\Repositories\Daemon\FileRepository;
|
||||
use Pterodactyl\Repositories\Daemon\PowerRepository;
|
||||
use Pterodactyl\Repositories\Eloquent\NodeRepository;
|
||||
use Pterodactyl\Repositories\Eloquent\PackRepository;
|
||||
use Pterodactyl\Repositories\Eloquent\UserRepository;
|
||||
|
@ -71,8 +77,8 @@ class RepositoryServiceProvider extends ServiceProvider
|
|||
$this->app->bind(AllocationRepositoryInterface::class, AllocationRepository::class);
|
||||
$this->app->bind(ApiKeyRepositoryInterface::class, ApiKeyRepository::class);
|
||||
$this->app->bind(ApiPermissionRepositoryInterface::class, ApiPermissionRepository::class);
|
||||
$this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class);
|
||||
$this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class);
|
||||
$this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class);
|
||||
$this->app->bind(LocationRepositoryInterface::class, LocationRepository::class);
|
||||
$this->app->bind(NodeRepositoryInterface::class, NodeRepository::class);
|
||||
$this->app->bind(OptionVariableRepositoryInterface::class, OptionVariableRepository::class);
|
||||
|
@ -86,6 +92,9 @@ class RepositoryServiceProvider extends ServiceProvider
|
|||
|
||||
// Daemon Repositories
|
||||
$this->app->bind(ConfigurationRepositoryInterface::class, ConfigurationRepository::class);
|
||||
$this->app->bind(CommandRepositoryInterface::class, CommandRepository::class);
|
||||
$this->app->bind(DaemonServerRepositoryInterface::class, DaemonServerRepository::class);
|
||||
$this->app->bind(FileRepositoryInterface::class, FileRepository::class);
|
||||
$this->app->bind(PowerRepositoryInterface::class, PowerRepository::class);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@ namespace Pterodactyl\Repositories\Eloquent;
|
|||
|
||||
use Webmozart\Assert\Assert;
|
||||
use Pterodactyl\Models\DatabaseHost;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
|
||||
|
||||
|
@ -48,6 +47,9 @@ class DatabaseHostRepository extends EloquentRepository implements DatabaseHostR
|
|||
return $this->getBuilder()->withCount('databases')->with('node')->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getWithServers($id)
|
||||
{
|
||||
Assert::numeric($id, 'First argument passed to getWithServers must be numeric, recieved %s.');
|
||||
|
@ -59,23 +61,4 @@ class DatabaseHostRepository extends EloquentRepository implements DatabaseHostR
|
|||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function deleteIfNoDatabases($id)
|
||||
{
|
||||
Assert::numeric($id, 'First argument passed to deleteIfNoDatabases must be numeric, recieved %s.');
|
||||
|
||||
$instance = $this->getBuilder()->withCount('databases')->find($id);
|
||||
if (! $instance) {
|
||||
throw new RecordNotFoundException();
|
||||
}
|
||||
|
||||
if ($instance->databases_count > 0) {
|
||||
throw new DisplayException('Cannot delete a database host that has active databases attached to it.');
|
||||
}
|
||||
|
||||
return $instance->delete();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,10 +24,10 @@
|
|||
|
||||
namespace Pterodactyl\Repositories\Eloquent;
|
||||
|
||||
use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
|
||||
use Pterodactyl\Models\Database;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
||||
|
||||
class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface
|
||||
|
@ -67,13 +67,13 @@ class DatabaseRepository extends EloquentRepository implements DatabaseRepositor
|
|||
public function createIfNotExists(array $data)
|
||||
{
|
||||
$instance = $this->getBuilder()->where([
|
||||
['server_id', $data['server_id']],
|
||||
['database_host_id', $data['database_host_id']],
|
||||
['database', $data['database']],
|
||||
['server_id', '=', array_get($data, 'server_id')],
|
||||
['database_host_id', '=', array_get($data, 'database_host_id')],
|
||||
['database', '=', array_get($data, 'database')],
|
||||
])->count();
|
||||
|
||||
if ($instance > 0) {
|
||||
throw new DisplayException('A database with those details already exists for the specified server.');
|
||||
throw new DuplicateDatabaseNameException('A database with those details already exists for the specified server.');
|
||||
}
|
||||
|
||||
return $this->create($data);
|
||||
|
|
|
@ -49,6 +49,7 @@ class LocationRepository extends EloquentRepository implements LocationRepositor
|
|||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @todo remove this, do logic in service
|
||||
*/
|
||||
public function deleteIfNoNodes($id)
|
||||
{
|
||||
|
|
|
@ -26,6 +26,8 @@ namespace Pterodactyl\Services\Database;
|
|||
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Illuminate\Contracts\Encryption\Encrypter;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Extensions\DynamicDatabaseConnection;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
|
||||
|
||||
|
@ -36,6 +38,11 @@ class DatabaseHostService
|
|||
*/
|
||||
protected $database;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
||||
*/
|
||||
protected $databaseRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
|
||||
*/
|
||||
|
@ -55,17 +62,20 @@ class DatabaseHostService
|
|||
* DatabaseHostService constructor.
|
||||
*
|
||||
* @param \Illuminate\Database\DatabaseManager $database
|
||||
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
|
||||
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
|
||||
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
|
||||
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
||||
*/
|
||||
public function __construct(
|
||||
DatabaseManager $database,
|
||||
DatabaseRepositoryInterface $databaseRepository,
|
||||
DatabaseHostRepositoryInterface $repository,
|
||||
DynamicDatabaseConnection $dynamic,
|
||||
Encrypter $encrypter
|
||||
) {
|
||||
$this->database = $database;
|
||||
$this->databaseRepository = $databaseRepository;
|
||||
$this->dynamic = $dynamic;
|
||||
$this->encrypter = $encrypter;
|
||||
$this->repository = $repository;
|
||||
|
@ -111,6 +121,7 @@ class DatabaseHostService
|
|||
* @return mixed
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function update($id, array $data)
|
||||
{
|
||||
|
@ -142,6 +153,11 @@ class DatabaseHostService
|
|||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
return $this->repository->deleteIfNoDatabases($id);
|
||||
$count = $this->databaseRepository->findCountWhere([['database_host_id', '=', $id]]);
|
||||
if ($count > 0) {
|
||||
throw new DisplayException(trans('admin/exceptions.databases.delete_has_databases'));
|
||||
}
|
||||
|
||||
return $this->repository->delete($id);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue