Skip to content

Commit c59a2c4

Browse files
committed
Don't waste time on a service better suited to an integration test
1 parent 2560163 commit c59a2c4

File tree

3 files changed

+63
-340
lines changed

3 files changed

+63
-340
lines changed

app/Services/Servers/ServerCreationService.php

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Ramsey\Uuid\Uuid;
66
use Illuminate\Support\Arr;
7+
use Pterodactyl\Models\Egg;
78
use Pterodactyl\Models\User;
9+
use Webmozart\Assert\Assert;
810
use Pterodactyl\Models\Server;
911
use Illuminate\Support\Collection;
1012
use Pterodactyl\Models\Allocation;
@@ -13,19 +15,13 @@
1315
use Pterodactyl\Repositories\Eloquent\EggRepository;
1416
use Pterodactyl\Repositories\Eloquent\ServerRepository;
1517
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
16-
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
1718
use Pterodactyl\Services\Deployment\FindViableNodesService;
1819
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
1920
use Pterodactyl\Services\Deployment\AllocationSelectionService;
2021
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
2122

2223
class ServerCreationService
2324
{
24-
/**
25-
* @var \Pterodactyl\Repositories\Eloquent\AllocationRepository
26-
*/
27-
private $allocationRepository;
28-
2925
/**
3026
* @var \Pterodactyl\Services\Deployment\AllocationSelectionService
3127
*/
@@ -79,7 +75,6 @@ class ServerCreationService
7975
/**
8076
* CreationService constructor.
8177
*
82-
* @param \Pterodactyl\Repositories\Eloquent\AllocationRepository $allocationRepository
8378
* @param \Pterodactyl\Services\Deployment\AllocationSelectionService $allocationSelectionService
8479
* @param \Illuminate\Database\ConnectionInterface $connection
8580
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
@@ -92,7 +87,6 @@ class ServerCreationService
9287
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
9388
*/
9489
public function __construct(
95-
AllocationRepository $allocationRepository,
9690
AllocationSelectionService $allocationSelectionService,
9791
ConnectionInterface $connection,
9892
DaemonServerRepository $daemonServerRepository,
@@ -105,7 +99,6 @@ public function __construct(
10599
VariableValidatorService $validatorService
106100
) {
107101
$this->allocationSelectionService = $allocationSelectionService;
108-
$this->allocationRepository = $allocationRepository;
109102
$this->configurationStructureService = $configurationStructureService;
110103
$this->connection = $connection;
111104
$this->findViableNodesService = $findViableNodesService;
@@ -149,14 +142,16 @@ public function handle(array $data, DeploymentObject $deployment = null): Server
149142

150143
// Auto-configure the node based on the selected allocation
151144
// if no node was defined.
152-
if (is_null(Arr::get($data, 'node_id'))) {
153-
$data['node_id'] = $this->getNodeFromAllocation($data['allocation_id']);
145+
if (empty($data['node_id'])) {
146+
Assert::false(empty($data['allocation_id']), 'Expected a non-empty allocation_id in server creation data.');
147+
148+
$data['node_id'] = Allocation::query()->findOrFail($data['allocation_id'])->node_id;
154149
}
155150

156-
if (is_null(Arr::get($data, 'nest_id'))) {
157-
/** @var \Pterodactyl\Models\Egg $egg */
158-
$egg = $this->eggRepository->setColumns(['id', 'nest_id'])->find(Arr::get($data, 'egg_id'));
159-
$data['nest_id'] = $egg->nest_id;
151+
if (empty($data['nest_id'])) {
152+
Assert::false(empty($data['egg_id']), 'Expected a non-empty egg_id in server creation data.');
153+
154+
$data['nest_id'] = Egg::query()->findOrFail($data['egg_id'])->nest_id;
160155
}
161156

162157
$eggVariableData = $this->validatorService
@@ -269,7 +264,7 @@ private function storeAssignedAllocations(Server $server, array $data)
269264
$records = array_merge($records, $data['allocation_additional']);
270265
}
271266

272-
$this->allocationRepository->updateWhereIn('id', $records, [
267+
Allocation::query()->whereIn('id', $records)->update([
273268
'server_id' => $server->id,
274269
]);
275270
}
@@ -295,22 +290,6 @@ private function storeEggVariables(Server $server, Collection $variables)
295290
}
296291
}
297292

298-
/**
299-
* Get the node that an allocation belongs to.
300-
*
301-
* @param int $id
302-
* @return int
303-
*
304-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
305-
*/
306-
private function getNodeFromAllocation(int $id): int
307-
{
308-
/** @var \Pterodactyl\Models\Allocation $allocation */
309-
$allocation = $this->allocationRepository->setColumns(['id', 'node_id'])->find($id);
310-
311-
return $allocation->node_id;
312-
}
313-
314293
/**
315294
* Create a unique UUID and UUID-Short combo for a server.
316295
*
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Tests\Traits;
4+
5+
use PDO;
6+
use Mockery;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\MySqlConnection;
9+
use Illuminate\Database\ConnectionResolver;
10+
11+
trait MocksPdoConnection
12+
{
13+
/**
14+
* @var \Illuminate\Database\ConnectionResolverInterface|null
15+
*/
16+
private static $initialResolver;
17+
18+
/**
19+
* Generates a mock PDO connection and injects it into the models so that any actual
20+
* DB call can be properly intercepted.
21+
*
22+
* @return \Mockery\MockInterface
23+
*/
24+
protected function mockPdoConnection()
25+
{
26+
self::$initialResolver = Model::getConnectionResolver();
27+
28+
Model::unsetConnectionResolver();
29+
30+
$connection = new MySqlConnection($mock = Mockery::mock(PDO::class), 'testing_mock');
31+
$resolver = new ConnectionResolver(['mocked' => $connection]);
32+
$resolver->setDefaultConnection('mocked');
33+
34+
Model::setConnectionResolver($resolver);
35+
36+
return $mock;
37+
}
38+
39+
/**
40+
* Resets the mock state.
41+
*/
42+
protected function tearDownPdoMock()
43+
{
44+
if (! self::$initialResolver) {
45+
return;
46+
}
47+
48+
Model::setConnectionResolver(self::$initialResolver);
49+
50+
self::$initialResolver = null;
51+
}
52+
}

0 commit comments

Comments
 (0)