Skip to content

Commit b557674

Browse files
committed
Update some server service tests
1 parent 83a59cd commit b557674

File tree

6 files changed

+114
-162
lines changed

6 files changed

+114
-162
lines changed

app/Services/Servers/ReinstallServerService.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use Pterodactyl\Models\Server;
66
use Illuminate\Database\ConnectionInterface;
7+
use Pterodactyl\Repositories\Eloquent\ServerRepository;
78
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
8-
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
99

1010
class ReinstallServerService
1111
{
@@ -17,27 +17,27 @@ class ReinstallServerService
1717
/**
1818
* @var \Illuminate\Database\ConnectionInterface
1919
*/
20-
private $database;
20+
private $connection;
2121

2222
/**
23-
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
23+
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
2424
*/
2525
private $repository;
2626

2727
/**
2828
* ReinstallService constructor.
2929
*
30-
* @param \Illuminate\Database\ConnectionInterface $database
30+
* @param \Illuminate\Database\ConnectionInterface $connection
3131
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
32-
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
32+
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
3333
*/
3434
public function __construct(
35-
ConnectionInterface $database,
35+
ConnectionInterface $connection,
3636
DaemonServerRepository $daemonServerRepository,
37-
ServerRepositoryInterface $repository
37+
ServerRepository $repository
3838
) {
3939
$this->daemonServerRepository = $daemonServerRepository;
40-
$this->database = $database;
40+
$this->connection = $connection;
4141
$this->repository = $repository;
4242
}
4343

@@ -51,14 +51,14 @@ public function __construct(
5151
*/
5252
public function reinstall(Server $server)
5353
{
54-
$this->database->transaction(function () use ($server) {
55-
$this->repository->withoutFreshModel()->update($server->id, [
54+
return $this->connection->transaction(function () use ($server) {
55+
$updated = $this->repository->update($server->id, [
5656
'installed' => Server::STATUS_INSTALLING,
5757
]);
5858

5959
$this->daemonServerRepository->setServer($server)->reinstall();
60-
});
6160

62-
return $server->refresh();
61+
return $updated;
62+
});
6363
}
6464
}

app/Services/Servers/ServerCreationService.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,16 +310,13 @@ private function getNodeFromAllocation(int $id): int
310310
return $allocation->node_id;
311311
}
312312

313-
/** @noinspection PhpDocMissingThrowsInspection */
314-
315313
/**
316314
* Create a unique UUID and UUID-Short combo for a server.
317315
*
318316
* @return string
319317
*/
320318
private function generateUniqueUuidCombo(): string
321319
{
322-
/** @noinspection PhpUnhandledExceptionInspection */
323320
$uuid = Uuid::uuid4()->toString();
324321

325322
if (! $this->repository->isUniqueUuidCombo($uuid, substr($uuid, 0, 8))) {

tests/Unit/Services/Packs/ExportPackServiceTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Illuminate\Contracts\Filesystem\Factory;
1818
use Pterodactyl\Services\Packs\ExportPackService;
1919
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
20+
use Pterodactyl\Exceptions\Service\Pack\ZipArchiveCreationException;
2021

2122
class ExportPackServiceTest extends TestCase
2223
{
@@ -132,11 +133,11 @@ public function testPackIdCanBePassedInPlaceOfModel()
132133

133134
/**
134135
* Test that an exception is thrown when a ZipArchive cannot be created.
135-
*
136-
* @expectedException \Pterodactyl\Exceptions\Service\Pack\ZipArchiveCreationException
137136
*/
138137
public function testExceptionIsThrownIfZipArchiveCannotBeCreated()
139138
{
139+
$this->expectException(ZipArchiveCreationException::class);
140+
140141
$this->setupTestData();
141142

142143
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')

tests/Unit/Services/Servers/ReinstallServerServiceTest.php

Lines changed: 27 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,30 @@
99

1010
namespace Tests\Unit\Services\Servers;
1111

12-
use Exception;
1312
use Mockery as m;
1413
use Tests\TestCase;
15-
use GuzzleHttp\Psr7\Response;
1614
use Pterodactyl\Models\Server;
17-
use GuzzleHttp\Exception\RequestException;
1815
use Illuminate\Database\ConnectionInterface;
16+
use Pterodactyl\Repositories\Eloquent\ServerRepository;
1917
use Pterodactyl\Services\Servers\ReinstallServerService;
20-
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
21-
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
18+
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
2219

2320
class ReinstallServerServiceTest extends TestCase
2421
{
2522
/**
26-
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
23+
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
2724
*/
28-
protected $daemonServerRepository;
25+
private $daemonServerRepository;
2926

3027
/**
3128
* @var \Illuminate\Database\ConnectionInterface
3229
*/
33-
protected $database;
34-
35-
/**
36-
* @var \GuzzleHttp\Exception\RequestException
37-
*/
38-
protected $exception;
30+
private $connection;
3931

4032
/**
4133
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
4234
*/
43-
protected $repository;
44-
45-
/**
46-
* @var \Pterodactyl\Models\Server
47-
*/
48-
protected $server;
49-
50-
/**
51-
* @var \Pterodactyl\Services\Servers\ReinstallServerService
52-
*/
53-
protected $service;
35+
private $repository;
5436

5537
/**
5638
* Setup tests.
@@ -59,89 +41,42 @@ public function setUp(): void
5941
{
6042
parent::setUp();
6143

62-
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
63-
$this->database = m::mock(ConnectionInterface::class);
64-
$this->exception = m::mock(RequestException::class)->makePartial();
65-
$this->repository = m::mock(ServerRepositoryInterface::class);
66-
67-
$this->server = factory(Server::class)->make(['node_id' => 1]);
68-
69-
$this->service = new ReinstallServerService(
70-
$this->database,
71-
$this->daemonServerRepository,
72-
$this->repository
73-
);
44+
$this->repository = m::mock(ServerRepository::class);
45+
$this->connection = m::mock(ConnectionInterface::class);
46+
$this->daemonServerRepository = m::mock(DaemonServerRepository::class);
7447
}
7548

7649
/**
7750
* Test that a server is reinstalled when it's model is passed to the function.
7851
*/
7952
public function testServerShouldBeReinstalledWhenModelIsPassed()
8053
{
81-
$this->repository->shouldNotReceive('find');
54+
/** @var \Pterodactyl\Models\Server $server */
55+
$server = factory(Server::class)->make(['id' => 123]);
56+
$updated = clone $server;
57+
$updated->installed = Server::STATUS_INSTALLING;
8258

83-
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
84-
$this->repository->shouldReceive('withoutFreshModel->update')->with($this->server->id, [
85-
'installed' => 0,
86-
], true, true)->once()->andReturnNull();
59+
$this->connection->expects('transaction')->with(m::on(function ($closure) use ($updated) {
60+
return $closure() instanceof Server;
61+
}))->andReturn($updated);
8762

88-
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
89-
->shouldReceive('reinstall')->withNoArgs()->once()->andReturn(new Response);
90-
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
63+
$this->repository->expects('update')->with($server->id, [
64+
'installed' => Server::STATUS_INSTALLING,
65+
])->andReturns($updated);
9166

92-
$this->service->reinstall($this->server);
93-
}
67+
$this->daemonServerRepository->expects('setServer')->with($server)->andReturnSelf();
68+
$this->daemonServerRepository->expects('reinstall')->withNoArgs();
9469

95-
/**
96-
* Test that a server is reinstalled when the ID of the server is passed to the function.
97-
*/
98-
public function testServerShouldBeReinstalledWhenServerIdIsPassed()
99-
{
100-
$this->repository->shouldReceive('find')->with($this->server->id)->once()->andReturn($this->server);
101-
102-
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
103-
$this->repository->shouldReceive('withoutFreshModel->update')->with($this->server->id, [
104-
'installed' => 0,
105-
], true, true)->once()->andReturnNull();
106-
107-
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
108-
->shouldReceive('reinstall')->withNoArgs()->once()->andReturn(new Response);
109-
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
110-
111-
$this->service->reinstall($this->server->id);
112-
}
113-
114-
/**
115-
* Test that an exception thrown by guzzle is rendered as a displayable exception.
116-
*
117-
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
118-
*/
119-
public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable()
120-
{
121-
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
122-
$this->repository->shouldReceive('withoutFreshModel->update')->with($this->server->id, [
123-
'installed' => 0,
124-
], true, true)->once()->andReturnNull();
125-
126-
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andThrow($this->exception);
127-
128-
$this->service->reinstall($this->server);
70+
$this->assertSame($updated, $this->getService()->reinstall($server));
12971
}
13072

13173
/**
132-
* Test that an exception thrown by something other than guzzle is not transformed to a displayable.
133-
*
134-
* @expectedException \Exception
74+
* @return \Pterodactyl\Services\Servers\ReinstallServerService
13575
*/
136-
public function testExceptionNotThrownByGuzzleShouldNotBeTransformedToDisplayable()
76+
private function getService()
13777
{
138-
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
139-
$this->repository->shouldReceive('withoutFreshModel->update')->with($this->server->id, [
140-
'installed' => 0,
141-
], true, true)->once()->andReturnNull();
142-
143-
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andThrow(new Exception());
144-
145-
$this->service->reinstall($this->server);
78+
return new ReinstallServerService(
79+
$this->connection, $this->daemonServerRepository, $this->repository
80+
);
14681
}
14782
}

tests/Unit/Services/Servers/ServerConfigurationStructureServiceTest.php

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,53 +40,62 @@ public function setUp(): void
4040
*/
4141
public function testCorrectStructureIsReturned()
4242
{
43+
/** @var \Pterodactyl\Models\Server $model */
4344
$model = factory(Server::class)->make();
4445
$model->setRelation('pack', null);
4546
$model->setRelation('allocation', factory(Allocation::class)->make());
4647
$model->setRelation('allocations', collect(factory(Allocation::class)->times(2)->make()));
4748
$model->setRelation('egg', factory(Egg::class)->make());
4849

49-
$portListing = $model->allocations->groupBy('ip')->map(function ($item) {
50-
return $item->pluck('port');
51-
})->toArray();
52-
53-
$this->repository->shouldReceive('getDataForCreation')->with($model)->once()->andReturn($model);
54-
$this->environment->shouldReceive('handle')->with($model)->once()->andReturn(['environment_array']);
50+
$this->environment->expects('handle')->with($model)->andReturn(['environment_array']);
5551

5652
$response = $this->getService()->handle($model);
5753
$this->assertNotEmpty($response);
5854
$this->assertArrayNotHasKey('user', $response);
5955
$this->assertArrayNotHasKey('keys', $response);
56+
6057
$this->assertArrayHasKey('uuid', $response);
58+
$this->assertArrayHasKey('suspended', $response);
59+
$this->assertArrayHasKey('environment', $response);
60+
$this->assertArrayHasKey('invocation', $response);
6161
$this->assertArrayHasKey('build', $response);
6262
$this->assertArrayHasKey('service', $response);
63-
$this->assertArrayHasKey('rebuild', $response);
64-
$this->assertArrayHasKey('suspended', $response);
63+
$this->assertArrayHasKey('container', $response);
64+
$this->assertArrayHasKey('allocations', $response);
6565

66-
$this->assertArraySubset([
66+
$this->assertSame([
6767
'default' => [
6868
'ip' => $model->allocation->ip,
6969
'port' => $model->allocation->port,
7070
],
71-
], $response['build'], true, 'Assert server default allocation is correct.');
72-
$this->assertArraySubset(['ports' => $portListing], $response['build'], true, 'Assert server ports are correct.');
73-
$this->assertArraySubset([
74-
'env' => ['environment_array'],
75-
'swap' => (int) $model->swap,
76-
'io' => (int) $model->io,
77-
'cpu' => (int) $model->cpu,
78-
'disk' => (int) $model->disk,
79-
'image' => $model->image,
80-
], $response['build'], true, 'Assert server build data is correct.');
71+
'mappings' => $model->getAllocationMappings(),
72+
], $response['allocations']);
73+
74+
$this->assertSame([
75+
'memory_limit' => $model->memory,
76+
'swap' => $model->swap,
77+
'io_weight' => $model->io,
78+
'cpu_limit' => $model->cpu,
79+
'threads' => $model->threads,
80+
'disk_space' => $model->disk,
81+
], $response['build']);
8182

82-
$this->assertArraySubset([
83+
$this->assertSame([
8384
'egg' => $model->egg->uuid,
8485
'pack' => null,
8586
'skip_scripts' => $model->skip_scripts,
8687
], $response['service']);
8788

88-
$this->assertFalse($response['rebuild']);
89-
$this->assertSame((int) $model->suspended, $response['suspended']);
89+
$this->assertSame([
90+
'image' => $model->image,
91+
'oom_disabled' => $model->oom_disabled,
92+
'requires_rebuild' => false,
93+
], $response['container']);
94+
95+
$this->assertSame($model->uuid, $response['uuid']);
96+
$this->assertSame((bool) $model->suspended, $response['suspended']);
97+
$this->assertSame(['environment_array'], $response['environment']);
98+
$this->assertSame($model->startup, $response['invocation']);
9099
}
91100

92101
/**

0 commit comments

Comments
 (0)