From 0c2bd416ee5fc4a708742dac2586502f93513e3b Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 5 Oct 2020 21:29:35 -0700 Subject: [PATCH] Fix unit tests for eggs --- .../Admin/Nests/EggScriptController.php | 12 +- .../Admin/Nests/EggShareController.php | 4 +- .../Eggs/Scripts/InstallScriptService.php | 13 +-- .../Eggs/Sharing/EggExporterService.php | 7 -- .../Eggs/Sharing/EggImporterService.php | 7 -- .../Eggs/Sharing/EggUpdateImporterService.php | 13 ++- database/factories/ModelFactory.php | 14 ++- database/seeds/EggSeeder.php | 2 +- .../AllocationDeletionServiceTest.php | 4 +- .../Services/Eggs/EggUpdateServiceTest.php | 9 +- .../Eggs/Scripts/InstallScriptServiceTest.php | 62 +++-------- .../Eggs/Sharing/EggExporterServiceTest.php | 30 ++--- .../Eggs/Sharing/EggImporterServiceTest.php | 104 +++++++++--------- .../Sharing/EggUpdateImporterServiceTest.php | 49 +++++---- 14 files changed, 130 insertions(+), 200 deletions(-) diff --git a/app/Http/Controllers/Admin/Nests/EggScriptController.php b/app/Http/Controllers/Admin/Nests/EggScriptController.php index 298126a6f..ea8d4dfa9 100644 --- a/app/Http/Controllers/Admin/Nests/EggScriptController.php +++ b/app/Http/Controllers/Admin/Nests/EggScriptController.php @@ -1,15 +1,9 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Http\Controllers\Admin\Nests; use Illuminate\View\View; +use Pterodactyl\Models\Egg; use Illuminate\Http\RedirectResponse; use Prologue\Alerts\AlertsMessageBag; use Pterodactyl\Http\Controllers\Controller; @@ -81,14 +75,14 @@ class EggScriptController extends Controller * Handle a request to update the installation script for an Egg. * * @param \Pterodactyl\Http\Requests\Admin\Egg\EggScriptFormRequest $request - * @param int $egg + * @param \Pterodactyl\Models\Egg $egg * @return \Illuminate\Http\RedirectResponse * * @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @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->alert->success(trans('admin/nests.eggs.notices.script_updated'))->flash(); diff --git a/app/Http/Controllers/Admin/Nests/EggShareController.php b/app/Http/Controllers/Admin/Nests/EggShareController.php index 9b9403e4d..7845680e4 100644 --- a/app/Http/Controllers/Admin/Nests/EggShareController.php +++ b/app/Http/Controllers/Admin/Nests/EggShareController.php @@ -102,7 +102,7 @@ class EggShareController extends Controller * Update an existing Egg using a new imported file. * * @param \Pterodactyl\Http\Requests\Admin\Egg\EggImportFormRequest $request - * @param int $egg + * @param \Pterodactyl\Models\Egg $egg * @return \Illuminate\Http\RedirectResponse * * @throws \Pterodactyl\Exceptions\Model\DataValidationException @@ -110,7 +110,7 @@ class EggShareController extends Controller * @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException * @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->alert->success(trans('admin/nests.eggs.notices.updated_via_import'))->flash(); diff --git a/app/Services/Eggs/Scripts/InstallScriptService.php b/app/Services/Eggs/Scripts/InstallScriptService.php index d51447568..70621c32b 100644 --- a/app/Services/Eggs/Scripts/InstallScriptService.php +++ b/app/Services/Eggs/Scripts/InstallScriptService.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Services\Eggs\Scripts; @@ -40,12 +33,8 @@ class InstallScriptService * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @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 (! $this->repository->isCopyableScript(array_get($data, 'copy_script_from'), $egg->nest_id)) { throw new InvalidCopyFromException(trans('exceptions.nest.egg.invalid_copy_id')); diff --git a/app/Services/Eggs/Sharing/EggExporterService.php b/app/Services/Eggs/Sharing/EggExporterService.php index 25a7131fa..1602fc919 100644 --- a/app/Services/Eggs/Sharing/EggExporterService.php +++ b/app/Services/Eggs/Sharing/EggExporterService.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Services\Eggs\Sharing; diff --git a/app/Services/Eggs/Sharing/EggImporterService.php b/app/Services/Eggs/Sharing/EggImporterService.php index 006360cfa..96c4c72eb 100644 --- a/app/Services/Eggs/Sharing/EggImporterService.php +++ b/app/Services/Eggs/Sharing/EggImporterService.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Services\Eggs\Sharing; diff --git a/app/Services/Eggs/Sharing/EggUpdateImporterService.php b/app/Services/Eggs/Sharing/EggUpdateImporterService.php index 3acf3f90e..91b8d2b2e 100644 --- a/app/Services/Eggs/Sharing/EggUpdateImporterService.php +++ b/app/Services/Eggs/Sharing/EggUpdateImporterService.php @@ -2,6 +2,7 @@ namespace Pterodactyl\Services\Eggs\Sharing; +use Pterodactyl\Models\Egg; use Illuminate\Http\UploadedFile; use Illuminate\Database\ConnectionInterface; use Pterodactyl\Contracts\Repository\EggRepositoryInterface; @@ -46,7 +47,7 @@ class EggUpdateImporterService /** * Update an existing Egg using an uploaded JSON file. * - * @param int $egg + * @param \Pterodactyl\Models\Egg $egg * @param \Illuminate\Http\UploadedFile $file * * @throws \Pterodactyl\Exceptions\Model\DataValidationException @@ -54,7 +55,7 @@ class EggUpdateImporterService * @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException * @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()) { throw new InvalidFileUploadException( @@ -81,7 +82,7 @@ class EggUpdateImporterService } $this->connection->beginTransaction(); - $this->repository->update($egg, [ + $this->repository->update($egg->id, [ 'author' => object_get($parsed, 'author'), 'name' => object_get($parsed, 'name'), 'description' => object_get($parsed, 'description'), @@ -99,19 +100,19 @@ class EggUpdateImporterService // Update Existing Variables collect($parsed->variables)->each(function ($variable) use ($egg) { $this->variableRepository->withoutFreshModel()->updateOrCreate([ - 'egg_id' => $egg, + 'egg_id' => $egg->id, 'env_variable' => $variable->env_variable, ], collect($variable)->except(['egg_id', '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. collect($existing)->each(function ($variable) use ($egg, $imported) { if (! in_array($variable->env_variable, $imported)) { $this->variableRepository->deleteWhere([ - ['egg_id', '=', $egg], + ['egg_id', '=', $egg->id], ['env_variable', '=', $variable->env_variable], ]); } diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 0525c30be..4997a9b6f 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -1,5 +1,6 @@ define(Pterodactyl\Models\Server::class, function (Faker $faker) { 'installed' => 1, 'database_limit' => null, 'allocation_limit' => null, - 'created_at' => \Carbon\Carbon::now(), - 'updated_at' => \Carbon\Carbon::now(), + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now(), ]; }); @@ -160,8 +162,8 @@ $factory->define(Pterodactyl\Models\Database::class, function (Faker $faker) { 'username' => str_random(10), 'remote' => '%', 'password' => $password ?: bcrypt('test123'), - 'created_at' => \Carbon\Carbon::now()->toDateTimeString(), - 'updated_at' => \Carbon\Carbon::now()->toDateTimeString(), + 'created_at' => 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)), 'allowed_ips' => null, 'memo' => 'Test Function Key', - 'created_at' => \Carbon\Carbon::now()->toDateTimeString(), - 'updated_at' => \Carbon\Carbon::now()->toDateTimeString(), + 'created_at' => Carbon::now()->toDateTimeString(), + 'updated_at' => Carbon::now()->toDateTimeString(), ]; }); diff --git a/database/seeds/EggSeeder.php b/database/seeds/EggSeeder.php index c532d556c..dea5b675e 100644 --- a/database/seeds/EggSeeder.php +++ b/database/seeds/EggSeeder.php @@ -130,7 +130,7 @@ class EggSeeder extends Seeder ['nest_id', '=', $nest->id], ]); - $this->updateImporterService->handle($egg->id, $file); + $this->updateImporterService->handle($egg, $file); $this->command->info('Updated ' . $decoded->name); } catch (RecordNotFoundException $exception) { diff --git a/tests/Unit/Services/Allocations/AllocationDeletionServiceTest.php b/tests/Unit/Services/Allocations/AllocationDeletionServiceTest.php index 521aed20a..055aedaa1 100644 --- a/tests/Unit/Services/Allocations/AllocationDeletionServiceTest.php +++ b/tests/Unit/Services/Allocations/AllocationDeletionServiceTest.php @@ -28,9 +28,9 @@ class AllocationDeletionServiceTest extends TestCase */ 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); $this->assertEquals(1, $response); diff --git a/tests/Unit/Services/Eggs/EggUpdateServiceTest.php b/tests/Unit/Services/Eggs/EggUpdateServiceTest.php index ec6cd4a25..0ffe4f373 100644 --- a/tests/Unit/Services/Eggs/EggUpdateServiceTest.php +++ b/tests/Unit/Services/Eggs/EggUpdateServiceTest.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Tests\Unit\Services\Services\Options; @@ -41,7 +34,7 @@ class EggUpdateServiceTest extends TestCase { parent::setUp(); - $this->model = factory(Egg::class)->make(); + $this->model = factory(Egg::class)->make(['id' => 123]); $this->repository = m::mock(EggRepositoryInterface::class); $this->service = new EggUpdateService($this->repository); diff --git a/tests/Unit/Services/Eggs/Scripts/InstallScriptServiceTest.php b/tests/Unit/Services/Eggs/Scripts/InstallScriptServiceTest.php index 02388af94..cf3987937 100644 --- a/tests/Unit/Services/Eggs/Scripts/InstallScriptServiceTest.php +++ b/tests/Unit/Services/Eggs/Scripts/InstallScriptServiceTest.php @@ -1,13 +1,6 @@ . - * - * 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 Mockery as m; @@ -30,21 +23,11 @@ class InstallScriptServiceTest extends TestCase 'copy_script_from' => null, ]; - /** - * @var \Pterodactyl\Models\Egg - */ - protected $model; - /** * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock */ protected $repository; - /** - * @var \Pterodactyl\Services\Eggs\Scripts\InstallScriptService - */ - protected $service; - /** * Setup tests. */ @@ -52,10 +35,7 @@ class InstallScriptServiceTest extends TestCase { parent::setUp(); - $this->model = factory(Egg::class)->make(); $this->repository = m::mock(EggRepositoryInterface::class); - - $this->service = new InstallScriptService($this->repository); } /** @@ -63,13 +43,13 @@ class InstallScriptServiceTest extends TestCase */ public function testUpdateWithValidCopyScriptFromAttribute() { + $model = factory(Egg::class)->make(['id' => 123, 'nest_id' => 456]); $this->data['copy_script_from'] = 1; - $this->repository->shouldReceive('isCopyableScript')->with(1, $this->model->nest_id)->once()->andReturn(true); - $this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull(); + $this->repository->shouldReceive('isCopyableScript')->with(1, $model->nest_id)->once()->andReturn(true); + $this->repository->expects('withoutFreshModel->update')->with($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->repository->shouldReceive('isCopyableScript')->with(1, $this->model->nest_id)->once()->andReturn(false); - try { - $this->service->handle($this->model, $this->data); - } catch (Exception $exception) { - $this->assertInstanceOf(InvalidCopyFromException::class, $exception); - $this->assertEquals(trans('exceptions.nest.egg.invalid_copy_id'), $exception->getMessage()); - } + $this->expectException(InvalidCopyFromException::class); + $this->expectExceptionMessage(trans('exceptions.nest.egg.invalid_copy_id')); + + $model = factory(Egg::class)->make(['id' => 123, 'nest_id' => 456]); + + $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() { - $this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull(); + $model = factory(Egg::class)->make(['id' => 123, 'nest_id' => 456]); - $this->service->handle($this->model, $this->data); + $this->repository->expects('withoutFreshModel->update')->with($model->id, $this->data)->andReturnNull(); + + $this->getService()->handle($model, $this->data); } - /** - * Test that an integer can be passed in place of a model. - */ - public function testFunctionAcceptsIntegerInPlaceOfModel() + private function getService() { - $this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model); - $this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull(); - - $this->service->handle($this->model->id, $this->data); + return new InstallScriptService($this->repository); } } diff --git a/tests/Unit/Services/Eggs/Sharing/EggExporterServiceTest.php b/tests/Unit/Services/Eggs/Sharing/EggExporterServiceTest.php index 416186c64..630f7e106 100644 --- a/tests/Unit/Services/Eggs/Sharing/EggExporterServiceTest.php +++ b/tests/Unit/Services/Eggs/Sharing/EggExporterServiceTest.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Tests\Unit\Services\Eggs\Sharing; @@ -22,21 +15,11 @@ class EggExporterServiceTest extends TestCase { use NestedObjectAssertionsTrait; - /** - * @var \Carbon\Carbon - */ - protected $carbon; - /** * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock */ protected $repository; - /** - * @var \Pterodactyl\Services\Eggs\Sharing\EggExporterService - */ - protected $service; - /** * Setup tests. */ @@ -45,10 +28,8 @@ class EggExporterServiceTest extends TestCase parent::setUp(); 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() { - $egg = factory(Egg::class)->make(); + $egg = factory(Egg::class)->make([ + 'id' => 123, + 'nest_id' => 456, + ]); $egg->variables = collect([$variable = factory(EggVariable::class)->make()]); $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); $data = json_decode($response); diff --git a/tests/Unit/Services/Eggs/Sharing/EggImporterServiceTest.php b/tests/Unit/Services/Eggs/Sharing/EggImporterServiceTest.php index 48378a6c3..0d53d5a7e 100644 --- a/tests/Unit/Services/Eggs/Sharing/EggImporterServiceTest.php +++ b/tests/Unit/Services/Eggs/Sharing/EggImporterServiceTest.php @@ -1,13 +1,6 @@ . - * - * 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 Tests\TestCase; @@ -17,7 +10,6 @@ use Tests\Traits\MocksUuids; use Illuminate\Http\UploadedFile; use Pterodactyl\Models\EggVariable; use Illuminate\Database\ConnectionInterface; -use Pterodactyl\Exceptions\PterodactylException; use Pterodactyl\Services\Eggs\Sharing\EggImporterService; use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface; @@ -66,9 +58,9 @@ class EggImporterServiceTest extends TestCase { parent::setUp(); + $this->file = m::mock(UploadedFile::class); $this->connection = m::mock(ConnectionInterface::class); $this->eggVariableRepository = m::mock(EggVariableRepositoryInterface::class); - $this->file = m::mock(UploadedFile::class); $this->nestRepository = m::mock(NestRepositoryInterface::class); $this->repository = m::mock(EggRepositoryInterface::class); @@ -82,13 +74,14 @@ class EggImporterServiceTest extends TestCase */ public function testEggConfigurationIsImported() { - $egg = factory(Egg::class)->make(); - $nest = factory(Nest::class)->make(); + $egg = factory(Egg::class)->make(['id' => 123]); + $nest = factory(Nest::class)->make(['id' => 456]); - $this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); - $this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true); - $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)->once()->andReturn(json_encode([ 'meta' => ['version' => 'PTDL_v1'], 'name' => $egg->name, 'author' => $egg->author, @@ -122,13 +115,18 @@ class EggImporterServiceTest extends TestCase */ public function testExceptionIsThrownIfFileIsInvalid() { - $this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_NO_FILE); - try { - $this->service->handle($this->file, 1234); - } catch (PterodactylException $exception) { - $this->assertInstanceOf(InvalidFileUploadException::class, $exception); - $this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage()); - } + $this->expectException(InvalidFileUploadException::class); + $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); } /** @@ -136,15 +134,18 @@ class EggImporterServiceTest extends TestCase */ public function testExceptionIsThrownIfFileIsNotAFile() { - $this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); - $this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(false); + $this->expectException(InvalidFileUploadException::class); + $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)' + ); - try { - $this->service->handle($this->file, 1234); - } catch (PterodactylException $exception) { - $this->assertInstanceOf(InvalidFileUploadException::class, $exception); - $this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage()); - } + $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'); + + $this->service->handle($this->file, 1234); } /** @@ -152,19 +153,18 @@ class EggImporterServiceTest extends TestCase */ public function testExceptionIsThrownIfJsonMetaDataIsInvalid() { - $this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); - $this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true); - $this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100); - $this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn(json_encode([ + $this->expectException(InvalidFileUploadException::class); + $this->expectExceptionMessage(trans('exceptions.nest.importer.invalid_json_provided')); + + $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'], ])); - try { - $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()); - } + $this->service->handle($this->file, 1234); } /** @@ -172,18 +172,16 @@ class EggImporterServiceTest extends TestCase */ public function testExceptionIsThrownIfBadJsonIsProvided() { - $this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); - $this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true); - $this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100); - $this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn('}'); + $this->expectException(BadJsonFormatException::class); + $this->expectExceptionMessage(trans('exceptions.nest.importer.json_error', [ + 'error' => 'Syntax error', + ])); - try { - $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()); - } + $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('}'); + + $this->service->handle($this->file, 1234); } } diff --git a/tests/Unit/Services/Eggs/Sharing/EggUpdateImporterServiceTest.php b/tests/Unit/Services/Eggs/Sharing/EggUpdateImporterServiceTest.php index 18b26868d..a73fc1cda 100644 --- a/tests/Unit/Services/Eggs/Sharing/EggUpdateImporterServiceTest.php +++ b/tests/Unit/Services/Eggs/Sharing/EggUpdateImporterServiceTest.php @@ -62,7 +62,7 @@ class EggUpdateImporterServiceTest extends TestCase */ public function testEggIsUpdated() { - $egg = factory(Egg::class)->make(); + $egg = factory(Egg::class)->make(['id' => 123]); $variable = factory(EggVariable::class)->make(); $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->service->handle($egg->id, $this->file); + $this->service->handle($egg, $this->file); $this->assertTrue(true); } @@ -101,7 +101,7 @@ class EggUpdateImporterServiceTest extends TestCase */ public function testVariablesMissingFromImportAreDeleted() { - $egg = factory(Egg::class)->make(); + $egg = factory(Egg::class)->make(['id' => 123]); $variable1 = 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->service->handle($egg->id, $this->file); + $this->service->handle($egg, $this->file); $this->assertTrue(true); } @@ -145,13 +145,13 @@ class EggUpdateImporterServiceTest extends TestCase */ public function testExceptionIsThrownIfFileIsInvalid() { - $this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_NO_FILE); - try { - $this->service->handle(1234, $this->file); - } catch (PterodactylException $exception) { - $this->assertInstanceOf(InvalidFileUploadException::class, $exception); - $this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage()); - } + $egg = factory(Egg::class)->make(['id' => 123]); + + $this->expectException(InvalidFileUploadException::class); + $this->expectExceptionMessageMatches('/^The selected file \["test\.txt"\] was not in a valid format to import\./'); + $file = new UploadedFile('test.txt', 'original.txt', 'application/json', UPLOAD_ERR_NO_FILE, true); + + $this->service->handle($egg, $file); } /** @@ -159,15 +159,18 @@ class EggUpdateImporterServiceTest extends TestCase */ public function testExceptionIsThrownIfFileIsNotAFile() { - $this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); - $this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(false); + $egg = factory(Egg::class)->make(['id' => 123]); - try { - $this->service->handle(1234, $this->file); - } catch (PterodactylException $exception) { - $this->assertInstanceOf(InvalidFileUploadException::class, $exception); - $this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage()); - } + $this->expectException(InvalidFileUploadException::class); + $this->expectExceptionMessageMatches('/^The selected file \["test\.txt"\] was not in a valid format to import\./'); + + $file = m::mock( + 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() { + $egg = factory(Egg::class)->make(['id' => 123]); + $this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); $this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true); $this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100); @@ -183,7 +188,7 @@ class EggUpdateImporterServiceTest extends TestCase ])); try { - $this->service->handle(1234, $this->file); + $this->service->handle($egg, $this->file); } catch (PterodactylException $exception) { $this->assertInstanceOf(InvalidFileUploadException::class, $exception); $this->assertEquals(trans('exceptions.nest.importer.invalid_json_provided'), $exception->getMessage()); @@ -195,13 +200,15 @@ class EggUpdateImporterServiceTest extends TestCase */ public function testExceptionIsThrownIfBadJsonIsProvided() { + $egg = factory(Egg::class)->make(['id' => 123]); + $this->file->shouldReceive('getError')->withNoArgs()->once()->andReturn(UPLOAD_ERR_OK); $this->file->shouldReceive('isFile')->withNoArgs()->once()->andReturn(true); $this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(100); $this->file->shouldReceive('openFile->fread')->with(100)->once()->andReturn('}'); try { - $this->service->handle(1234, $this->file); + $this->service->handle($egg, $this->file); } catch (PterodactylException $exception) { $this->assertInstanceOf(BadJsonFormatException::class, $exception); $this->assertEquals(trans('exceptions.nest.importer.json_error', [