Skip to content

Commit f5b20e3

Browse files
committed
Fix failing tests due to way nest creation worked
1 parent 233cbfd commit f5b20e3

File tree

4 files changed

+46
-35
lines changed

4 files changed

+46
-35
lines changed

app/Services/Nests/NestCreationService.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
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\Nests;
114

@@ -19,12 +12,12 @@ class NestCreationService
1912
/**
2013
* @var \Illuminate\Contracts\Config\Repository
2114
*/
22-
protected $config;
15+
private $config;
2316

2417
/**
2518
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
2619
*/
27-
protected $repository;
20+
private $repository;
2821

2922
/**
3023
* NestCreationService constructor.
@@ -41,16 +34,16 @@ public function __construct(ConfigRepository $config, NestRepositoryInterface $r
4134
/**
4235
* Create a new nest on the system.
4336
*
44-
* @param array $data
37+
* @param array $data
38+
* @param string|null $author
4539
* @return \Pterodactyl\Models\Nest
46-
*
4740
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
4841
*/
49-
public function handle(array $data): Nest
42+
public function handle(array $data, string $author = null): Nest
5043
{
5144
return $this->repository->create([
5245
'uuid' => Uuid::uuid4()->toString(),
53-
'author' => $this->config->get('pterodactyl.service.author'),
46+
'author' => $author ?? $this->config->get('pterodactyl.service.author'),
5447
'name' => array_get($data, 'name'),
5548
'description' => array_get($data, 'description'),
5649
], true, true);

config/pterodactyl.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
| standard Pterodactyl shipped services.
1212
*/
1313
'service' => [
14-
'core' => 'ptrdctyl-v040-11e6-8b77-86f30ca893d3',
15-
'author' => env('SERVICE_AUTHOR'),
14+
'author' => env('SERVICE_AUTHOR', 'unknown@unknown.com'),
1615
],
1716

1817
/*

database/seeds/NestSeeder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private function createMinecraftNest(array $nest = null)
5959
$this->creationService->handle([
6060
'name' => 'Minecraft',
6161
'description' => 'Minecraft - the classic game from Mojang. With support for Vanilla MC, Spigot, and many others!',
62-
]);
62+
], 'support@pterodactyl.io');
6363
}
6464
}
6565

@@ -76,7 +76,7 @@ private function createSourceEngineNest(array $nest = null)
7676
$this->creationService->handle([
7777
'name' => 'Source Engine',
7878
'description' => 'Includes support for most Source Dedicated Server games.',
79-
]);
79+
], 'support@pterodactyl.io');
8080
}
8181
}
8282

@@ -93,7 +93,7 @@ private function createVoiceServersNest(array $nest = null)
9393
$this->creationService->handle([
9494
'name' => 'Voice Servers',
9595
'description' => 'Voice servers such as Mumble and Teamspeak 3.',
96-
]);
96+
], 'support@pterodactyl.io');
9797
}
9898
}
9999
}

tests/Unit/Services/Nests/NestCreationServiceTest.php

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use Mockery as m;
1313
use Tests\TestCase;
14-
use Ramsey\Uuid\Uuid;
1514
use Pterodactyl\Models\Nest;
1615
use Tests\Traits\MocksUuids;
1716
use Illuminate\Contracts\Config\Repository;
@@ -25,17 +24,12 @@ class NestCreationServiceTest extends TestCase
2524
/**
2625
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
2726
*/
28-
protected $config;
27+
private $config;
2928

3029
/**
3130
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock
3231
*/
33-
protected $repository;
34-
35-
/**
36-
* @var \Pterodactyl\Services\Nests\NestCreationService
37-
*/
38-
protected $service;
32+
private $repository;
3933

4034
/**
4135
* Setup tests.
@@ -46,8 +40,6 @@ public function setUp()
4640

4741
$this->config = m::mock(Repository::class);
4842
$this->repository = m::mock(NestRepositoryInterface::class);
49-
50-
$this->service = new NestCreationService($this->config, $this->repository);
5143
}
5244

5345
/**
@@ -56,21 +48,48 @@ public function setUp()
5648
public function testCreateNewService()
5749
{
5850
$model = factory(Nest::class)->make();
59-
$data = [
60-
'name' => $model->name,
61-
'description' => $model->description,
62-
];
6351

6452
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('testauthor@example.com');
6553
$this->repository->shouldReceive('create')->with([
6654
'uuid' => $this->getKnownUuid(),
6755
'author' => 'testauthor@example.com',
68-
'name' => $data['name'],
69-
'description' => $data['description'],
56+
'name' => $model->name,
57+
'description' => $model->description,
58+
], true, true)->once()->andReturn($model);
59+
60+
$response = $this->getService()->handle(['name' => $model->name, 'description' => $model->description]);
61+
$this->assertInstanceOf(Nest::class, $response);
62+
$this->assertEquals($model, $response);
63+
}
64+
65+
/**
66+
* Test creation of a new nest with a defined author. This is used by seeder
67+
* scripts which need to set a specific author for nests in order for other
68+
* functionality to work correctly.
69+
*/
70+
public function testCreateServiceWithDefinedAuthor()
71+
{
72+
$model = factory(Nest::class)->make();
73+
74+
$this->repository->shouldReceive('create')->with([
75+
'uuid' => $this->getKnownUuid(),
76+
'author' => 'support@pterodactyl.io',
77+
'name' => $model->name,
78+
'description' => $model->description,
7079
], true, true)->once()->andReturn($model);
7180

72-
$response = $this->service->handle($data);
81+
$response = $this->getService()->handle(['name' => $model->name, 'description' => $model->description], 'support@pterodactyl.io');
7382
$this->assertInstanceOf(Nest::class, $response);
7483
$this->assertEquals($model, $response);
7584
}
85+
86+
/**
87+
* Return an instance of the service with mocked dependencies.
88+
*
89+
* @return \Pterodactyl\Services\Nests\NestCreationService
90+
*/
91+
private function getService(): NestCreationService
92+
{
93+
return new NestCreationService($this->config, $this->repository);
94+
}
7695
}

0 commit comments

Comments
 (0)