misc_pterodactyl-panel/tests/Unit/Services/Servers/ServerDeletionServiceTest.php

157 lines
5.7 KiB
PHP
Raw Normal View History

2017-08-13 19:55:09 +00:00
<?php
namespace Tests\Unit\Services\Servers;
use Mockery as m;
2017-08-16 04:21:01 +00:00
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
2017-08-16 04:21:01 +00:00
use Pterodactyl\Models\Server;
use Tests\Traits\MocksRequestException;
2017-08-13 19:55:09 +00:00
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Servers\ServerDeletionService;
use Pterodactyl\Services\Databases\DatabaseManagementService;
2017-08-16 04:21:01 +00:00
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
2017-08-13 19:55:09 +00:00
class ServerDeletionServiceTest extends TestCase
2017-08-13 19:55:09 +00:00
{
use MocksRequestException;
2017-08-13 19:55:09 +00:00
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
2017-08-13 19:55:09 +00:00
*/
private $connection;
2017-08-13 19:55:09 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
2017-08-13 19:55:09 +00:00
*/
private $daemonServerRepository;
2017-08-13 19:55:09 +00:00
/**
* @var \Pterodactyl\Services\Databases\DatabaseManagementService|\Mockery\Mock
2017-08-13 19:55:09 +00:00
*/
private $databaseManagementService;
2017-08-13 19:55:09 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock
2017-08-13 19:55:09 +00:00
*/
private $databaseRepository;
2017-08-13 19:55:09 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
2017-08-13 19:55:09 +00:00
*/
private $repository;
2017-08-13 19:55:09 +00:00
/**
* @var \Illuminate\Log\Writer|\Mockery\Mock
2017-08-13 19:55:09 +00:00
*/
private $writer;
2017-08-13 19:55:09 +00:00
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
$this->databaseRepository = m::mock(DatabaseRepositoryInterface::class);
$this->databaseManagementService = m::mock(DatabaseManagementService::class);
$this->repository = m::mock(ServerRepositoryInterface::class);
$this->writer = m::mock(Writer::class);
}
/**
* Test that a server can be force deleted by setting it in a function call.
*/
public function testForceParameterCanBeSet()
{
$response = $this->getService()->withForce(true);
2017-08-13 19:55:09 +00:00
$this->assertInstanceOf(ServerDeletionService::class, $response);
2017-08-13 19:55:09 +00:00
}
/**
* Test that a server can be deleted when force is not set.
*/
public function testServerCanBeDeletedWithoutForce()
{
$model = factory(Server::class)->make();
2017-08-13 19:55:09 +00:00
$this->daemonServerRepository->shouldReceive('setServer')->once()->with($model)->andReturnSelf();
$this->daemonServerRepository->shouldReceive('delete')->once()->withNoArgs()->andReturn(new Response);
2017-08-13 19:55:09 +00:00
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
$this->databaseRepository->shouldReceive('setColumns')->once()->with('id')->andReturnSelf();
$this->databaseRepository->shouldReceive('findWhere')->once()->with([
['server_id', '=', $model->id],
])->andReturn(collect([(object) ['id' => 50]]));
2017-08-13 19:55:09 +00:00
$this->databaseManagementService->shouldReceive('delete')->once()->with(50)->andReturnNull();
$this->repository->shouldReceive('delete')->once()->with($model->id)->andReturn(1);
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
$this->getService()->handle($model);
2017-08-13 19:55:09 +00:00
}
/**
* Test that a server is deleted when force is set.
*/
public function testServerShouldBeDeletedEvenWhenFailureOccursIfForceIsSet()
{
$this->configureExceptionMock();
$model = factory(Server::class)->make();
$this->daemonServerRepository->shouldReceive('setServer')->once()->with($model)->andReturnSelf();
$this->daemonServerRepository->shouldReceive('delete')->once()->withNoArgs()->andThrow($this->getExceptionMock());
2017-08-13 19:55:09 +00:00
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf();
$this->databaseRepository->shouldReceive('findWhere')->with([
['server_id', '=', $model->id],
])->once()->andReturn(collect([(object) ['id' => 50]]));
2017-08-13 19:55:09 +00:00
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturn(1);
2017-08-13 19:55:09 +00:00
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->getService()->withForce()->handle($model);
2017-08-13 19:55:09 +00:00
}
/**
* Test that an exception is thrown if a server cannot be deleted from the node and force is not set.
*
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
2017-08-13 19:55:09 +00:00
*/
public function testExceptionShouldBeThrownIfDaemonReturnsAnErrorAndForceIsNotSet()
{
$this->configureExceptionMock();
$model = factory(Server::class)->make();
2017-08-13 19:55:09 +00:00
$this->daemonServerRepository->shouldReceive('setServer->delete')->once()->andThrow($this->getExceptionMock());
$this->getService()->handle($model);
2017-08-13 19:55:09 +00:00
}
/**
* Return an instance of the class with mocked dependencies.
*
* @return \Pterodactyl\Services\Servers\ServerDeletionService
2017-08-13 19:55:09 +00:00
*/
private function getService(): ServerDeletionService
2017-08-13 19:55:09 +00:00
{
return new ServerDeletionService(
$this->connection,
$this->daemonServerRepository,
$this->databaseRepository,
$this->databaseManagementService,
$this->repository,
$this->writer
);
2017-08-13 19:55:09 +00:00
}
}