Skip to content

Commit 83efb2d

Browse files
committed
More fixes for broken unit tests
1 parent b9eb87d commit 83efb2d

File tree

10 files changed

+56
-213
lines changed

10 files changed

+56
-213
lines changed

app/Http/Controllers/Admin/ServersController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public function toggleInstall(Server $server)
252252
*/
253253
public function reinstallServer(Server $server)
254254
{
255-
$this->reinstallService->reinstall($server);
255+
$this->reinstallService->handle($server);
256256
$this->alert->success(trans('admin/server.alerts.server_reinstalled'))->flash();
257257

258258
return redirect()->route('admin.servers.view.manage', $server->id);

app/Http/Controllers/Api/Application/Servers/ServerManagementController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function unsuspend(ServerWriteRequest $request, Server $server): Response
8282
*/
8383
public function reinstall(ServerWriteRequest $request, Server $server): Response
8484
{
85-
$this->reinstallServerService->reinstall($server);
85+
$this->reinstallServerService->handle($server);
8686

8787
return $this->returnNoContent();
8888
}

app/Http/Controllers/Api/Client/Servers/SettingsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function rename(RenameServerRequest $request, Server $server)
6969
*/
7070
public function reinstall(ReinstallServerRequest $request, Server $server)
7171
{
72-
$this->reinstallServerService->reinstall($server);
72+
$this->reinstallServerService->handle($server);
7373

7474
return new JsonResponse([], Response::HTTP_ACCEPTED);
7575
}

app/Services/Servers/EnvironmentService.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use Pterodactyl\Models\Server;
66
use Pterodactyl\Models\EggVariable;
7-
use Illuminate\Contracts\Config\Repository as ConfigRepository;
8-
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
97

108
class EnvironmentService
119
{
@@ -14,28 +12,6 @@ class EnvironmentService
1412
*/
1513
private $additional = [];
1614

17-
/**
18-
* @var \Illuminate\Contracts\Config\Repository
19-
*/
20-
private $config;
21-
22-
/**
23-
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
24-
*/
25-
private $repository;
26-
27-
/**
28-
* EnvironmentService constructor.
29-
*
30-
* @param \Illuminate\Contracts\Config\Repository $config
31-
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
32-
*/
33-
public function __construct(ConfigRepository $config, ServerRepositoryInterface $repository)
34-
{
35-
$this->config = $config;
36-
$this->repository = $repository;
37-
}
38-
3915
/**
4016
* Dynamically configure additional environment variables to be assigned
4117
* with a specific server.
@@ -79,7 +55,7 @@ public function handle(Server $server): array
7955
}
8056

8157
// Process variables set in the configuration file.
82-
foreach ($this->config->get('pterodactyl.environment_variables', []) as $key => $object) {
58+
foreach (config('pterodactyl.environment_variables', []) as $key => $object) {
8359
$variables->put(
8460
$key, is_callable($object) ? call_user_func($object, $server) : object_get($server, $object)
8561
);

app/Services/Servers/ReinstallServerService.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,18 @@ class ReinstallServerService
1919
*/
2020
private $connection;
2121

22-
/**
23-
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
24-
*/
25-
private $repository;
26-
2722
/**
2823
* ReinstallService constructor.
2924
*
3025
* @param \Illuminate\Database\ConnectionInterface $connection
3126
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
32-
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
3327
*/
3428
public function __construct(
3529
ConnectionInterface $connection,
36-
DaemonServerRepository $daemonServerRepository,
37-
ServerRepository $repository
30+
DaemonServerRepository $daemonServerRepository
3831
) {
3932
$this->daemonServerRepository = $daemonServerRepository;
4033
$this->connection = $connection;
41-
$this->repository = $repository;
4234
}
4335

4436
/**
@@ -49,16 +41,14 @@ public function __construct(
4941
*
5042
* @throws \Throwable
5143
*/
52-
public function reinstall(Server $server)
44+
public function handle(Server $server)
5345
{
5446
return $this->connection->transaction(function () use ($server) {
55-
$updated = $this->repository->update($server->id, [
56-
'installed' => Server::STATUS_INSTALLING,
57-
], true, true);
47+
$server->forceFill(['installed' => Server::STATUS_INSTALLING])->save();
5848

5949
$this->daemonServerRepository->setServer($server)->reinstall();
6050

61-
return $updated;
51+
return $server->refresh();
6252
});
6353
}
6454
}

app/Services/Servers/ServerConfigurationStructureService.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Services\Servers;
114

125
use Pterodactyl\Models\Mount;
136
use Pterodactyl\Models\Server;
14-
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
157

168
class ServerConfigurationStructureService
179
{
@@ -22,22 +14,13 @@ class ServerConfigurationStructureService
2214
*/
2315
private $environment;
2416

25-
/**
26-
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
27-
*/
28-
private $repository;
29-
3017
/**
3118
* ServerConfigurationStructureService constructor.
3219
*
33-
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
3420
* @param \Pterodactyl\Services\Servers\EnvironmentService $environment
3521
*/
36-
public function __construct(
37-
ServerRepositoryInterface $repository,
38-
EnvironmentService $environment
39-
) {
40-
$this->repository = $repository;
22+
public function __construct(EnvironmentService $environment)
23+
{
4124
$this->environment = $environment;
4225
}
4326

@@ -112,8 +95,6 @@ protected function returnCurrentFormat(Server $server)
11295
*
11396
* @param \Pterodactyl\Models\Server $server
11497
* @return array
115-
*
116-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
11798
*/
11899
protected function returnLegacyFormat(Server $server)
119100
{

tests/Unit/Services/Servers/EnvironmentServiceTest.php

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,13 @@
66
use Tests\TestCase;
77
use Pterodactyl\Models\Server;
88
use Pterodactyl\Models\Location;
9-
use Illuminate\Contracts\Config\Repository;
9+
use Illuminate\Support\Collection;
10+
use Pterodactyl\Models\EggVariable;
1011
use Pterodactyl\Services\Servers\EnvironmentService;
1112
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
1213

1314
class EnvironmentServiceTest extends TestCase
1415
{
15-
const CONFIG_MAPPING = 'pterodactyl.environment_variables';
16-
17-
/**
18-
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
19-
*/
20-
private $config;
21-
2216
/**
2317
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
2418
*/
@@ -30,9 +24,7 @@ class EnvironmentServiceTest extends TestCase
3024
public function setUp(): void
3125
{
3226
parent::setUp();
33-
34-
$this->config = m::mock(Repository::class);
35-
$this->repository = m::mock(ServerRepositoryInterface::class);
27+
config()->set('pterodactyl.environment_variables', []);
3628
}
3729

3830
/**
@@ -55,15 +47,17 @@ public function testSettingEnvironmentKeyPersistsItInArray()
5547
*/
5648
public function testProcessShouldReturnDefaultEnvironmentVariablesForAServer()
5749
{
58-
$model = $this->getServerModel();
59-
$this->config->shouldReceive('get')->with(self::CONFIG_MAPPING, [])->once()->andReturn([]);
60-
$this->repository->shouldReceive('getVariablesWithValues')->with($model->id)->once()->andReturn([
61-
'TEST_VARIABLE' => 'Test Variable',
50+
$model = $this->getServerModel([
51+
'TEST_VARIABLE' => factory(EggVariable::class)->make([
52+
'id' => 987,
53+
'env_variable' => 'TEST_VARIABLE',
54+
'default_value' => 'Test Variable',
55+
]),
6256
]);
6357

6458
$response = $this->getService()->handle($model);
6559
$this->assertNotEmpty($response);
66-
$this->assertEquals(4, count($response));
60+
$this->assertCount(4, $response);
6761
$this->assertArrayHasKey('TEST_VARIABLE', $response);
6862
$this->assertSame('Test Variable', $response['TEST_VARIABLE']);
6963
}
@@ -73,10 +67,7 @@ public function testProcessShouldReturnDefaultEnvironmentVariablesForAServer()
7367
*/
7468
public function testProcessShouldReturnKeySetAtRuntime()
7569
{
76-
$model = $this->getServerModel();
77-
$this->config->shouldReceive('get')->with(self::CONFIG_MAPPING, [])->once()->andReturn([]);
78-
$this->repository->shouldReceive('getVariablesWithValues')->with($model->id)->once()->andReturn([]);
79-
70+
$model = $this->getServerModel([]);
8071
$service = $this->getService();
8172
$service->setEnvironmentKey('TEST_VARIABLE', function ($server) {
8273
return $server->uuidShort;
@@ -94,12 +85,11 @@ public function testProcessShouldReturnKeySetAtRuntime()
9485
*/
9586
public function testProcessShouldAllowOverwritingVariablesWithConfigurationFile()
9687
{
97-
$model = $this->getServerModel();
98-
$this->repository->shouldReceive('getVariablesWithValues')->with($model->id)->once()->andReturn([]);
99-
$this->config->shouldReceive('get')->with(self::CONFIG_MAPPING, [])->once()->andReturn([
88+
config()->set('pterodactyl.environment_variables', [
10089
'P_SERVER_UUID' => 'name',
10190
]);
10291

92+
$model = $this->getServerModel([]);
10393
$response = $this->getService()->handle($model);
10494

10595
$this->assertNotEmpty($response);
@@ -113,14 +103,13 @@ public function testProcessShouldAllowOverwritingVariablesWithConfigurationFile(
113103
*/
114104
public function testVariablesSetInConfigurationAllowForClosures()
115105
{
116-
$model = $this->getServerModel();
117-
$this->config->shouldReceive('get')->with(self::CONFIG_MAPPING, [])->once()->andReturn([
106+
config()->set('pterodactyl.environment_variables', [
118107
'P_SERVER_UUID' => function ($server) {
119108
return $server->id * 2;
120109
},
121110
]);
122-
$this->repository->shouldReceive('getVariablesWithValues')->with($model->id)->once()->andReturn([]);
123111

112+
$model = $this->getServerModel([]);
124113
$response = $this->getService()->handle($model);
125114

126115
$this->assertNotEmpty($response);
@@ -135,12 +124,11 @@ public function testVariablesSetInConfigurationAllowForClosures()
135124
*/
136125
public function testProcessShouldAllowOverwritingDefaultVariablesWithRuntimeProvided()
137126
{
138-
$model = $this->getServerModel();
139-
$this->config->shouldReceive('get')->with(self::CONFIG_MAPPING, [])->once()->andReturn([
127+
config()->set('pterodactyl.environment_variables', [
140128
'P_SERVER_UUID' => 'overwritten-config',
141129
]);
142-
$this->repository->shouldReceive('getVariablesWithValues')->with($model->id)->once()->andReturn([]);
143130

131+
$model = $this->getServerModel([]);
144132
$service = $this->getService();
145133
$service->setEnvironmentKey('P_SERVER_UUID', function ($model) {
146134
return 'overwritten';
@@ -161,18 +149,25 @@ public function testProcessShouldAllowOverwritingDefaultVariablesWithRuntimeProv
161149
*/
162150
private function getService(): EnvironmentService
163151
{
164-
return new EnvironmentService($this->config, $this->repository);
152+
return new EnvironmentService;
165153
}
166154

167155
/**
168156
* Return a server model with a location relationship to be used in the tests.
169157
*
158+
* @param array $variables
170159
* @return \Pterodactyl\Models\Server
171160
*/
172-
private function getServerModel(): Server
161+
private function getServerModel(array $variables): Server
173162
{
174-
return factory(Server::class)->make([
163+
/** @var \Pterodactyl\Models\Server $server */
164+
$server = factory(Server::class)->make([
165+
'id' => 123,
175166
'location' => factory(Location::class)->make(),
176167
]);
168+
169+
$server->setRelation('variables', Collection::make($variables));
170+
171+
return $server;
177172
}
178173
}

0 commit comments

Comments
 (0)