From 787346525b754fe3cba27389fb57d120c879c451 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 7 Oct 2017 23:29:08 -0500 Subject: [PATCH] Update a batch of failing tests --- .../Eggs/Sharing/EggImporterService.php | 24 ++-- .../Variables/VariableCreationService.php | 24 ++-- app/Services/Nests/NestCreationService.php | 6 +- tests/TestCase.php | 14 +++ tests/Traits/KnownUuid.php | 47 ++++++++ .../Scripts/InstallScriptServiceTest.php} | 16 +-- .../Sharing/EggExporterServiceTest.php} | 20 ++-- .../Sharing/EggImporterServiceTest.php} | 108 ++++++------------ .../Variables/VariableCreationServiceTest.php | 56 ++++----- .../Variables/VariableUpdateServiceTest.php | 42 +++++-- .../NestCreationServiceTest.php} | 27 ++--- .../NestDeletionServiceTest.php} | 21 ++-- .../NestUpdateServiceTest.php} | 8 +- 13 files changed, 218 insertions(+), 195 deletions(-) create mode 100644 tests/Traits/KnownUuid.php rename tests/Unit/Services/{Services/Options/InstallScriptUpdateServiceTest.php => Eggs/Scripts/InstallScriptServiceTest.php} (86%) rename tests/Unit/Services/{Services/Sharing/ServiceOptionExporterServiceTest.php => Eggs/Sharing/EggExporterServiceTest.php} (76%) rename tests/Unit/Services/{Services/Sharing/ServiceOptionImporterServiceTest.php => Eggs/Sharing/EggImporterServiceTest.php} (55%) rename tests/Unit/Services/{Services => Eggs}/Variables/VariableCreationServiceTest.php (58%) rename tests/Unit/Services/{Services => Eggs}/Variables/VariableUpdateServiceTest.php (73%) rename tests/Unit/Services/{Services/ServiceCreationServiceTest.php => Nests/NestCreationServiceTest.php} (68%) rename tests/Unit/Services/{Services/ServiceDeletionServiceTest.php => Nests/NestDeletionServiceTest.php} (81%) rename tests/Unit/Services/{Services/ServiceUpdateServiceTest.php => Nests/NestUpdateServiceTest.php} (90%) diff --git a/app/Services/Eggs/Sharing/EggImporterService.php b/app/Services/Eggs/Sharing/EggImporterService.php index abc74f647..d42c51ccf 100644 --- a/app/Services/Eggs/Sharing/EggImporterService.php +++ b/app/Services/Eggs/Sharing/EggImporterService.php @@ -30,34 +30,34 @@ class EggImporterService */ protected $eggVariableRepository; + /** + * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface + */ + protected $nestRepository; + /** * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface */ protected $repository; - /** - * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface - */ - protected $serviceRepository; - /** * EggImporterService constructor. * * @param \Illuminate\Database\ConnectionInterface $connection * @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $repository * @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $eggVariableRepository - * @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $serviceRepository + * @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $nestRepository */ public function __construct( ConnectionInterface $connection, EggRepositoryInterface $repository, EggVariableRepositoryInterface $eggVariableRepository, - NestRepositoryInterface $serviceRepository + NestRepositoryInterface $nestRepository ) { $this->connection = $connection; - $this->repository = $repository; - $this->serviceRepository = $serviceRepository; $this->eggVariableRepository = $eggVariableRepository; + $this->repository = $repository; + $this->nestRepository = $nestRepository; } /** @@ -74,16 +74,16 @@ class EggImporterService public function handle(UploadedFile $file, int $nest): Egg { if (! $file->isValid() || ! $file->isFile()) { - throw new InvalidFileUploadException(trans('exceptions.egg.importer.file_error')); + throw new InvalidFileUploadException(trans('exceptions.nest.importer.file_error')); } $parsed = json_decode($file->openFile()->fread($file->getSize())); if (object_get($parsed, 'meta.version') !== 'PTDL_v1') { - throw new InvalidFileUploadException(trans('exceptions.egg.importer.invalid_json_provided')); + throw new InvalidFileUploadException(trans('exceptions.nest.importer.invalid_json_provided')); } - $nest = $this->serviceRepository->getWithEggs($nest); + $nest = $this->nestRepository->getWithEggs($nest); $this->connection->beginTransaction(); $egg = $this->repository->create([ diff --git a/app/Services/Eggs/Variables/VariableCreationService.php b/app/Services/Eggs/Variables/VariableCreationService.php index 76aac4456..920ca312e 100644 --- a/app/Services/Eggs/Variables/VariableCreationService.php +++ b/app/Services/Eggs/Variables/VariableCreationService.php @@ -10,34 +10,24 @@ namespace Pterodactyl\Services\Eggs\Variables; use Pterodactyl\Models\EggVariable; -use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface; use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException; class VariableCreationService { - /** - * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface - */ - protected $eggRepository; - /** * @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface */ - protected $variableRepository; + protected $repository; /** * VariableCreationService constructor. * - * @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $eggRepository - * @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $variableRepository + * @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $repository */ - public function __construct( - EggRepositoryInterface $eggRepository, - EggVariableRepositoryInterface $variableRepository - ) { - $this->eggRepository = $eggRepository; - $this->variableRepository = $variableRepository; + public function __construct(EggVariableRepositoryInterface $repository) + { + $this->repository = $repository; } /** @@ -61,10 +51,10 @@ class VariableCreationService $options = array_get($data, 'options', []); - return $this->variableRepository->create(array_merge([ + return $this->repository->create(array_merge($data, [ 'egg_id' => $egg, 'user_viewable' => in_array('user_viewable', $options), 'user_editable' => in_array('user_editable', $options), - ], $data)); + ])); } } diff --git a/app/Services/Nests/NestCreationService.php b/app/Services/Nests/NestCreationService.php index 797ad8a42..cad638844 100644 --- a/app/Services/Nests/NestCreationService.php +++ b/app/Services/Nests/NestCreationService.php @@ -32,10 +32,8 @@ class NestCreationService * @param \Illuminate\Contracts\Config\Repository $config * @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $repository */ - public function __construct( - ConfigRepository $config, - NestRepositoryInterface $repository - ) { + public function __construct(ConfigRepository $config, NestRepositoryInterface $repository) + { $this->config = $config; $this->repository = $repository; } diff --git a/tests/TestCase.php b/tests/TestCase.php index 664e4a9c3..5f9ba7482 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -8,8 +8,22 @@ abstract class TestCase extends BaseTestCase { use CreatesApplication; + /** + * Setup tests. + */ public function setUp() { parent::setUp(); + + $this->setKnownUuidFactory(); + } + + /** + * Handles the known UUID handling in certain unit tests. Use the "KnownUuid" trait + * in order to enable this ability. + */ + public function setKnownUuidFactory() + { + // do nothing } } diff --git a/tests/Traits/KnownUuid.php b/tests/Traits/KnownUuid.php new file mode 100644 index 000000000..03d198380 --- /dev/null +++ b/tests/Traits/KnownUuid.php @@ -0,0 +1,47 @@ +. + * + * This software is licensed under the terms of the MIT license. + * https://opensource.org/licenses/MIT + */ + +namespace Tests\Traits; + +use Mockery as m; +use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\UuidFactory; + +trait KnownUuid +{ + /** + * The known UUID string. + * + * @var string + */ + protected $knownUuid = 'ffb5c3a6-ab17-43ab-97f0-8ff37ccd7f5f'; + + /** + * Setup a factory mock to produce the same UUID whenever called. + */ + public function setKnownUuidFactory() + { + $uuid = Uuid::fromString($this->getKnownUuid()); + $factoryMock = m::mock(UuidFactory::class . '[uuid4]', [ + 'uuid4' => $uuid, + ]); + + Uuid::setFactory($factoryMock); + } + + /** + * Returns the known UUID for tests to use. + * + * @return string + */ + public function getKnownUuid(): string + { + return $this->knownUuid; + } +} diff --git a/tests/Unit/Services/Services/Options/InstallScriptUpdateServiceTest.php b/tests/Unit/Services/Eggs/Scripts/InstallScriptServiceTest.php similarity index 86% rename from tests/Unit/Services/Services/Options/InstallScriptUpdateServiceTest.php rename to tests/Unit/Services/Eggs/Scripts/InstallScriptServiceTest.php index 2492dc600..ac29f44c4 100644 --- a/tests/Unit/Services/Services/Options/InstallScriptUpdateServiceTest.php +++ b/tests/Unit/Services/Eggs/Scripts/InstallScriptServiceTest.php @@ -13,11 +13,11 @@ use Exception; use Mockery as m; use Tests\TestCase; use Pterodactyl\Models\Egg; +use Pterodactyl\Services\Eggs\Scripts\InstallScriptService; use Pterodactyl\Contracts\Repository\EggRepositoryInterface; -use Pterodactyl\Services\Services\Options\InstallScriptService; -use Pterodactyl\Exceptions\Service\ServiceOption\InvalidCopyFromException; +use Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException; -class InstallScriptUpdateServiceTest extends TestCase +class InstallScriptServiceTest extends TestCase { /** * @var array @@ -36,12 +36,12 @@ class InstallScriptUpdateServiceTest extends TestCase protected $model; /** - * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock */ protected $repository; /** - * @var \Pterodactyl\Services\Services\Options\InstallScriptService + * @var \Pterodactyl\Services\Eggs\Scripts\InstallScriptService */ protected $service; @@ -65,7 +65,7 @@ class InstallScriptUpdateServiceTest extends TestCase { $this->data['copy_script_from'] = 1; - $this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->service_id)->once()->andReturn(true); + $this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(true); $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull(); @@ -79,12 +79,12 @@ class InstallScriptUpdateServiceTest extends TestCase { $this->data['copy_script_from'] = 1; - $this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->service_id)->once()->andReturn(false); + $this->repository->shouldReceive('isCopiableScript')->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.service.options.invalid_copy_id'), $exception->getMessage()); + $this->assertEquals(trans('exceptions.nest.egg.invalid_copy_id'), $exception->getMessage()); } } diff --git a/tests/Unit/Services/Services/Sharing/ServiceOptionExporterServiceTest.php b/tests/Unit/Services/Eggs/Sharing/EggExporterServiceTest.php similarity index 76% rename from tests/Unit/Services/Services/Sharing/ServiceOptionExporterServiceTest.php rename to tests/Unit/Services/Eggs/Sharing/EggExporterServiceTest.php index 60f9a2eb9..c40531b97 100644 --- a/tests/Unit/Services/Services/Sharing/ServiceOptionExporterServiceTest.php +++ b/tests/Unit/Services/Eggs/Sharing/EggExporterServiceTest.php @@ -7,7 +7,7 @@ * https://opensource.org/licenses/MIT */ -namespace Tests\Unit\Services\Services\Sharing; +namespace Tests\Unit\Services\Eggs\Sharing; use Mockery as m; use Carbon\Carbon; @@ -15,10 +15,10 @@ use Tests\TestCase; use Pterodactyl\Models\Egg; use Pterodactyl\Models\EggVariable; use Tests\Assertions\NestedObjectAssertionsTrait; +use Pterodactyl\Services\Eggs\Sharing\EggExporterService; use Pterodactyl\Contracts\Repository\EggRepositoryInterface; -use Pterodactyl\Services\Services\Sharing\ServiceOptionExporterService; -class ServiceOptionExporterServiceTest extends TestCase +class EggExporterServiceTest extends TestCase { use NestedObjectAssertionsTrait; @@ -33,7 +33,7 @@ class ServiceOptionExporterServiceTest extends TestCase protected $repository; /** - * @var \Pterodactyl\Services\Services\Sharing\ServiceOptionExporterService + * @var \Pterodactyl\Services\Eggs\Sharing\EggExporterService */ protected $service; @@ -48,7 +48,7 @@ class ServiceOptionExporterServiceTest extends TestCase $this->carbon = new Carbon(); $this->repository = m::mock(EggRepositoryInterface::class); - $this->service = new ServiceOptionExporterService($this->carbon, $this->repository); + $this->service = new EggExporterService($this->repository); } /** @@ -56,18 +56,20 @@ class ServiceOptionExporterServiceTest extends TestCase */ public function testJsonStructureIsExported() { - $option = factory(Egg::class)->make(); - $option->variables = collect([$variable = factory(EggVariable::class)->make()]); + $egg = factory(Egg::class)->make(); + $egg->variables = collect([$variable = factory(EggVariable::class)->make()]); - $this->repository->shouldReceive('getWithExportAttributes')->with($option->id)->once()->andReturn($option); + $this->repository->shouldReceive('getWithExportAttributes')->with($egg->id)->once()->andReturn($egg); - $response = $this->service->handle($option->id); + $response = $this->service->handle($egg->id); $this->assertNotEmpty($response); $data = json_decode($response); $this->assertEquals(JSON_ERROR_NONE, json_last_error()); $this->assertObjectHasNestedAttribute('meta.version', $data); $this->assertObjectNestedValueEquals('meta.version', 'PTDL_v1', $data); + $this->assertObjectHasNestedAttribute('author', $data); + $this->assertObjectNestedValueEquals('author', $egg->author, $data); $this->assertObjectHasNestedAttribute('exported_at', $data); $this->assertObjectNestedValueEquals('exported_at', Carbon::now()->toIso8601String(), $data); $this->assertObjectHasNestedAttribute('scripts.installation.script', $data); diff --git a/tests/Unit/Services/Services/Sharing/ServiceOptionImporterServiceTest.php b/tests/Unit/Services/Eggs/Sharing/EggImporterServiceTest.php similarity index 55% rename from tests/Unit/Services/Services/Sharing/ServiceOptionImporterServiceTest.php rename to tests/Unit/Services/Eggs/Sharing/EggImporterServiceTest.php index db9992db6..7e8c37601 100644 --- a/tests/Unit/Services/Services/Sharing/ServiceOptionImporterServiceTest.php +++ b/tests/Unit/Services/Eggs/Sharing/EggImporterServiceTest.php @@ -11,57 +11,53 @@ namespace Tests\Unit\Services\Services\Sharing; use Mockery as m; use Tests\TestCase; -use Ramsey\Uuid\Uuid; use Pterodactyl\Models\Egg; +use Tests\Traits\KnownUuid; use Pterodactyl\Models\Nest; 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; -use Pterodactyl\Services\Services\Sharing\EggImporterService; use Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException; -use Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface; -use Pterodactyl\Exceptions\Service\ServiceOption\DuplicateOptionTagException; +use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface; -class ServiceOptionImporterServiceTest extends TestCase +class EggImporterServiceTest extends TestCase { + use KnownUuid; + /** * @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock */ protected $connection; + /** + * @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock + */ + protected $eggVariableRepository; + /** * @var \Illuminate\Http\UploadedFile|\Mockery\Mock */ protected $file; + /** + * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock + */ + protected $nestRepository; + /** * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock */ protected $repository; /** - * @var \Pterodactyl\Services\Services\Sharing\EggImporterService + * @var \Pterodactyl\Services\Eggs\Sharing\EggImporterService */ protected $service; - /** - * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock - */ - protected $serviceRepository; - - /** - * @var \Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface|\Mockery\Mock - */ - protected $serviceVariableRepository; - - /** - * @var \Ramsey\Uuid\Uuid|\Mockery\Mock - */ - protected $uuid; - /** * Setup tests. */ @@ -70,58 +66,54 @@ class ServiceOptionImporterServiceTest extends TestCase parent::setUp(); $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); - $this->serviceRepository = m::mock(NestRepositoryInterface::class); - $this->serviceVariableRepository = m::mock(ServiceVariableRepositoryInterface::class); - $this->uuid = m::mock('overload:' . Uuid::class); $this->service = new EggImporterService( - $this->connection, $this->serviceRepository, $this->repository, $this->serviceVariableRepository + $this->connection, $this->repository, $this->eggVariableRepository, $this->nestRepository ); } /** * Test that a service option can be successfully imported. */ - public function testServiceOptionIsImported() + public function testEggConfigurationIsImported() { - $option = factory(Egg::class)->make(); - $service = factory(Nest::class)->make(); - $service->options = collect([factory(Egg::class)->make()]); + $egg = factory(Egg::class)->make(); + $nest = factory(Nest::class)->make(); $this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true); $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([ 'meta' => ['version' => 'PTDL_v1'], - 'name' => $option->name, - 'tag' => $option->tag, + 'name' => $egg->name, + 'tag' => $egg->tag, 'variables' => [ $variable = factory(EggVariable::class)->make(), ], ])); - $this->serviceRepository->shouldReceive('getWithOptions')->with($service->id)->once()->andReturn($service); + $this->nestRepository->shouldReceive('getWithEggs')->with($nest->id)->once()->andReturn($nest); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); - $this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn($option->uuid); $this->repository->shouldReceive('create')->with(m::subset([ - 'uuid' => $option->uuid, - 'service_id' => $service->id, - 'name' => $option->name, - 'tag' => $option->tag, - ]), true, true)->once()->andReturn($option); + 'uuid' => $this->getKnownUuid(), + 'nest_id' => $nest->id, + 'name' => $egg->name, + ]), true, true)->once()->andReturn($egg); - $this->serviceVariableRepository->shouldReceive('create')->with(m::subset([ - 'option_id' => $option->id, + $this->eggVariableRepository->shouldReceive('create')->with(m::subset([ + 'egg_id' => $egg->id, 'env_variable' => $variable->env_variable, ]))->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); - $response = $this->service->handle($this->file, $service->id); + $response = $this->service->handle($this->file, $nest->id); $this->assertNotEmpty($response); $this->assertInstanceOf(Egg::class, $response); - $this->assertSame($option, $response); + $this->assertSame($egg, $response); } /** @@ -134,7 +126,7 @@ class ServiceOptionImporterServiceTest extends TestCase $this->service->handle($this->file, 1234); } catch (PterodactylException $exception) { $this->assertInstanceOf(InvalidFileUploadException::class, $exception); - $this->assertEquals(trans('exceptions.service.exporter.import_file_error'), $exception->getMessage()); + $this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage()); } } @@ -150,7 +142,7 @@ class ServiceOptionImporterServiceTest extends TestCase $this->service->handle($this->file, 1234); } catch (PterodactylException $exception) { $this->assertInstanceOf(InvalidFileUploadException::class, $exception); - $this->assertEquals(trans('exceptions.service.exporter.import_file_error'), $exception->getMessage()); + $this->assertEquals(trans('exceptions.nest.importer.file_error'), $exception->getMessage()); } } @@ -170,33 +162,7 @@ class ServiceOptionImporterServiceTest extends TestCase $this->service->handle($this->file, 1234); } catch (PterodactylException $exception) { $this->assertInstanceOf(InvalidFileUploadException::class, $exception); - $this->assertEquals(trans('exceptions.service.exporter.invalid_json_provided'), $exception->getMessage()); - } - } - - /** - * Test that an exception is thrown if a duplicate tag exists. - */ - public function testExceptionIsThrownIfDuplicateTagExists() - { - $option = factory(Egg::class)->make(); - $service = factory(Nest::class)->make(); - $service->options = collect([factory(Egg::class)->make(['tag' => $option->tag])]); - - $this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true); - $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([ - 'meta' => ['version' => 'PTDL_v1'], - 'tag' => $option->tag, - ])); - $this->serviceRepository->shouldReceive('getWithOptions')->with($service->id)->once()->andReturn($service); - - try { - $this->service->handle($this->file, $service->id); - } catch (PterodactylException $exception) { - $this->assertInstanceOf(DuplicateOptionTagException::class, $exception); - $this->assertEquals(trans('exceptions.service.options.duplicate_tag'), $exception->getMessage()); + $this->assertEquals(trans('exceptions.nest.importer.invalid_json_provided'), $exception->getMessage()); } } } diff --git a/tests/Unit/Services/Services/Variables/VariableCreationServiceTest.php b/tests/Unit/Services/Eggs/Variables/VariableCreationServiceTest.php similarity index 58% rename from tests/Unit/Services/Services/Variables/VariableCreationServiceTest.php rename to tests/Unit/Services/Eggs/Variables/VariableCreationServiceTest.php index dc0303202..320d85aa5 100644 --- a/tests/Unit/Services/Services/Variables/VariableCreationServiceTest.php +++ b/tests/Unit/Services/Eggs/Variables/VariableCreationServiceTest.php @@ -7,30 +7,24 @@ * https://opensource.org/licenses/MIT */ -namespace Tests\Unit\Services\Services\Variables; +namespace Tests\Unit\Services\Eggs\Variables; use Mockery as m; use Tests\TestCase; use Pterodactyl\Models\Egg; use Pterodactyl\Models\EggVariable; -use Pterodactyl\Contracts\Repository\EggRepositoryInterface; -use Pterodactyl\Services\Services\Variables\VariableCreationService; -use Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface; +use Pterodactyl\Services\Eggs\Variables\VariableCreationService; +use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface; class VariableCreationServiceTest extends TestCase { /** - * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock */ - protected $serviceOptionRepository; + protected $repository; /** - * @var \Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface - */ - protected $serviceVariableRepository; - - /** - * @var \Pterodactyl\Services\Services\Variables\VariableCreationService + * @var \Pterodactyl\Services\Eggs\Variables\VariableCreationService */ protected $service; @@ -41,10 +35,9 @@ class VariableCreationServiceTest extends TestCase { parent::setUp(); - $this->serviceOptionRepository = m::mock(EggRepositoryInterface::class); - $this->serviceVariableRepository = m::mock(ServiceVariableRepositoryInterface::class); + $this->repository = m::mock(EggVariableRepositoryInterface::class); - $this->service = new VariableCreationService($this->serviceOptionRepository, $this->serviceVariableRepository); + $this->service = new VariableCreationService($this->repository); } /** @@ -53,11 +46,11 @@ class VariableCreationServiceTest extends TestCase public function testVariableIsCreatedAndStored() { $data = ['env_variable' => 'TEST_VAR_123']; - $this->serviceVariableRepository->shouldReceive('create')->with([ - 'option_id' => 1, - 'user_viewable' => false, - 'user_editable' => false, - 'env_variable' => 'TEST_VAR_123', + $this->repository->shouldReceive('create')->with([ + 'egg_id' => 1, + 'user_viewable' => false, + 'user_editable' => false, + 'env_variable' => 'TEST_VAR_123', ])->once()->andReturn(new EggVariable); $this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data)); @@ -69,8 +62,8 @@ class VariableCreationServiceTest extends TestCase public function testOptionsPassedInArrayKeyAreParsedProperly() { $data = ['env_variable' => 'TEST_VAR_123', 'options' => ['user_viewable', 'user_editable']]; - $this->serviceVariableRepository->shouldReceive('create')->with([ - 'option_id' => 1, + $this->repository->shouldReceive('create')->with([ + 'egg_id' => 1, 'user_viewable' => true, 'user_editable' => true, 'env_variable' => 'TEST_VAR_123', @@ -84,29 +77,28 @@ class VariableCreationServiceTest extends TestCase * Test that all of the reserved variables defined in the model trigger an exception. * * @dataProvider reservedNamesProvider - * @expectedException \Pterodactyl\Exceptions\Service\ServiceVariable\ReservedVariableNameException + * @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException */ - public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames($variable) + public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames(string $variable) { $this->service->handle(1, ['env_variable' => $variable]); } /** - * Test that a model can be passed in place of an integer. + * Test that the egg ID applied in the function takes higher priority than an + * ID passed into the handler. */ - public function testModelCanBePassedInPlaceOfInteger() + public function testEggIdPassedInDataIsNotApplied() { - $model = factory(Egg::class)->make(); - $data = ['env_variable' => 'TEST_VAR_123']; - - $this->serviceVariableRepository->shouldReceive('create')->with([ - 'option_id' => $model->id, + $data = ['egg_id' => 123456, 'env_variable' => 'TEST_VAR_123']; + $this->repository->shouldReceive('create')->with([ + 'egg_id' => 1, 'user_viewable' => false, 'user_editable' => false, 'env_variable' => 'TEST_VAR_123', ])->once()->andReturn(new EggVariable); - $this->assertInstanceOf(EggVariable::class, $this->service->handle($model, $data)); + $this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data)); } /** diff --git a/tests/Unit/Services/Services/Variables/VariableUpdateServiceTest.php b/tests/Unit/Services/Eggs/Variables/VariableUpdateServiceTest.php similarity index 73% rename from tests/Unit/Services/Services/Variables/VariableUpdateServiceTest.php rename to tests/Unit/Services/Eggs/Variables/VariableUpdateServiceTest.php index 58e9fdc8a..48703f8e4 100644 --- a/tests/Unit/Services/Services/Variables/VariableUpdateServiceTest.php +++ b/tests/Unit/Services/Eggs/Variables/VariableUpdateServiceTest.php @@ -7,15 +7,15 @@ * https://opensource.org/licenses/MIT */ -namespace Tests\Unit\Services\Services\Variables; +namespace Tests\Unit\Services\Eggs\Variables; use Exception; use Mockery as m; use Tests\TestCase; use Pterodactyl\Models\EggVariable; use Pterodactyl\Exceptions\DisplayException; -use Pterodactyl\Services\Services\Variables\VariableUpdateService; -use Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface; +use Pterodactyl\Services\Eggs\Variables\VariableUpdateService; +use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface; class VariableUpdateServiceTest extends TestCase { @@ -25,12 +25,12 @@ class VariableUpdateServiceTest extends TestCase protected $model; /** - * @var \Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface|\Mockery\Mock + * @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock */ protected $repository; /** - * @var \Pterodactyl\Services\Services\Variables\VariableUpdateService + * @var \Pterodactyl\Services\Eggs\Variables\VariableUpdateService */ protected $service; @@ -42,7 +42,7 @@ class VariableUpdateServiceTest extends TestCase parent::setUp(); $this->model = factory(EggVariable::class)->make(); - $this->repository = m::mock(ServiceVariableRepositoryInterface::class); + $this->repository = m::mock(EggVariableRepositoryInterface::class); $this->service = new VariableUpdateService($this->repository); } @@ -86,7 +86,7 @@ class VariableUpdateServiceTest extends TestCase $this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf() ->shouldReceive('findCountWhere')->with([ ['env_variable', '=', 'TEST_VAR_123'], - ['option_id', '=', $this->model->option_id], + ['egg_id', '=', $this->model->option_id], ['id', '!=', $this->model->id], ])->once()->andReturn(0); @@ -100,6 +100,28 @@ class VariableUpdateServiceTest extends TestCase $this->assertTrue($this->service->handle($this->model, ['env_variable' => 'TEST_VAR_123'])); } + /** + * Test that data passed into the handler is overwritten inside the handler. + */ + public function testDataPassedIntoHandlerTakesLowerPriorityThanDataSet() + { + $this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf() + ->shouldReceive('findCountWhere')->with([ + ['env_variable', '=', 'TEST_VAR_123'], + ['egg_id', '=', $this->model->option_id], + ['id', '!=', $this->model->id], + ])->once()->andReturn(0); + + $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() + ->shouldReceive('update')->with($this->model->id, [ + 'user_viewable' => false, + 'user_editable' => false, + 'env_variable' => 'TEST_VAR_123', + ])->once()->andReturn(true); + + $this->assertTrue($this->service->handle($this->model, ['user_viewable' => 123456, 'env_variable' => 'TEST_VAR_123'])); + } + /** * Test that a non-unique environment variable triggers an exception. */ @@ -108,7 +130,7 @@ class VariableUpdateServiceTest extends TestCase $this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf() ->shouldReceive('findCountWhere')->with([ ['env_variable', '=', 'TEST_VAR_123'], - ['option_id', '=', $this->model->option_id], + ['egg_id', '=', $this->model->option_id], ['id', '!=', $this->model->id], ])->once()->andReturn(1); @@ -126,9 +148,9 @@ class VariableUpdateServiceTest extends TestCase * Test that all of the reserved variables defined in the model trigger an exception. * * @dataProvider reservedNamesProvider - * @expectedException \Pterodactyl\Exceptions\Service\ServiceVariable\ReservedVariableNameException + * @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException */ - public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames($variable) + public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames(string $variable) { $this->service->handle($this->model, ['env_variable' => $variable]); } diff --git a/tests/Unit/Services/Services/ServiceCreationServiceTest.php b/tests/Unit/Services/Nests/NestCreationServiceTest.php similarity index 68% rename from tests/Unit/Services/Services/ServiceCreationServiceTest.php rename to tests/Unit/Services/Nests/NestCreationServiceTest.php index 0d9ae6b9c..67467e87f 100644 --- a/tests/Unit/Services/Services/ServiceCreationServiceTest.php +++ b/tests/Unit/Services/Nests/NestCreationServiceTest.php @@ -12,15 +12,15 @@ namespace Tests\Unit\Services\Services; use Mockery as m; use Tests\TestCase; use Ramsey\Uuid\Uuid; +use Tests\Traits\KnownUuid; use Pterodactyl\Models\Nest; use Illuminate\Contracts\Config\Repository; -use Pterodactyl\Traits\Services\CreatesServiceIndex; -use Pterodactyl\Services\Services\NestCreationService; +use Pterodactyl\Services\Nests\NestCreationService; use Pterodactyl\Contracts\Repository\NestRepositoryInterface; -class ServiceCreationServiceTest extends TestCase +class NestCreationServiceTest extends TestCase { - use CreatesServiceIndex; + use KnownUuid; /** * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock @@ -33,15 +33,10 @@ class ServiceCreationServiceTest extends TestCase protected $repository; /** - * @var \Pterodactyl\Services\Services\NestCreationService + * @var \Pterodactyl\Services\Nests\NestCreationService */ protected $service; - /** - * @var \Ramsey\Uuid\Uuid|\Mockery\Mock - */ - protected $uuid; - /** * Setup tests. */ @@ -51,7 +46,6 @@ class ServiceCreationServiceTest extends TestCase $this->config = m::mock(Repository::class); $this->repository = m::mock(NestRepositoryInterface::class); - $this->uuid = m::mock('overload:' . Uuid::class); $this->service = new NestCreationService($this->config, $this->repository); } @@ -65,19 +59,14 @@ class ServiceCreationServiceTest extends TestCase $data = [ 'name' => $model->name, 'description' => $model->description, - 'folder' => $model->folder, - 'startup' => $model->startup, ]; - $this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-0000'); - $this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('0000-author'); + $this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('testauthor@example.com'); $this->repository->shouldReceive('create')->with([ - 'uuid' => 'uuid-0000', - 'author' => '0000-author', + 'uuid' => $this->getKnownUuid(), + 'author' => 'testauthor@example.com', 'name' => $data['name'], 'description' => $data['description'], - 'startup' => $data['startup'], - 'index_file' => $this->getIndexScript(), ], true, true)->once()->andReturn($model); $response = $this->service->handle($data); diff --git a/tests/Unit/Services/Services/ServiceDeletionServiceTest.php b/tests/Unit/Services/Nests/NestDeletionServiceTest.php similarity index 81% rename from tests/Unit/Services/Services/ServiceDeletionServiceTest.php rename to tests/Unit/Services/Nests/NestDeletionServiceTest.php index 396690e0e..b0bc0b2bb 100644 --- a/tests/Unit/Services/Services/ServiceDeletionServiceTest.php +++ b/tests/Unit/Services/Nests/NestDeletionServiceTest.php @@ -12,25 +12,26 @@ namespace Tests\Unit\Services\Services; use Exception; use Mockery as m; use Tests\TestCase; -use Pterodactyl\Services\Services\NestDeletionService; +use Pterodactyl\Exceptions\PterodactylException; +use Pterodactyl\Services\Nests\NestDeletionService; use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Exceptions\Service\HasActiveServersException; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; -class ServiceDeletionServiceTest extends TestCase +class NestDeletionServiceTest extends TestCase { /** - * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock */ protected $serverRepository; /** - * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock */ protected $repository; /** - * @var \Pterodactyl\Services\Services\NestDeletionService + * @var \Pterodactyl\Services\Nests\NestDeletionService */ protected $service; @@ -52,7 +53,7 @@ class ServiceDeletionServiceTest extends TestCase */ public function testServiceIsDeleted() { - $this->serverRepository->shouldReceive('findCountWhere')->with([['service_id', '=', 1]])->once()->andReturn(0); + $this->serverRepository->shouldReceive('findCountWhere')->with([['nest_id', '=', 1]])->once()->andReturn(0); $this->repository->shouldReceive('delete')->with(1)->once()->andReturn(1); $this->assertEquals(1, $this->service->handle(1)); @@ -62,14 +63,16 @@ class ServiceDeletionServiceTest extends TestCase * Test that an exception is thrown when there are servers attached to a service. * * @dataProvider serverCountProvider + * + * @param int $count */ - public function testExceptionIsThrownIfServersAreAttached($count) + public function testExceptionIsThrownIfServersAreAttached(int $count) { - $this->serverRepository->shouldReceive('findCountWhere')->with([['service_id', '=', 1]])->once()->andReturn($count); + $this->serverRepository->shouldReceive('findCountWhere')->with([['nest_id', '=', 1]])->once()->andReturn($count); try { $this->service->handle(1); - } catch (Exception $exception) { + } catch (PterodactylException $exception) { $this->assertInstanceOf(HasActiveServersException::class, $exception); $this->assertEquals(trans('exceptions.service.delete_has_servers'), $exception->getMessage()); } diff --git a/tests/Unit/Services/Services/ServiceUpdateServiceTest.php b/tests/Unit/Services/Nests/NestUpdateServiceTest.php similarity index 90% rename from tests/Unit/Services/Services/ServiceUpdateServiceTest.php rename to tests/Unit/Services/Nests/NestUpdateServiceTest.php index 612d684b2..e5b03e974 100644 --- a/tests/Unit/Services/Services/ServiceUpdateServiceTest.php +++ b/tests/Unit/Services/Nests/NestUpdateServiceTest.php @@ -11,18 +11,18 @@ namespace Tests\Unit\Services\Services; use Mockery as m; use Tests\TestCase; -use Pterodactyl\Services\Services\NestUpdateService; +use Pterodactyl\Services\Nests\NestUpdateService; use Pterodactyl\Contracts\Repository\NestRepositoryInterface; -class ServiceUpdateServiceTest extends TestCase +class NestUpdateServiceTest extends TestCase { /** - * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock */ protected $repository; /** - * @var \Pterodactyl\Services\Services\NestUpdateService + * @var \Pterodactyl\Services\Nests\NestUpdateService */ protected $service;