From 6e02e9491a478b95a2ac9544a109b797439f7149 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 8 Oct 2017 15:29:46 -0500 Subject: [PATCH] Egg tests updated --- app/Services/Eggs/EggCreationService.php | 2 +- .../Traits/{KnownUuid.php => MocksUuids.php} | 2 +- .../EggCreationServiceTest.php} | 112 ++++++++---------- .../EggDeletionServiceTest.php} | 32 ++--- .../EggUpdateServiceTest.php} | 45 ++++--- .../Eggs/Sharing/EggImporterServiceTest.php | 4 +- .../Nests/NestCreationServiceTest.php | 4 +- 7 files changed, 99 insertions(+), 102 deletions(-) rename tests/Traits/{KnownUuid.php => MocksUuids.php} (98%) rename tests/Unit/Services/{Services/Options/OptionCreationServiceTest.php => Eggs/EggCreationServiceTest.php} (62%) rename tests/Unit/Services/{Services/Options/OptionDeletionServiceTest.php => Eggs/EggDeletionServiceTest.php} (68%) rename tests/Unit/Services/{Services/Options/OptionUpdateServiceTest.php => Eggs/EggUpdateServiceTest.php} (60%) diff --git a/app/Services/Eggs/EggCreationService.php b/app/Services/Eggs/EggCreationService.php index 13f04c3b0..aaf9823f1 100644 --- a/app/Services/Eggs/EggCreationService.php +++ b/app/Services/Eggs/EggCreationService.php @@ -59,7 +59,7 @@ class EggCreationService ]); if ($results !== 1) { - throw new NoParentConfigurationFoundException(trans('exceptions.service.options.must_be_child')); + throw new NoParentConfigurationFoundException(trans('exceptions.nest.egg.must_be_child')); } } diff --git a/tests/Traits/KnownUuid.php b/tests/Traits/MocksUuids.php similarity index 98% rename from tests/Traits/KnownUuid.php rename to tests/Traits/MocksUuids.php index 03d198380..af57f93a8 100644 --- a/tests/Traits/KnownUuid.php +++ b/tests/Traits/MocksUuids.php @@ -13,7 +13,7 @@ use Mockery as m; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidFactory; -trait KnownUuid +trait MocksUuids { /** * The known UUID string. diff --git a/tests/Unit/Services/Services/Options/OptionCreationServiceTest.php b/tests/Unit/Services/Eggs/EggCreationServiceTest.php similarity index 62% rename from tests/Unit/Services/Services/Options/OptionCreationServiceTest.php rename to tests/Unit/Services/Eggs/EggCreationServiceTest.php index 3e7ddade5..7afa07871 100644 --- a/tests/Unit/Services/Services/Options/OptionCreationServiceTest.php +++ b/tests/Unit/Services/Eggs/EggCreationServiceTest.php @@ -12,15 +12,18 @@ namespace Tests\Unit\Services\Services\Options; use Exception; use Mockery as m; use Tests\TestCase; -use Ramsey\Uuid\Uuid; use Pterodactyl\Models\Egg; +use Tests\Traits\MocksUuids; use Illuminate\Contracts\Config\Repository; +use Pterodactyl\Exceptions\PterodactylException; +use Pterodactyl\Services\Eggs\EggCreationService; use Pterodactyl\Contracts\Repository\EggRepositoryInterface; -use Pterodactyl\Services\Services\Options\EggCreationService; -use Pterodactyl\Exceptions\Service\ServiceOption\NoParentConfigurationFoundException; +use Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException; -class OptionCreationServiceTest extends TestCase +class EggCreationServiceTest extends TestCase { + use MocksUuids; + /** * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock */ @@ -32,15 +35,10 @@ class OptionCreationServiceTest extends TestCase protected $repository; /** - * @var \Pterodactyl\Services\Services\Options\EggCreationService + * @var \Pterodactyl\Services\Eggs\EggCreationService */ protected $service; - /** - * @var \Ramsey\Uuid\Uuid|\Mockery\Mock - */ - protected $uuid; - /** * Setup tests. */ @@ -50,7 +48,6 @@ class OptionCreationServiceTest extends TestCase $this->config = m::mock(Repository::class); $this->repository = m::mock(EggRepositoryInterface::class); - $this->uuid = m::mock('overload:' . Uuid::class); $this->service = new EggCreationService($this->config, $this->repository); } @@ -60,52 +57,23 @@ class OptionCreationServiceTest extends TestCase */ public function testCreateNewModelWithoutUsingConfigFrom() { - $model = factory(Egg::class)->make([ - 'tag' => str_random(10), - ]); + $model = factory(Egg::class)->make(); $this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com'); - $this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-string'); $this->repository->shouldReceive('create')->with([ - 'name' => $model->name, - 'tag' => 'test@example.com:' . $model->tag, + 'uuid' => $this->getKnownUuid(), + 'author' => 'test@example.com', 'config_from' => null, - 'uuid' => 'uuid-string', + 'name' => $model->name, ], true, true)->once()->andReturn($model); - $response = $this->service->handle(['name' => $model->name, 'tag' => $model->tag]); + $response = $this->service->handle(['name' => $model->name]); $this->assertNotEmpty($response); $this->assertNull(object_get($response, 'config_from')); $this->assertEquals($model->name, $response->name); } - /** - * Test that passing a bad tag into the function will set the correct tag. - */ - public function testCreateNewModelUsingLongTagForm() - { - $model = factory(Egg::class)->make([ - 'tag' => 'test@example.com:tag', - ]); - - $this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com'); - $this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-string'); - $this->repository->shouldReceive('create')->with([ - 'name' => $model->name, - 'config_from' => null, - 'tag' => $model->tag, - 'uuid' => 'uuid-string', - ], true, true)->once()->andReturn($model); - - $response = $this->service->handle(['name' => $model->name, 'tag' => 'bad@example.com:tag']); - - $this->assertNotEmpty($response); - $this->assertNull(object_get($response, 'config_from')); - $this->assertEquals($model->name, $response->name); - $this->assertEquals('test@example.com:tag', $response->tag); - } - /** * Test that a new model is created when using the config from attribute. */ @@ -113,44 +81,66 @@ class OptionCreationServiceTest extends TestCase { $model = factory(Egg::class)->make(); - $data = [ - 'name' => $model->name, - 'service_id' => $model->service_id, - 'tag' => 'test@example.com:tag', - 'config_from' => 1, - 'uuid' => 'uuid-string', - ]; - $this->repository->shouldReceive('findCountWhere')->with([ - ['service_id', '=', $data['service_id']], - ['id', '=', $data['config_from']], + ['nest_id', '=', $model->nest_id], + ['id', '=', 12345], ])->once()->andReturn(1); $this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com'); - $this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-string'); - $this->repository->shouldReceive('create')->with($data, true, true)->once()->andReturn($model); + $this->repository->shouldReceive('create')->with([ + 'nest_id' => $model->nest_id, + 'config_from' => 12345, + 'uuid' => $this->getKnownUuid(), + 'author' => 'test@example.com', + ], true, true)->once()->andReturn($model); - $response = $this->service->handle($data); + $response = $this->service->handle([ + 'nest_id' => $model->nest_id, + 'config_from' => 12345, + ]); $this->assertNotEmpty($response); $this->assertEquals($response, $model); } + /** + * Test that certain data, such as the UUID or author takes priority over data + * that is passed into the function. + */ + public function testDataProvidedByHandlerTakesPriorityOverPassedData() + { + $model = factory(Egg::class)->make(); + + $this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com'); + $this->repository->shouldReceive('create')->with([ + 'uuid' => $this->getKnownUuid(), + 'author' => 'test@example.com', + 'config_from' => null, + 'name' => $model->name, + ], true, true)->once()->andReturn($model); + + $response = $this->service->handle(['name' => $model->name, 'uuid' => 'should-be-ignored', 'author' => 'should-be-ignored']); + + $this->assertNotEmpty($response); + $this->assertNull(object_get($response, 'config_from')); + $this->assertEquals($model->name, $response->name); + } + /** * Test that an exception is thrown if no parent configuration can be located. */ public function testExceptionIsThrownIfNoParentConfigurationIsFound() { $this->repository->shouldReceive('findCountWhere')->with([ - ['service_id', '=', null], + ['nest_id', '=', null], ['id', '=', 1], ])->once()->andReturn(0); try { $this->service->handle(['config_from' => 1]); - } catch (Exception $exception) { + } catch (PterodactylException $exception) { $this->assertInstanceOf(NoParentConfigurationFoundException::class, $exception); - $this->assertEquals(trans('exceptions.service.options.must_be_child'), $exception->getMessage()); + $this->assertEquals(trans('exceptions.nest.egg.must_be_child'), $exception->getMessage()); } } } diff --git a/tests/Unit/Services/Services/Options/OptionDeletionServiceTest.php b/tests/Unit/Services/Eggs/EggDeletionServiceTest.php similarity index 68% rename from tests/Unit/Services/Services/Options/OptionDeletionServiceTest.php rename to tests/Unit/Services/Eggs/EggDeletionServiceTest.php index 875b3a547..c3ef3ef1b 100644 --- a/tests/Unit/Services/Services/Options/OptionDeletionServiceTest.php +++ b/tests/Unit/Services/Eggs/EggDeletionServiceTest.php @@ -11,14 +11,14 @@ namespace Tests\Unit\Services\Services\Options; use Mockery as m; use Tests\TestCase; -use Pterodactyl\Exceptions\DisplayException; +use Pterodactyl\Exceptions\PterodactylException; +use Pterodactyl\Services\Eggs\EggDeletionService; use Pterodactyl\Contracts\Repository\EggRepositoryInterface; +use Pterodactyl\Exceptions\Service\Egg\HasChildrenException; use Pterodactyl\Exceptions\Service\HasActiveServersException; -use Pterodactyl\Services\Services\Options\EggDeletionService; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; -use Pterodactyl\Exceptions\Service\ServiceOption\HasChildrenException; -class OptionDeletionServiceTest extends TestCase +class EggDeletionServiceTest extends TestCase { /** * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock @@ -31,7 +31,7 @@ class OptionDeletionServiceTest extends TestCase protected $serverRepository; /** - * @var \Pterodactyl\Services\Services\Options\EggDeletionService + * @var \Pterodactyl\Services\Eggs\EggDeletionService */ protected $service; @@ -49,11 +49,11 @@ class OptionDeletionServiceTest extends TestCase } /** - * Test that option is deleted if no servers are found. + * Test that Egg is deleted if no servers are found. */ - public function testOptionIsDeletedIfNoServersAreFound() + public function testEggIsDeletedIfNoServersAreFound() { - $this->serverRepository->shouldReceive('findCountWhere')->with([['option_id', '=', 1]])->once()->andReturn(0); + $this->serverRepository->shouldReceive('findCountWhere')->with([['egg_id', '=', 1]])->once()->andReturn(0); $this->repository->shouldReceive('findCountWhere')->with([['config_from', '=', 1]])->once()->andReturn(0); $this->repository->shouldReceive('delete')->with(1)->once()->andReturn(1); @@ -61,33 +61,33 @@ class OptionDeletionServiceTest extends TestCase } /** - * Test that option is not deleted if servers are found. + * Test that Egg is not deleted if servers are found. */ public function testExceptionIsThrownIfServersAreFound() { - $this->serverRepository->shouldReceive('findCountWhere')->with([['option_id', '=', 1]])->once()->andReturn(1); + $this->serverRepository->shouldReceive('findCountWhere')->with([['egg_id', '=', 1]])->once()->andReturn(1); try { $this->service->handle(1); - } catch (DisplayException $exception) { + } catch (PterodactylException $exception) { $this->assertInstanceOf(HasActiveServersException::class, $exception); - $this->assertEquals(trans('exceptions.service.options.delete_has_servers'), $exception->getMessage()); + $this->assertEquals(trans('exceptions.nest.egg.delete_has_servers'), $exception->getMessage()); } } /** - * Test that an exception is thrown if children options exist. + * Test that an exception is thrown if children Eggs exist. */ public function testExceptionIsThrownIfChildrenArePresent() { - $this->serverRepository->shouldReceive('findCountWhere')->with([['option_id', '=', 1]])->once()->andReturn(0); + $this->serverRepository->shouldReceive('findCountWhere')->with([['egg_id', '=', 1]])->once()->andReturn(0); $this->repository->shouldReceive('findCountWhere')->with([['config_from', '=', 1]])->once()->andReturn(1); try { $this->service->handle(1); - } catch (DisplayException $exception) { + } catch (PterodactylException $exception) { $this->assertInstanceOf(HasChildrenException::class, $exception); - $this->assertEquals(trans('exceptions.service.options.has_children'), $exception->getMessage()); + $this->assertEquals(trans('exceptions.nest.egg.has_children'), $exception->getMessage()); } } } diff --git a/tests/Unit/Services/Services/Options/OptionUpdateServiceTest.php b/tests/Unit/Services/Eggs/EggUpdateServiceTest.php similarity index 60% rename from tests/Unit/Services/Services/Options/OptionUpdateServiceTest.php rename to tests/Unit/Services/Eggs/EggUpdateServiceTest.php index 0da64ca58..89f02c49c 100644 --- a/tests/Unit/Services/Services/Options/OptionUpdateServiceTest.php +++ b/tests/Unit/Services/Eggs/EggUpdateServiceTest.php @@ -13,11 +13,12 @@ use Exception; use Mockery as m; use Tests\TestCase; use Pterodactyl\Models\Egg; -use Pterodactyl\Services\Services\Options\EggUpdateService; +use Pterodactyl\Services\Eggs\EggUpdateService; +use Pterodactyl\Exceptions\PterodactylException; use Pterodactyl\Contracts\Repository\EggRepositoryInterface; -use Pterodactyl\Exceptions\Service\ServiceOption\NoParentConfigurationFoundException; +use Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException; -class OptionUpdateServiceTest extends TestCase +class EggUpdateServiceTest extends TestCase { /** * @var \Pterodactyl\Models\Egg @@ -25,12 +26,12 @@ class OptionUpdateServiceTest extends TestCase protected $model; /** - * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock */ protected $repository; /** - * @var \Pterodactyl\Services\Services\Options\EggUpdateService + * @var \Pterodactyl\Services\Eggs\EggUpdateService */ protected $service; @@ -48,30 +49,34 @@ class OptionUpdateServiceTest extends TestCase } /** - * Test that an option is updated when no config_from attribute is passed. + * Test that an Egg is updated when no config_from attribute is passed. */ - public function testOptionIsUpdatedWhenNoConfigFromIsProvided() + public function testEggIsUpdatedWhenNoConfigFromIsProvided() { - $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('update')->with($this->model->id, ['test_field' => 'field_value'])->once()->andReturnNull(); + $this->repository->shouldReceive('withoutFresh->update') + ->with($this->model->id, ['test_field' => 'field_value'])->once()->andReturnNull(); $this->service->handle($this->model, ['test_field' => 'field_value']); + + $this->assertTrue(true); } /** - * Test that option is updated when a valid config_from attribute is passed. + * Test that Egg is updated when a valid config_from attribute is passed. */ public function testOptionIsUpdatedWhenValidConfigFromIsPassed() { $this->repository->shouldReceive('findCountWhere')->with([ - ['service_id', '=', $this->model->service_id], + ['nest_id', '=', $this->model->nest_id], ['id', '=', 1], ])->once()->andReturn(1); - $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('update')->with($this->model->id, ['config_from' => 1])->once()->andReturnNull(); + $this->repository->shouldReceive('withoutFresh->update') + ->with($this->model->id, ['config_from' => 1])->once()->andReturnNull(); $this->service->handle($this->model, ['config_from' => 1]); + + $this->assertTrue(true); } /** @@ -80,27 +85,29 @@ class OptionUpdateServiceTest extends TestCase public function testExceptionIsThrownIfInvalidParentConfigIsPassed() { $this->repository->shouldReceive('findCountWhere')->with([ - ['service_id', '=', $this->model->service_id], + ['nest_id', '=', $this->model->nest_id], ['id', '=', 1], ])->once()->andReturn(0); try { $this->service->handle($this->model, ['config_from' => 1]); - } catch (Exception $exception) { + } catch (PterodactylException $exception) { $this->assertInstanceOf(NoParentConfigurationFoundException::class, $exception); - $this->assertEquals(trans('exceptions.service.options.must_be_child'), $exception->getMessage()); + $this->assertEquals(trans('exceptions.nest.egg.must_be_child'), $exception->getMessage()); } } /** - * Test that an integer linking to a model can be passed in place of the ServiceOption model. + * Test that an integer linking to a model can be passed in place of the Egg model. */ public function testIntegerCanBePassedInPlaceOfModel() { $this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model); - $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('update')->with($this->model->id, ['test_field' => 'field_value'])->once()->andReturnNull(); + $this->repository->shouldReceive('withoutFresh->update') + ->with($this->model->id, ['test_field' => 'field_value'])->once()->andReturnNull(); $this->service->handle($this->model->id, ['test_field' => 'field_value']); + + $this->assertTrue(true); } } diff --git a/tests/Unit/Services/Eggs/Sharing/EggImporterServiceTest.php b/tests/Unit/Services/Eggs/Sharing/EggImporterServiceTest.php index 7e8c37601..870482e68 100644 --- a/tests/Unit/Services/Eggs/Sharing/EggImporterServiceTest.php +++ b/tests/Unit/Services/Eggs/Sharing/EggImporterServiceTest.php @@ -12,8 +12,8 @@ namespace Tests\Unit\Services\Services\Sharing; use Mockery as m; use Tests\TestCase; use Pterodactyl\Models\Egg; -use Tests\Traits\KnownUuid; use Pterodactyl\Models\Nest; +use Tests\Traits\MocksUuids; use Illuminate\Http\UploadedFile; use Pterodactyl\Models\EggVariable; use Illuminate\Database\ConnectionInterface; @@ -26,7 +26,7 @@ use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface; class EggImporterServiceTest extends TestCase { - use KnownUuid; + use MocksUuids; /** * @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock diff --git a/tests/Unit/Services/Nests/NestCreationServiceTest.php b/tests/Unit/Services/Nests/NestCreationServiceTest.php index 67467e87f..b5d38e071 100644 --- a/tests/Unit/Services/Nests/NestCreationServiceTest.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 Tests\Traits\MocksUuids; use Illuminate\Contracts\Config\Repository; use Pterodactyl\Services\Nests\NestCreationService; use Pterodactyl\Contracts\Repository\NestRepositoryInterface; class NestCreationServiceTest extends TestCase { - use KnownUuid; + use MocksUuids; /** * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock