Fix unit tests for eggs

This commit is contained in:
Dane Everitt 2020-10-05 21:29:35 -07:00
parent 0f4f2235a3
commit 0c2bd416ee
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
14 changed files with 130 additions and 200 deletions

View file

@ -1,15 +1,9 @@
<?php <?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Http\Controllers\Admin\Nests; namespace Pterodactyl\Http\Controllers\Admin\Nests;
use Illuminate\View\View; use Illuminate\View\View;
use Pterodactyl\Models\Egg;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag; use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
@ -81,14 +75,14 @@ class EggScriptController extends Controller
* Handle a request to update the installation script for an Egg. * Handle a request to update the installation script for an Egg.
* *
* @param \Pterodactyl\Http\Requests\Admin\Egg\EggScriptFormRequest $request * @param \Pterodactyl\Http\Requests\Admin\Egg\EggScriptFormRequest $request
* @param int $egg * @param \Pterodactyl\Models\Egg $egg
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\RedirectResponse
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException * @throws \Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException
*/ */
public function update(EggScriptFormRequest $request, int $egg): RedirectResponse public function update(EggScriptFormRequest $request, Egg $egg): RedirectResponse
{ {
$this->installScriptService->handle($egg, $request->normalize()); $this->installScriptService->handle($egg, $request->normalize());
$this->alert->success(trans('admin/nests.eggs.notices.script_updated'))->flash(); $this->alert->success(trans('admin/nests.eggs.notices.script_updated'))->flash();

View file

@ -102,7 +102,7 @@ class EggShareController extends Controller
* Update an existing Egg using a new imported file. * Update an existing Egg using a new imported file.
* *
* @param \Pterodactyl\Http\Requests\Admin\Egg\EggImportFormRequest $request * @param \Pterodactyl\Http\Requests\Admin\Egg\EggImportFormRequest $request
* @param int $egg * @param \Pterodactyl\Models\Egg $egg
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\RedirectResponse
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
@ -110,7 +110,7 @@ class EggShareController extends Controller
* @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException * @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException * @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
*/ */
public function update(EggImportFormRequest $request, int $egg): RedirectResponse public function update(EggImportFormRequest $request, Egg $egg): RedirectResponse
{ {
$this->updateImporterService->handle($egg, $request->file('import_file')); $this->updateImporterService->handle($egg, $request->file('import_file'));
$this->alert->success(trans('admin/nests.eggs.notices.updated_via_import'))->flash(); $this->alert->success(trans('admin/nests.eggs.notices.updated_via_import'))->flash();

View file

@ -1,11 +1,4 @@
<?php <?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Eggs\Scripts; namespace Pterodactyl\Services\Eggs\Scripts;
@ -40,12 +33,8 @@ class InstallScriptService
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException * @throws \Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException
*/ */
public function handle($egg, array $data) public function handle(Egg $egg, array $data)
{ {
if (! $egg instanceof Egg) {
$egg = $this->repository->find($egg);
}
if (! is_null(array_get($data, 'copy_script_from'))) { if (! is_null(array_get($data, 'copy_script_from'))) {
if (! $this->repository->isCopyableScript(array_get($data, 'copy_script_from'), $egg->nest_id)) { if (! $this->repository->isCopyableScript(array_get($data, 'copy_script_from'), $egg->nest_id)) {
throw new InvalidCopyFromException(trans('exceptions.nest.egg.invalid_copy_id')); throw new InvalidCopyFromException(trans('exceptions.nest.egg.invalid_copy_id'));

View file

@ -1,11 +1,4 @@
<?php <?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Eggs\Sharing; namespace Pterodactyl\Services\Eggs\Sharing;

View file

@ -1,11 +1,4 @@
<?php <?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Eggs\Sharing; namespace Pterodactyl\Services\Eggs\Sharing;

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Services\Eggs\Sharing; namespace Pterodactyl\Services\Eggs\Sharing;
use Pterodactyl\Models\Egg;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
@ -46,7 +47,7 @@ class EggUpdateImporterService
/** /**
* Update an existing Egg using an uploaded JSON file. * Update an existing Egg using an uploaded JSON file.
* *
* @param int $egg * @param \Pterodactyl\Models\Egg $egg
* @param \Illuminate\Http\UploadedFile $file * @param \Illuminate\Http\UploadedFile $file
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
@ -54,7 +55,7 @@ class EggUpdateImporterService
* @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException * @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException * @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
*/ */
public function handle(int $egg, UploadedFile $file) public function handle(Egg $egg, UploadedFile $file)
{ {
if ($file->getError() !== UPLOAD_ERR_OK || ! $file->isFile()) { if ($file->getError() !== UPLOAD_ERR_OK || ! $file->isFile()) {
throw new InvalidFileUploadException( throw new InvalidFileUploadException(
@ -81,7 +82,7 @@ class EggUpdateImporterService
} }
$this->connection->beginTransaction(); $this->connection->beginTransaction();
$this->repository->update($egg, [ $this->repository->update($egg->id, [
'author' => object_get($parsed, 'author'), 'author' => object_get($parsed, 'author'),
'name' => object_get($parsed, 'name'), 'name' => object_get($parsed, 'name'),
'description' => object_get($parsed, 'description'), 'description' => object_get($parsed, 'description'),
@ -99,19 +100,19 @@ class EggUpdateImporterService
// Update Existing Variables // Update Existing Variables
collect($parsed->variables)->each(function ($variable) use ($egg) { collect($parsed->variables)->each(function ($variable) use ($egg) {
$this->variableRepository->withoutFreshModel()->updateOrCreate([ $this->variableRepository->withoutFreshModel()->updateOrCreate([
'egg_id' => $egg, 'egg_id' => $egg->id,
'env_variable' => $variable->env_variable, 'env_variable' => $variable->env_variable,
], collect($variable)->except(['egg_id', 'env_variable'])->toArray()); ], collect($variable)->except(['egg_id', 'env_variable'])->toArray());
}); });
$imported = collect($parsed->variables)->pluck('env_variable')->toArray(); $imported = collect($parsed->variables)->pluck('env_variable')->toArray();
$existing = $this->variableRepository->setColumns(['id', 'env_variable'])->findWhere([['egg_id', '=', $egg]]); $existing = $this->variableRepository->setColumns(['id', 'env_variable'])->findWhere([['egg_id', '=', $egg->id]]);
// Delete variables not present in the import. // Delete variables not present in the import.
collect($existing)->each(function ($variable) use ($egg, $imported) { collect($existing)->each(function ($variable) use ($egg, $imported) {
if (! in_array($variable->env_variable, $imported)) { if (! in_array($variable->env_variable, $imported)) {
$this->variableRepository->deleteWhere([ $this->variableRepository->deleteWhere([
['egg_id', '=', $egg], ['egg_id', '=', $egg->id],
['env_variable', '=', $variable->env_variable], ['env_variable', '=', $variable->env_variable],
]); ]);
} }

View file

@ -1,5 +1,6 @@
<?php <?php
use Carbon\Carbon;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Cake\Chronos\Chronos; use Cake\Chronos\Chronos;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -7,6 +8,7 @@ use Pterodactyl\Models\Node;
use Faker\Generator as Faker; use Faker\Generator as Faker;
use Pterodactyl\Models\ApiKey; use Pterodactyl\Models\ApiKey;
/** @var \Illuminate\Database\Eloquent\Factory $factory */
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Model Factories | Model Factories
@ -35,8 +37,8 @@ $factory->define(Pterodactyl\Models\Server::class, function (Faker $faker) {
'installed' => 1, 'installed' => 1,
'database_limit' => null, 'database_limit' => null,
'allocation_limit' => null, 'allocation_limit' => null,
'created_at' => \Carbon\Carbon::now(), 'created_at' => Carbon::now(),
'updated_at' => \Carbon\Carbon::now(), 'updated_at' => Carbon::now(),
]; ];
}); });
@ -160,8 +162,8 @@ $factory->define(Pterodactyl\Models\Database::class, function (Faker $faker) {
'username' => str_random(10), 'username' => str_random(10),
'remote' => '%', 'remote' => '%',
'password' => $password ?: bcrypt('test123'), 'password' => $password ?: bcrypt('test123'),
'created_at' => \Carbon\Carbon::now()->toDateTimeString(), 'created_at' => Carbon::now()->toDateTimeString(),
'updated_at' => \Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => Carbon::now()->toDateTimeString(),
]; ];
}); });
@ -190,7 +192,7 @@ $factory->define(Pterodactyl\Models\ApiKey::class, function (Faker $faker) {
'token' => $token ?: $token = encrypt(str_random(Pterodactyl\Models\ApiKey::KEY_LENGTH)), 'token' => $token ?: $token = encrypt(str_random(Pterodactyl\Models\ApiKey::KEY_LENGTH)),
'allowed_ips' => null, 'allowed_ips' => null,
'memo' => 'Test Function Key', 'memo' => 'Test Function Key',
'created_at' => \Carbon\Carbon::now()->toDateTimeString(), 'created_at' => Carbon::now()->toDateTimeString(),
'updated_at' => \Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => Carbon::now()->toDateTimeString(),
]; ];
}); });

View file

@ -130,7 +130,7 @@ class EggSeeder extends Seeder
['nest_id', '=', $nest->id], ['nest_id', '=', $nest->id],
]); ]);
$this->updateImporterService->handle($egg->id, $file); $this->updateImporterService->handle($egg, $file);
$this->command->info('Updated ' . $decoded->name); $this->command->info('Updated ' . $decoded->name);
} catch (RecordNotFoundException $exception) { } catch (RecordNotFoundException $exception) {

View file

@ -28,9 +28,9 @@ class AllocationDeletionServiceTest extends TestCase
*/ */
public function testAllocationIsDeleted() public function testAllocationIsDeleted()
{ {
$model = factory(Allocation::class)->make(); $model = factory(Allocation::class)->make(['id' => 123]);
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturn(1); $this->repository->expects('delete')->with($model->id)->andReturns(1);
$response = $this->getService()->handle($model); $response = $this->getService()->handle($model);
$this->assertEquals(1, $response); $this->assertEquals(1, $response);

View file

@ -1,11 +1,4 @@
<?php <?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Services\Options; namespace Tests\Unit\Services\Services\Options;
@ -41,7 +34,7 @@ class EggUpdateServiceTest extends TestCase
{ {
parent::setUp(); parent::setUp();
$this->model = factory(Egg::class)->make(); $this->model = factory(Egg::class)->make(['id' => 123]);
$this->repository = m::mock(EggRepositoryInterface::class); $this->repository = m::mock(EggRepositoryInterface::class);
$this->service = new EggUpdateService($this->repository); $this->service = new EggUpdateService($this->repository);

View file

@ -1,13 +1,6 @@
<?php <?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Services\Options; namespace Tests\Unit\Services\Eggs\Scripts;
use Exception; use Exception;
use Mockery as m; use Mockery as m;
@ -30,21 +23,11 @@ class InstallScriptServiceTest extends TestCase
'copy_script_from' => null, 'copy_script_from' => null,
]; ];
/**
* @var \Pterodactyl\Models\Egg
*/
protected $model;
/** /**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
*/ */
protected $repository; protected $repository;
/**
* @var \Pterodactyl\Services\Eggs\Scripts\InstallScriptService
*/
protected $service;
/** /**
* Setup tests. * Setup tests.
*/ */
@ -52,10 +35,7 @@ class InstallScriptServiceTest extends TestCase
{ {
parent::setUp(); parent::setUp();
$this->model = factory(Egg::class)->make();
$this->repository = m::mock(EggRepositoryInterface::class); $this->repository = m::mock(EggRepositoryInterface::class);
$this->service = new InstallScriptService($this->repository);
} }
/** /**
@ -63,13 +43,13 @@ class InstallScriptServiceTest extends TestCase
*/ */
public function testUpdateWithValidCopyScriptFromAttribute() public function testUpdateWithValidCopyScriptFromAttribute()
{ {
$model = factory(Egg::class)->make(['id' => 123, 'nest_id' => 456]);
$this->data['copy_script_from'] = 1; $this->data['copy_script_from'] = 1;
$this->repository->shouldReceive('isCopyableScript')->with(1, $this->model->nest_id)->once()->andReturn(true); $this->repository->shouldReceive('isCopyableScript')->with(1, $model->nest_id)->once()->andReturn(true);
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf() $this->repository->expects('withoutFreshModel->update')->with($model->id, $this->data)->andReturnNull();
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
$this->service->handle($this->model, $this->data); $this->getService()->handle($model, $this->data);
} }
/** /**
@ -79,13 +59,13 @@ class InstallScriptServiceTest extends TestCase
{ {
$this->data['copy_script_from'] = 1; $this->data['copy_script_from'] = 1;
$this->repository->shouldReceive('isCopyableScript')->with(1, $this->model->nest_id)->once()->andReturn(false); $this->expectException(InvalidCopyFromException::class);
try { $this->expectExceptionMessage(trans('exceptions.nest.egg.invalid_copy_id'));
$this->service->handle($this->model, $this->data);
} catch (Exception $exception) { $model = factory(Egg::class)->make(['id' => 123, 'nest_id' => 456]);
$this->assertInstanceOf(InvalidCopyFromException::class, $exception);
$this->assertEquals(trans('exceptions.nest.egg.invalid_copy_id'), $exception->getMessage()); $this->repository->expects('isCopyableScript')->with(1, $model->nest_id)->andReturn(false);
} $this->getService()->handle($model, $this->data);
} }
/** /**
@ -93,21 +73,15 @@ class InstallScriptServiceTest extends TestCase
*/ */
public function testUpdateWithoutNewCopyScriptFromAttribute() public function testUpdateWithoutNewCopyScriptFromAttribute()
{ {
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf() $model = factory(Egg::class)->make(['id' => 123, 'nest_id' => 456]);
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
$this->service->handle($this->model, $this->data); $this->repository->expects('withoutFreshModel->update')->with($model->id, $this->data)->andReturnNull();
$this->getService()->handle($model, $this->data);
} }
/** private function getService()
* Test that an integer can be passed in place of a model.
*/
public function testFunctionAcceptsIntegerInPlaceOfModel()
{ {
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model); return new InstallScriptService($this->repository);
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
$this->service->handle($this->model->id, $this->data);
} }
} }

View file

@ -1,11 +1,4 @@
<?php <?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Eggs\Sharing; namespace Tests\Unit\Services\Eggs\Sharing;
@ -22,21 +15,11 @@ class EggExporterServiceTest extends TestCase
{ {
use NestedObjectAssertionsTrait; use NestedObjectAssertionsTrait;
/**
* @var \Carbon\Carbon
*/
protected $carbon;
/** /**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
*/ */
protected $repository; protected $repository;
/**
* @var \Pterodactyl\Services\Eggs\Sharing\EggExporterService
*/
protected $service;
/** /**
* Setup tests. * Setup tests.
*/ */
@ -45,10 +28,8 @@ class EggExporterServiceTest extends TestCase
parent::setUp(); parent::setUp();
Carbon::setTestNow(Carbon::now()); Carbon::setTestNow(Carbon::now());
$this->carbon = new Carbon();
$this->repository = m::mock(EggRepositoryInterface::class);
$this->service = new EggExporterService($this->repository); $this->repository = m::mock(EggRepositoryInterface::class);
} }
/** /**
@ -56,12 +37,17 @@ class EggExporterServiceTest extends TestCase
*/ */
public function testJsonStructureIsExported() public function testJsonStructureIsExported()
{ {
$egg = factory(Egg::class)->make(); $egg = factory(Egg::class)->make([
'id' => 123,
'nest_id' => 456,
]);
$egg->variables = collect([$variable = factory(EggVariable::class)->make()]); $egg->variables = collect([$variable = factory(EggVariable::class)->make()]);
$this->repository->shouldReceive('getWithExportAttributes')->with($egg->id)->once()->andReturn($egg); $this->repository->shouldReceive('getWithExportAttributes')->with($egg->id)->once()->andReturn($egg);
$response = $this->service->handle($egg->id); $service = new EggExporterService($this->repository);
$response = $service->handle($egg->id);
$this->assertNotEmpty($response); $this->assertNotEmpty($response);
$data = json_decode($response); $data = json_decode($response);

View file

@ -1,13 +1,6 @@
<?php <?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Services\Sharing; namespace Tests\Unit\Services\Eggs\Sharing;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
@ -17,7 +10,6 @@ use Tests\Traits\MocksUuids;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Pterodactyl\Models\EggVariable; use Pterodactyl\Models\EggVariable;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Services\Eggs\Sharing\EggImporterService; use Pterodactyl\Services\Eggs\Sharing\EggImporterService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
@ -66,9 +58,9 @@ class EggImporterServiceTest extends TestCase
{ {
parent::setUp(); parent::setUp();
$this->file = m::mock(UploadedFile::class);
$this->connection = m::mock(ConnectionInterface::class); $this->connection = m::mock(ConnectionInterface::class);
$this->eggVariableRepository = m::mock(EggVariableRepositoryInterface::class); $this->eggVariableRepository = m::mock(EggVariableRepositoryInterface::class);
$this->file = m::mock(UploadedFile::class);
$this->nestRepository = m::mock(NestRepositoryInterface::class); $this->nestRepository = m::mock(NestRepositoryInterface::class);
$this->repository = m::mock(EggRepositoryInterface::class); $this->repository = m::mock(EggRepositoryInterface::class);
@ -82,13 +74,14 @@ class EggImporterServiceTest extends TestCase
*/ */
public function testEggConfigurationIsImported() public function testEggConfigurationIsImported()
{ {
$egg = factory(Egg::class)->make(); $egg = factory(Egg::class)->make(['id' => 123]);
$nest = factory(Nest::class)->make(); $nest = factory(Nest::class)->make(['id' => 456]);
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); $this->file->expects('getError')->andReturn(UPLOAD_ERR_OK);
$this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true); $this->file->expects('isFile')->andReturn(true);
$this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100); $this->file->expects('getSize')->andReturn(100);
$this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn(json_encode([
$this->file->expects('openFile->fread')->with(100)->once()->andReturn(json_encode([
'meta' => ['version' => 'PTDL_v1'], 'meta' => ['version' => 'PTDL_v1'],
'name' => $egg->name, 'name' => $egg->name,
'author' => $egg->author, 'author' => $egg->author,
@ -122,13 +115,18 @@ class EggImporterServiceTest extends TestCase
*/ */
public function testExceptionIsThrownIfFileIsInvalid() public function testExceptionIsThrownIfFileIsInvalid()
{ {
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_NO_FILE); $this->expectException(InvalidFileUploadException::class);
try { $this->expectExceptionMessage(
'The selected file ["test.txt"] was not in a valid format to import. (is_file: true is_valid: true err_code: 4 err: UPLOAD_ERR_NO_FILE)'
);
$this->file->expects('getFilename')->andReturns('test.txt');
$this->file->expects('isFile')->andReturns(true);
$this->file->expects('isValid')->andReturns(true);
$this->file->expects('getError')->twice()->andReturns(UPLOAD_ERR_NO_FILE);
$this->file->expects('getErrorMessage')->andReturns('UPLOAD_ERR_NO_FILE');
$this->service->handle($this->file, 1234); $this->service->handle($this->file, 1234);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(InvalidFileUploadException::class, $exception);
$this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage());
}
} }
/** /**
@ -136,15 +134,18 @@ class EggImporterServiceTest extends TestCase
*/ */
public function testExceptionIsThrownIfFileIsNotAFile() public function testExceptionIsThrownIfFileIsNotAFile()
{ {
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); $this->expectException(InvalidFileUploadException::class);
$this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(false); $this->expectExceptionMessage(
'The selected file ["test.txt"] was not in a valid format to import. (is_file: false is_valid: true err_code: 4 err: UPLOAD_ERR_NO_FILE)'
);
$this->file->expects('getFilename')->andReturns('test.txt');
$this->file->expects('isFile')->andReturns(false);
$this->file->expects('isValid')->andReturns(true);
$this->file->expects('getError')->twice()->andReturns(UPLOAD_ERR_NO_FILE);
$this->file->expects('getErrorMessage')->andReturns('UPLOAD_ERR_NO_FILE');
try {
$this->service->handle($this->file, 1234); $this->service->handle($this->file, 1234);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(InvalidFileUploadException::class, $exception);
$this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage());
}
} }
/** /**
@ -152,19 +153,18 @@ class EggImporterServiceTest extends TestCase
*/ */
public function testExceptionIsThrownIfJsonMetaDataIsInvalid() public function testExceptionIsThrownIfJsonMetaDataIsInvalid()
{ {
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); $this->expectException(InvalidFileUploadException::class);
$this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true); $this->expectExceptionMessage(trans('exceptions.nest.importer.invalid_json_provided'));
$this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100);
$this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn(json_encode([ $this->file->expects('getError')->andReturn(UPLOAD_ERR_OK);
$this->file->expects('isFile')->andReturn(true);
$this->file->expects('getSize')->andReturn(100);
$this->file->expects('openFile->fread')->with(100)->andReturn(json_encode([
'meta' => ['version' => 'hodor'], 'meta' => ['version' => 'hodor'],
])); ]));
try {
$this->service->handle($this->file, 1234); $this->service->handle($this->file, 1234);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(InvalidFileUploadException::class, $exception);
$this->assertEquals(trans('exceptions.nest.importer.invalid_json_provided'), $exception->getMessage());
}
} }
/** /**
@ -172,18 +172,16 @@ class EggImporterServiceTest extends TestCase
*/ */
public function testExceptionIsThrownIfBadJsonIsProvided() public function testExceptionIsThrownIfBadJsonIsProvided()
{ {
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); $this->expectException(BadJsonFormatException::class);
$this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true); $this->expectExceptionMessage(trans('exceptions.nest.importer.json_error', [
$this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100); 'error' => 'Syntax error',
$this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn('}'); ]));
$this->file->expects('getError')->andReturn(UPLOAD_ERR_OK);
$this->file->expects('isFile')->andReturn(true);
$this->file->expects('getSize')->andReturn(100);
$this->file->expects('openFile->fread')->with(100)->andReturn('}');
try {
$this->service->handle($this->file, 1234); $this->service->handle($this->file, 1234);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(BadJsonFormatException::class, $exception);
$this->assertEquals(trans('exceptions.nest.importer.json_error', [
'error' => json_last_error_msg(),
]), $exception->getMessage());
}
} }
} }

View file

@ -62,7 +62,7 @@ class EggUpdateImporterServiceTest extends TestCase
*/ */
public function testEggIsUpdated() public function testEggIsUpdated()
{ {
$egg = factory(Egg::class)->make(); $egg = factory(Egg::class)->make(['id' => 123]);
$variable = factory(EggVariable::class)->make(); $variable = factory(EggVariable::class)->make();
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); $this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK);
@ -91,7 +91,7 @@ class EggUpdateImporterServiceTest extends TestCase
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($egg->id, $this->file); $this->service->handle($egg, $this->file);
$this->assertTrue(true); $this->assertTrue(true);
} }
@ -101,7 +101,7 @@ class EggUpdateImporterServiceTest extends TestCase
*/ */
public function testVariablesMissingFromImportAreDeleted() public function testVariablesMissingFromImportAreDeleted()
{ {
$egg = factory(Egg::class)->make(); $egg = factory(Egg::class)->make(['id' => 123]);
$variable1 = factory(EggVariable::class)->make(); $variable1 = factory(EggVariable::class)->make();
$variable2 = factory(EggVariable::class)->make(); $variable2 = factory(EggVariable::class)->make();
@ -136,7 +136,7 @@ class EggUpdateImporterServiceTest extends TestCase
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($egg->id, $this->file); $this->service->handle($egg, $this->file);
$this->assertTrue(true); $this->assertTrue(true);
} }
@ -145,13 +145,13 @@ class EggUpdateImporterServiceTest extends TestCase
*/ */
public function testExceptionIsThrownIfFileIsInvalid() public function testExceptionIsThrownIfFileIsInvalid()
{ {
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_NO_FILE); $egg = factory(Egg::class)->make(['id' => 123]);
try {
$this->service->handle(1234, $this->file); $this->expectException(InvalidFileUploadException::class);
} catch (PterodactylException $exception) { $this->expectExceptionMessageMatches('/^The selected file \["test\.txt"\] was not in a valid format to import\./');
$this->assertInstanceOf(InvalidFileUploadException::class, $exception); $file = new UploadedFile('test.txt', 'original.txt', 'application/json', UPLOAD_ERR_NO_FILE, true);
$this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage());
} $this->service->handle($egg, $file);
} }
/** /**
@ -159,15 +159,18 @@ class EggUpdateImporterServiceTest extends TestCase
*/ */
public function testExceptionIsThrownIfFileIsNotAFile() public function testExceptionIsThrownIfFileIsNotAFile()
{ {
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); $egg = factory(Egg::class)->make(['id' => 123]);
$this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(false);
try { $this->expectException(InvalidFileUploadException::class);
$this->service->handle(1234, $this->file); $this->expectExceptionMessageMatches('/^The selected file \["test\.txt"\] was not in a valid format to import\./');
} catch (PterodactylException $exception) {
$this->assertInstanceOf(InvalidFileUploadException::class, $exception); $file = m::mock(
$this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage()); new UploadedFile('test.txt', 'original.txt', 'application/json', UPLOAD_ERR_INI_SIZE, true)
} )->makePartial();
$file->expects('isFile')->andReturnFalse();
$this->service->handle($egg, $file);
} }
/** /**
@ -175,6 +178,8 @@ class EggUpdateImporterServiceTest extends TestCase
*/ */
public function testExceptionIsThrownIfJsonMetaDataIsInvalid() public function testExceptionIsThrownIfJsonMetaDataIsInvalid()
{ {
$egg = factory(Egg::class)->make(['id' => 123]);
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); $this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK);
$this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true); $this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100); $this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100);
@ -183,7 +188,7 @@ class EggUpdateImporterServiceTest extends TestCase
])); ]));
try { try {
$this->service->handle(1234, $this->file); $this->service->handle($egg, $this->file);
} catch (PterodactylException $exception) { } catch (PterodactylException $exception) {
$this->assertInstanceOf(InvalidFileUploadException::class, $exception); $this->assertInstanceOf(InvalidFileUploadException::class, $exception);
$this->assertEquals(trans('exceptions.nest.importer.invalid_json_provided'), $exception->getMessage()); $this->assertEquals(trans('exceptions.nest.importer.invalid_json_provided'), $exception->getMessage());
@ -195,13 +200,15 @@ class EggUpdateImporterServiceTest extends TestCase
*/ */
public function testExceptionIsThrownIfBadJsonIsProvided() public function testExceptionIsThrownIfBadJsonIsProvided()
{ {
$egg = factory(Egg::class)->make(['id' => 123]);
$this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); $this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK);
$this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true); $this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100); $this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100);
$this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn('}'); $this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn('}');
try { try {
$this->service->handle(1234, $this->file); $this->service->handle($egg, $this->file);
} catch (PterodactylException $exception) { } catch (PterodactylException $exception) {
$this->assertInstanceOf(BadJsonFormatException::class, $exception); $this->assertInstanceOf(BadJsonFormatException::class, $exception);
$this->assertEquals(trans('exceptions.nest.importer.json_error', [ $this->assertEquals(trans('exceptions.nest.importer.json_error', [