Refactor how repositories for the daemon work.

This commit is contained in:
Dane Everitt 2018-01-05 18:27:47 -06:00
parent 5f9fe4a69b
commit d2afc29a80
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
58 changed files with 388 additions and 997 deletions

View file

@ -12,11 +12,10 @@ namespace Tests\Unit\Services\Servers;
use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Servers\ReinstallServerService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
@ -53,11 +52,6 @@ class ReinstallServerServiceTest extends TestCase
*/
protected $service;
/**
* @var \Illuminate\Log\Writer
*/
protected $writer;
/**
* Setup tests.
*/
@ -69,15 +63,13 @@ class ReinstallServerServiceTest extends TestCase
$this->database = m::mock(ConnectionInterface::class);
$this->exception = m::mock(RequestException::class)->makePartial();
$this->repository = m::mock(ServerRepositoryInterface::class);
$this->writer = m::mock(Writer::class);
$this->server = factory(Server::class)->make(['node_id' => 1]);
$this->service = new ReinstallServerService(
$this->database,
$this->daemonServerRepository,
$this->repository,
$this->writer
$this->repository
);
}
@ -94,9 +86,8 @@ class ReinstallServerServiceTest extends TestCase
'installed' => 0,
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('reinstall')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('reinstall')->withNoArgs()->once()->andReturn(new Response);
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->reinstall($this->server);
@ -115,9 +106,8 @@ class ReinstallServerServiceTest extends TestCase
'installed' => 0,
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('reinstall')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('reinstall')->withNoArgs()->once()->andReturn(new Response);
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->reinstall($this->server->id);
@ -125,6 +115,8 @@ class ReinstallServerServiceTest extends TestCase
/**
* Test that an exception thrown by guzzle is rendered as a displayable exception.
*
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable()
{
@ -134,23 +126,9 @@ class ReinstallServerServiceTest extends TestCase
'installed' => 0,
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
->once()->andThrow($this->exception);
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
try {
$this->service->reinstall($this->server);
} catch (Exception $exception) {
$this->assertInstanceOf(DisplayException::class, $exception);
$this->assertEquals(
trans('admin/server.exceptions.daemon_exception', ['code' => 400]),
$exception->getMessage()
);
}
$this->service->reinstall($this->server);
}
/**
@ -166,8 +144,7 @@ class ReinstallServerServiceTest extends TestCase
'installed' => 0,
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
->once()->andThrow(new Exception());
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andThrow(new Exception());
$this->service->reinstall($this->server);
}