Skip to content

Commit 159ad30

Browse files
committed
Fix existing tests
1 parent 6e02e94 commit 159ad30

File tree

5 files changed

+40
-51
lines changed

5 files changed

+40
-51
lines changed

tests/Unit/Services/Packs/PackCreationServiceTest.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Mockery as m;
1414
use Tests\TestCase;
1515
use Pterodactyl\Models\Pack;
16+
use Tests\Traits\MocksUuids;
1617
use Illuminate\Http\UploadedFile;
1718
use Illuminate\Contracts\Filesystem\Factory;
1819
use Illuminate\Database\ConnectionInterface;
@@ -23,18 +24,20 @@
2324

2425
class PackCreationServiceTest extends TestCase
2526
{
27+
use MocksUuids;
28+
2629
/**
27-
* @var \Illuminate\Database\ConnectionInterface
30+
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
2831
*/
2932
protected $connection;
3033

3134
/**
32-
* @var \Illuminate\Http\UploadedFile
35+
* @var \Illuminate\Http\UploadedFile|\Mockery\Mock
3336
*/
3437
protected $file;
3538

3639
/**
37-
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface
40+
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface|\Mockery\Mock
3841
*/
3942
protected $repository;
4043

@@ -44,15 +47,10 @@ class PackCreationServiceTest extends TestCase
4447
protected $service;
4548

4649
/**
47-
* @var \Illuminate\Contracts\Filesystem\Factory
50+
* @var \Illuminate\Contracts\Filesystem\Factory|\Mockery\Mock
4851
*/
4952
protected $storage;
5053

51-
/**
52-
* @var \Ramsey\Uuid\Uuid
53-
*/
54-
protected $uuid;
55-
5654
/**
5755
* Setup tests.
5856
*/
@@ -64,7 +62,6 @@ public function setUp()
6462
$this->file = m::mock(UploadedFile::class);
6563
$this->repository = m::mock(PackRepositoryInterface::class);
6664
$this->storage = m::mock(Factory::class);
67-
$this->uuid = m::mock('overload:\Ramsey\Uuid\Uuid');
6865

6966
$this->service = new PackCreationService($this->connection, $this->storage, $this->repository);
7067
}
@@ -77,17 +74,15 @@ public function testPackIsCreatedWhenNoUploadedFileIsPassed()
7774
$model = factory(Pack::class)->make();
7875

7976
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
80-
$this->uuid->shouldReceive('uuid4')->withNoArgs()->once()->andReturn($model->uuid);
8177
$this->repository->shouldReceive('create')->with([
82-
'uuid' => $model->uuid,
78+
'uuid' => $this->getKnownUuid(),
8379
'selectable' => false,
8480
'visible' => false,
8581
'locked' => false,
8682
'test-data' => 'value',
8783
])->once()->andReturn($model);
8884

