Skip to content

Commit 0c2bd41

Browse files
committed
Fix unit tests for eggs
1 parent 0f4f223 commit 0c2bd41

File tree

14 files changed

+132
-202
lines changed

14 files changed

+132
-202
lines changed

app/Http/Controllers/Admin/Nests/EggScriptController.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +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\Http\Controllers\Admin\Nests;
114

125
use Illuminate\View\View;
6+
use Pterodactyl\Models\Egg;
137
use Illuminate\Http\RedirectResponse;
148
use Prologue\Alerts\AlertsMessageBag;
159
use Pterodactyl\Http\Controllers\Controller;
@@ -81,14 +75,14 @@ public function index(int $egg): View
8175
* Handle a request to update the installation script for an Egg.
8276
*
8377
* @param \Pterodactyl\Http\Requests\Admin\Egg\EggScriptFormRequest $request
84-
* @param int $egg
78+
* @param \Pterodactyl\Models\Egg $egg
8579
* @return \Illuminate\Http\RedirectResponse
8680
*
8781
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
8882
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
8983
* @throws \Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException
9084
*/
91-
public function update(EggScriptFormRequest $request, int $egg): RedirectResponse
85+
public function update(EggScriptFormRequest $request, Egg $egg): RedirectResponse
9286
{
9387
$this->installScriptService->handle($egg, $request->normalize());
9488
$this->alert->success(trans('admin/nests.eggs.notices.script_updated'))->flash();

app/Http/Controllers/Admin/Nests/EggShareController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ public function import(EggImportFormRequest $request): RedirectResponse
102102
* Update an existing Egg using a new imported file.
103103
*
104104
* @param \Pterodactyl\Http\Requests\Admin\Egg\EggImportFormRequest $request
105-
* @param int $egg
105+
* @param \Pterodactyl\Models\Egg $egg
106106
* @return \Illuminate\Http\RedirectResponse
107107
*
108108
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
109109
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
110110
* @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException
111111
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
112112
*/
113-
public function update(EggImportFormRequest $request, int $egg): RedirectResponse
113+
public function update(EggImportFormRequest $request, Egg $egg): RedirectResponse
114114
{
115115
$this->updateImporterService->handle($egg, $request->file('import_file'));
116116
$this->alert->success(trans('admin/nests.eggs.notices.updated_via_import'))->flash();

app/Services/Eggs/Scripts/InstallScriptService.php

Lines changed: 1 addition & 12 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\Eggs\Scripts;
114

@@ -40,12 +33,8 @@ public function __construct(EggRepositoryInterface $repository)
4033
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
4134
* @throws \Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException
4235
*/
43-
public function handle($egg, array $data)
36+
public function handle(Egg $egg, array $data)
4437
{
45-
if (! $egg instanceof Egg) {
46-
$egg = $this->repository->find($egg);
47-
}
48-
4938
if (! is_null(array_get($data, 'copy_script_from'))) {
5039
if (! $this->repository->isCopyableScript(array_get($data, 'copy_script_from'), $egg->nest_id)) {
5140
throw new InvalidCopyFromException(trans('exceptions.nest.egg.invalid_copy_id'));

app/Services/Eggs/Sharing/EggExporterService.php

Lines changed: 0 additions & 7 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\Eggs\Sharing;
114

app/Services/Eggs/Sharing/EggImporterService.php

Lines changed: 0 additions & 7 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\Eggs\Sharing;
114

app/Services/Eggs/Sharing/EggUpdateImporterService.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pterodactyl\Services\Eggs\Sharing;
44

5+
use Pterodactyl\Models\Egg;
56
use Illuminate\Http\UploadedFile;
67
use Illuminate\Database\ConnectionInterface;
78
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
@@ -46,15 +47,15 @@ public function __construct(
4647
/**
4748
* Update an existing Egg using an uploaded JSON file.
4849
*
49-
* @param int $egg
50+
* @param \Pterodactyl\Models\Egg $egg
5051
* @param \Illuminate\Http\UploadedFile $file
5152
*
5253
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
5354
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
5455
* @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException
5556
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
5657
*/
57-
public function handle(int $egg, UploadedFile $file)
58+
public function handle(Egg $egg, UploadedFile $file)
5859
{
5960
if ($file->getError() !== UPLOAD_ERR_OK || ! $file->isFile()) {
6061
throw new InvalidFileUploadException(
@@ -81,7 +82,7 @@ public function handle(int $egg, UploadedFile $file)
8182
}
8283

8384
$this->connection->beginTransaction();
84-
$this->repository->update($egg, [
85+
$this->repository->update($egg->id, [
8586
'author' => object_get($parsed, 'author'),
8687
'name' => object_get($parsed, 'name'),
8788
'description' => object_get($parsed, 'description'),
@@ -99,19 +100,19 @@ public function handle(int $egg, UploadedFile $file)
99100
// Update Existing Variables
100101
collect($parsed->variables)->each(function ($variable) use ($egg) {
101102
$this->variableRepository->withoutFreshModel()->updateOrCreate([
102-
'egg_id' => $egg,
103+
'egg_id' => $egg->id,
103104
'env_variable' => $variable->env_variable,
104105
], collect($variable)->except(['egg_id', 'env_variable'])->toArray());
105106
});
106107

107108
$imported = collect($parsed->variables)->pluck('env_variable')->toArray();
108-
$existing = $this->variableRepository->setColumns(['id', 'env_variable'])->findWhere([['egg_id', '=', $egg]]);
109+
$existing = $this->variableRepository->setColumns(['id', 'env_variable'])->findWhere([['egg_id', '=', $egg->id]]);
109110

110111
// Delete variables not present in the import.
111112
collect($existing)->each(function ($variable) use ($egg, $imported) {
112113
if (! in_array($variable->env_variable, $imported)) {
113114
$this->variableRepository->deleteWhere([
114-
['egg_id', '=', $egg],
115+
['egg_id', '=', $egg->id],
115116
['env_variable', '=', $variable->env_variable],
116117
]);
117118
}

database/factories/ModelFactory.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

3+
use Carbon\Carbon;
34
use Ramsey\Uuid\Uuid;
45
use Cake\Chronos\Chronos;
56
use Illuminate\Support\Str;
67
use Pterodactyl\Models\Node;
78
use Faker\Generator as Faker;
89
use Pterodactyl\Models\ApiKey;
910

11+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
1012
/*
1113
|--------------------------------------------------------------------------
1214
| Model Factories
@@ -35,8 +37,8 @@
3537
'installed' => 1,
3638
'database_limit' => null,
3739
'allocation_limit' => null,
38-
'created_at' => \Carbon\Carbon::now(),
39-
'updated_at' => \Carbon\Carbon::now(),
40+
'created_at' => Carbon::now(),
41+
'updated_at' => Carbon::now(),
4042
];
4143
});
4244

@@ -160,8 +162,8 @@
160162
'username' => str_random(10),
161163
'remote' => '%',
162164
'password' => $password ?: bcrypt('test123'),
163-
'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
164-
'updated_at' => \Carbon\Carbon::now()->toDateTimeString(),
165+
'created_at' => Carbon::now()->toDateTimeString(),
166+
'updated_at' => Carbon::now()->toDateTimeString(),
165167
];
166168
});
167169

@@ -190,7 +192,7 @@
190192
'token' => $token ?: $token = encrypt(str_random(Pterodactyl\Models\ApiKey::KEY_LENGTH)),
191193
'allowed_ips' => null,
192194
'memo' => 'Test Function Key',
193-
'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
194-
'updated_at' => \Carbon\Carbon::now()->toDateTimeString(),
195+
'created_at' => Carbon::now()->toDateTimeString(),
196+
'updated_at' => Carbon::now()->toDateTimeString(),
195197
];
196198
});

database/seeds/EggSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private function parseEggFiles(Nest $nest)
130130
['nest_id', '=', $nest->id],
131131
]);
132132

133-
$this->updateImporterService->handle($egg->id, $file);
133+
$this->updateImporterService->handle($egg, $file);
134134

135135
$this->command->info('Updated ' . $decoded->name);
136136
} catch (RecordNotFoundException $exception) {

tests/Unit/Services/Allocations/AllocationDeletionServiceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public function setUp(): void
2828
*/
2929
public function testAllocationIsDeleted()
3030
{
31-
$model = factory(Allocation::class)->make();
31+
$model = factory(Allocation::class)->make(['id' => 123]);
3232

33-
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturn(1);
33+
$this->repository->expects('delete')->with($model->id)->andReturns(1);
3434

3535
$response = $this->getService()->handle($model);
3636
$this->assertEquals(1, $response);

tests/Unit/Services/Eggs/EggUpdateServiceTest.php

Lines changed: 1 addition & 8 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 Tests\Unit\Services\Services\Options;
114

@@ -41,7 +34,7 @@ public function setUp(): void
4134
{
4235
parent::setUp();
4336

44-
$this->model = factory(Egg::class)->make();
37+
$this->model = factory(Egg::class)->make(['id' => 123]);
4538
$this->repository = m::mock(EggRepositoryInterface::class);
4639

4740
$this->service = new EggUpdateService($this->repository);

0 commit comments

Comments
 (0)