89-
$this->storage->shouldReceive('disk')->withNoArgs()->once()->andReturnSelf()
90-
->shouldReceive('makeDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
85+
$this->storage->shouldReceive('disk->makeDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
9186
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
9287

9388
$response = $this->service->handle(['test-data' => 'value']);
@@ -107,17 +102,15 @@ public function testPackIsCreatedWhenUploadedFileIsProvided($mime)
107102
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
108103
$this->file->shouldReceive('getMimeType')->withNoArgs()->once()->andReturn($mime);
109104
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
110-
$this->uuid->shouldReceive('uuid4')->withNoArgs()->once()->andReturn($model->uuid);
111105
$this->repository->shouldReceive('create')->with([
112-
'uuid' => $model->uuid,
106+
'uuid' => $this->getKnownUuid(),
113107
'selectable' => false,
114108
'visible' => false,
115109
'locked' => false,
116110
'test-data' => 'value',
117111
])->once()->andReturn($model);
118112

119-
$this->storage->shouldReceive('disk')->withNoArgs()->once()->andReturnSelf()
120-
->shouldReceive('makeDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
113+
$this->storage->shouldReceive('disk->makeDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
121114
$this->file->shouldReceive('storeAs')->with('packs/' . $model->uuid, 'archive.tar.gz')->once()->andReturnNull();
122115
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
123116

tests/Unit/Services/Packs/PackUpdateServiceTest.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
class PackUpdateServiceTest extends TestCase
2121
{
2222
/**
23-
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface
23+
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface|\Mockery\Mock
2424
*/
2525
protected $repository;
2626

2727
/**
28-
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
28+
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
2929
*/
3030
protected $serverRepository;
3131

@@ -53,8 +53,7 @@ public function setUp()
5353
public function testPackIsUpdated()
5454
{
5555
$model = factory(Pack::class)->make();
56-
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
57-
->shouldReceive('update')->with($model->id, [
56+
$this->repository->shouldReceive('withoutFresh->update')->with($model->id, [
5857
'locked' => false,
5958
'visible' => false,
6059
'selectable' => false,
@@ -67,13 +66,13 @@ public function testPackIsUpdated()
6766
/**
6867
* Test that an exception is thrown if the pack option ID is changed while servers are using the pack.
6968
*/
70-
public function testExceptionIsThrownIfModifyingOptionIdWhenServersAreAttached()
69+
public function testExceptionIsThrownIfModifyingEggIdWhenServersAreAttached()
7170
{
7271
$model = factory(Pack::class)->make();
7372
$this->serverRepository->shouldReceive('findCountWhere')->with([['pack_id', '=', $model->id]])->once()->andReturn(1);
7473

7574
try {
76-
$this->service->handle($model, ['option_id' => 0]);
75+
$this->service->handle($model, ['egg_id' => 0]);
7776
} catch (HasActiveServersException $exception) {
7877
$this->assertEquals(trans('exceptions.packs.update_has_servers'), $exception->getMessage());
7978
}
@@ -86,10 +85,9 @@ public function testPackIdCanBePassedInPlaceOfModel()
8685
{
8786
$model = factory(Pack::class)->make();
8887

89-
$this->repository->shouldReceive('withColumns')->with(['id', 'option_id'])->once()->andReturnSelf()
88+
$this->repository->shouldReceive('withColumns')->with(['id', 'egg_id'])->once()->andReturnSelf()
9089
->shouldReceive('find')->with($model->id)->once()->andReturn($model);
91-
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
92-
->shouldReceive('update')->with($model->id, [
90+
$this->repository->shouldReceive('withoutFresh->update')->with($model->id, [
9391
'locked' => false,
9492
'visible' => false,
9593
'selectable' => false,

tests/Unit/Services/Packs/TemplateUploadServiceTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ class TemplateUploadServiceTest extends TestCase
2727
const JSON_FILE_CONTENTS = '{"test_content": "value"}';
2828

2929
/**
30-
* @var \ZipArchive
30+
* @var \ZipArchive|\Mockery\Mock
3131
*/
3232
protected $archive;
3333

3434
/**
35-
* @var \Pterodactyl\Services\Packs\PackCreationService
35+
* @var \Pterodactyl\Services\Packs\PackCreationService|\Mockery\Mock
3636
*/
3737
protected $creationService;
3838

3939
/**
40-
* @var \Illuminate\Http\UploadedFile
40+
* @var \Illuminate\Http\UploadedFile|\Mockery\Mock
4141
*/
4242
protected $file;
4343

@@ -70,10 +70,9 @@ public function testJsonFileIsProcessed($mime)
7070
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
7171
$this->file->shouldReceive('getMimeType')->withNoArgs()->twice()->andReturn($mime);
7272
$this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(128);
73-
$this->file->shouldReceive('openFile')->withNoArgs()->once()->andReturnSelf()
74-
->shouldReceive('fread')->with(128)->once()->andReturn(self::JSON_FILE_CONTENTS);
73+
$this->file->shouldReceive('openFile->fread')->with(128)->once()->andReturn(self::JSON_FILE_CONTENTS);
7574

76-
$this->creationService->shouldReceive('handle')->with(['test_content' => 'value', 'option_id' => 1])
75+
$this->creationService->shouldReceive('handle')->with(['test_content' => 'value', 'egg_id' => 1])
7776
->once()->andReturn(factory(Pack::class)->make());
7877

7978
$this->assertInstanceOf(Pack::class, $this->service->handle(1, $this->file));
@@ -94,7 +93,7 @@ public function testZipfileIsProcessed()
9493
$this->archive->shouldReceive('locateName')->with('import.json')->once()->andReturn(true);
9594
$this->archive->shouldReceive('locateName')->with('archive.tar.gz')->once()->andReturn(true);
9695
$this->archive->shouldReceive('getFromName')->with('import.json')->once()->andReturn(self::JSON_FILE_CONTENTS);
97-
$this->creationService->shouldReceive('handle')->with(['test_content' => 'value', 'option_id' => 1])
96+
$this->creationService->shouldReceive('handle')->with(['test_content' => 'value', 'egg_id' => 1])
9897
->once()->andReturn($model);
9998
$this->archive->shouldReceive('extractTo')->with(storage_path('app/packs/' . $model->uuid), 'archive.tar.gz')
10099
->once()->andReturn(true);

tests/Unit/Services/Servers/ServerCreationServiceTest.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Mockery as m;
1313
use Tests\TestCase;
1414
use phpmock\phpunit\PHPMock;
15+
use Tests\Traits\MocksUuids;
1516
use GuzzleHttp\Exception\RequestException;
1617
use Illuminate\Database\ConnectionInterface;
1718
use Pterodactyl\Exceptions\PterodactylException;
@@ -32,7 +33,7 @@
3233
*/
3334
class ServerCreationServiceTest extends TestCase
3435
{
35-
use PHPMock;
36+
use MocksUuids, PHPMock;
3637

3738
/**
3839
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock
@@ -72,8 +73,8 @@ class ServerCreationServiceTest extends TestCase
7273
'environment' => [
7374
'TEST_VAR_1' => 'var1-value',
7475
],
75-
'service_id' => 1,
76-
'option_id' => 1,
76+
'nest_id' => 1,
77+
'egg_id' => 1,
7778
'startup' => 'startup-param',
7879
'docker_image' => 'some/image',
7980
];
@@ -118,11 +119,6 @@ class ServerCreationServiceTest extends TestCase
118119
*/
119120
protected $validatorService;
120121

121-
/**
122-
* @var \Ramsey\Uuid\Uuid|\Mockery\Mock
123-
*/
124-
protected $uuid;
125-
126122
/**
127123
* Setup tests.
128124
*/
@@ -141,7 +137,6 @@ public function setUp()
141137
$this->userRepository = m::mock(UserRepositoryInterface::class);
142138
$this->usernameService = m::mock(UsernameGenerationService::class);
143139
$this->validatorService = m::mock(VariableValidatorService::class);
144-
$this->uuid = m::mock('overload:Ramsey\Uuid\Uuid');
145140

146141
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'str_random')
147142
->expects($this->any())->willReturn('random_string');
@@ -167,14 +162,19 @@ public function testCreateShouldHitAllOfTheNecessaryServicesAndStoreTheServer()
167162
{
168163
$this->validatorService->shouldReceive('isAdmin')->withNoArgs()->once()->andReturnSelf()
169164
->shouldReceive('setFields')->with($this->data['environment'])->once()->andReturnSelf()
170-
->shouldReceive('validate')->with($this->data['option_id'])->once()->andReturnSelf();
165+
->shouldReceive('validate')->with($this->data['egg_id'])->once()->andReturnSelf();
171166

172167
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
173-
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-0000');
174168
$this->usernameService->shouldReceive('generate')->with($this->data['name'], 'random_string')
175169
->once()->andReturn('user_name');
176170

177-
$this->repository->shouldReceive('create')->withAnyArgs()->once()->andReturn((object) [
171+
$this->repository->shouldReceive('create')->with(m::subset([
172+
'uuid' => $this->getKnownUuid(),
173+
'node_id' => $this->data['node_id'],
174+
'owner_id' => 1,
175+
'nest_id' => 1,
176+
'egg_id' => 1,
177+
]))->once()->andReturn((object) [
178178
'node_id' => 1,
179179
'id' => 1,
180180
]);
@@ -212,7 +212,6 @@ public function testExceptionShouldBeThrownIfTheRequestFails()
212212
$this->validatorService->shouldReceive('isAdmin->setFields->validate->getResults')->once()->andReturn([]);
213213
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
214214
$this->usernameService->shouldReceive('generate')->once()->andReturn('user_name');
215-
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-0000');
216215
$this->repository->shouldReceive('create')->once()->andReturn((object) [
217216
'node_id' => 1,
218217
'id' => 1,

tests/Unit/Services/Servers/VariableValidatorServiceTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function testGettingResultsReturnsAnArrayOfValues()
115115
*/
116116
public function testEmptyResultSetShouldBeReturnedIfNoVariablesAreFound()
117117
{
118-
$this->optionVariableRepository->shouldReceive('findWhere')->with([['option_id', '=', 1]])->andReturn([]);
118+
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn([]);
119119

120120
$response = $this->service->validate(1);
121121

@@ -129,7 +129,7 @@ public function testEmptyResultSetShouldBeReturnedIfNoVariablesAreFound()
129129
*/
130130
public function testValidatorShouldNotProcessVariablesSetAsNotUserEditableWhenAdminFlagIsNotPassed()
131131
{
132-
$this->optionVariableRepository->shouldReceive('findWhere')->with([['option_id', '=', 1]])->andReturn($this->variables);
132+
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($this->variables);
133133

134134
$this->validator->shouldReceive('make')->with([
135135
'variable_value' => 'Test_SomeValue_0',
@@ -161,7 +161,7 @@ public function testValidatorShouldNotProcessVariablesSetAsNotUserEditableWhenAd
161161
*/
162162
public function testValidatorShouldProcessAllVariablesWhenAdminFlagIsSet()
163163
{
164-
$this->optionVariableRepository->shouldReceive('findWhere')->with([['option_id', '=', 1]])->andReturn($this->variables);
164+
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($this->variables);
165165

166166
foreach ($this->variables as $key => $variable) {
167167
$this->validator->shouldReceive('make')->with([
@@ -198,7 +198,7 @@ public function testValidatorShouldProcessAllVariablesWhenAdminFlagIsSet()
198198
*/
199199
public function testValidatorShouldThrowExceptionWhenAValidationErrorIsEncountered()
200200
{
201-
$this->optionVariableRepository->shouldReceive('findWhere')->with([['option_id', '=', 1]])->andReturn($this->variables);
201+
$this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($this->variables);
202202

203203
$this->validator->shouldReceive('make')->with([
204204
'variable_value' => null,

0 commit comments

Comments
 (0)