From 159ad3079f8b0a7bfc77d163a248d947534e4834 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 8 Oct 2017 15:44:28 -0500 Subject: [PATCH] Fix existing tests --- .../Packs/PackCreationServiceTest.php | 29 +++++++------------ .../Services/Packs/PackUpdateServiceTest.php | 16 +++++----- .../Packs/TemplateUploadServiceTest.php | 13 ++++----- .../Servers/ServerCreationServiceTest.php | 25 ++++++++-------- .../Servers/VariableValidatorServiceTest.php | 8 ++--- 5 files changed, 40 insertions(+), 51 deletions(-) diff --git a/tests/Unit/Services/Packs/PackCreationServiceTest.php b/tests/Unit/Services/Packs/PackCreationServiceTest.php index 518fa9090..972ee7a45 100644 --- a/tests/Unit/Services/Packs/PackCreationServiceTest.php +++ b/tests/Unit/Services/Packs/PackCreationServiceTest.php @@ -13,6 +13,7 @@ use Exception; use Mockery as m; use Tests\TestCase; use Pterodactyl\Models\Pack; +use Tests\Traits\MocksUuids; use Illuminate\Http\UploadedFile; use Illuminate\Contracts\Filesystem\Factory; use Illuminate\Database\ConnectionInterface; @@ -23,18 +24,20 @@ use Pterodactyl\Exceptions\Service\Pack\InvalidFileMimeTypeException; class PackCreationServiceTest extends TestCase { + use MocksUuids; + /** - * @var \Illuminate\Database\ConnectionInterface + * @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock */ protected $connection; /** - * @var \Illuminate\Http\UploadedFile + * @var \Illuminate\Http\UploadedFile|\Mockery\Mock */ protected $file; /** - * @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface|\Mockery\Mock */ protected $repository; @@ -44,15 +47,10 @@ class PackCreationServiceTest extends TestCase protected $service; /** - * @var \Illuminate\Contracts\Filesystem\Factory + * @var \Illuminate\Contracts\Filesystem\Factory|\Mockery\Mock */ protected $storage; - /** - * @var \Ramsey\Uuid\Uuid - */ - protected $uuid; - /** * Setup tests. */ @@ -64,7 +62,6 @@ class PackCreationServiceTest extends TestCase $this->file = m::mock(UploadedFile::class); $this->repository = m::mock(PackRepositoryInterface::class); $this->storage = m::mock(Factory::class); - $this->uuid = m::mock('overload:\Ramsey\Uuid\Uuid'); $this->service = new PackCreationService($this->connection, $this->storage, $this->repository); } @@ -77,17 +74,15 @@ class PackCreationServiceTest extends TestCase $model = factory(Pack::class)->make(); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); - $this->uuid->shouldReceive('uuid4')->withNoArgs()->once()->andReturn($model->uuid); $this->repository->shouldReceive('create')->with([ - 'uuid' => $model->uuid, + 'uuid' => $this->getKnownUuid(), 'selectable' => false, 'visible' => false, 'locked' => false, 'test-data' => 'value', ])->once()->andReturn($model); - $this->storage->shouldReceive('disk')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('makeDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull(); + $this->storage->shouldReceive('disk->makeDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $response = $this->service->handle(['test-data' => 'value']); @@ -107,17 +102,15 @@ class PackCreationServiceTest extends TestCase $this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true); $this->file->shouldReceive('getMimeType')->withNoArgs()->once()->andReturn($mime); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); - $this->uuid->shouldReceive('uuid4')->withNoArgs()->once()->andReturn($model->uuid); $this->repository->shouldReceive('create')->with([ - 'uuid' => $model->uuid, + 'uuid' => $this->getKnownUuid(), 'selectable' => false, 'visible' => false, 'locked' => false, 'test-data' => 'value', ])->once()->andReturn($model); - $this->storage->shouldReceive('disk')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('makeDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull(); + $this->storage->shouldReceive('disk->makeDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull(); $this->file->shouldReceive('storeAs')->with('packs/' . $model->uuid, 'archive.tar.gz')->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); diff --git a/tests/Unit/Services/Packs/PackUpdateServiceTest.php b/tests/Unit/Services/Packs/PackUpdateServiceTest.php index 02dd45fd4..8a09311f1 100644 --- a/tests/Unit/Services/Packs/PackUpdateServiceTest.php +++ b/tests/Unit/Services/Packs/PackUpdateServiceTest.php @@ -20,12 +20,12 @@ use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; class PackUpdateServiceTest extends TestCase { /** - * @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface|\Mockery\Mock */ protected $repository; /** - * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock */ protected $serverRepository; @@ -53,8 +53,7 @@ class PackUpdateServiceTest extends TestCase public function testPackIsUpdated() { $model = factory(Pack::class)->make(); - $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('update')->with($model->id, [ + $this->repository->shouldReceive('withoutFresh->update')->with($model->id, [ 'locked' => false, 'visible' => false, 'selectable' => false, @@ -67,13 +66,13 @@ class PackUpdateServiceTest extends TestCase /** * Test that an exception is thrown if the pack option ID is changed while servers are using the pack. */ - public function testExceptionIsThrownIfModifyingOptionIdWhenServersAreAttached() + public function testExceptionIsThrownIfModifyingEggIdWhenServersAreAttached() { $model = factory(Pack::class)->make(); $this->serverRepository->shouldReceive('findCountWhere')->with([['pack_id', '=', $model->id]])->once()->andReturn(1); try { - $this->service->handle($model, ['option_id' => 0]); + $this->service->handle($model, ['egg_id' => 0]); } catch (HasActiveServersException $exception) { $this->assertEquals(trans('exceptions.packs.update_has_servers'), $exception->getMessage()); } @@ -86,10 +85,9 @@ class PackUpdateServiceTest extends TestCase { $model = factory(Pack::class)->make(); - $this->repository->shouldReceive('withColumns')->with(['id', 'option_id'])->once()->andReturnSelf() + $this->repository->shouldReceive('withColumns')->with(['id', 'egg_id'])->once()->andReturnSelf() ->shouldReceive('find')->with($model->id)->once()->andReturn($model); - $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('update')->with($model->id, [ + $this->repository->shouldReceive('withoutFresh->update')->with($model->id, [ 'locked' => false, 'visible' => false, 'selectable' => false, diff --git a/tests/Unit/Services/Packs/TemplateUploadServiceTest.php b/tests/Unit/Services/Packs/TemplateUploadServiceTest.php index 115241087..4082fdd59 100644 --- a/tests/Unit/Services/Packs/TemplateUploadServiceTest.php +++ b/tests/Unit/Services/Packs/TemplateUploadServiceTest.php @@ -27,17 +27,17 @@ class TemplateUploadServiceTest extends TestCase const JSON_FILE_CONTENTS = '{"test_content": "value"}'; /** - * @var \ZipArchive + * @var \ZipArchive|\Mockery\Mock */ protected $archive; /** - * @var \Pterodactyl\Services\Packs\PackCreationService + * @var \Pterodactyl\Services\Packs\PackCreationService|\Mockery\Mock */ protected $creationService; /** - * @var \Illuminate\Http\UploadedFile + * @var \Illuminate\Http\UploadedFile|\Mockery\Mock */ protected $file; @@ -70,10 +70,9 @@ class TemplateUploadServiceTest extends TestCase $this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true); $this->file->shouldReceive('getMimeType')->withNoArgs()->twice()->andReturn($mime); $this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(128); - $this->file->shouldReceive('openFile')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('fread')->with(128)->once()->andReturn(self::JSON_FILE_CONTENTS); + $this->file->shouldReceive('openFile->fread')->with(128)->once()->andReturn(self::JSON_FILE_CONTENTS); - $this->creationService->shouldReceive('handle')->with(['test_content' => 'value', 'option_id' => 1]) + $this->creationService->shouldReceive('handle')->with(['test_content' => 'value', 'egg_id' => 1]) ->once()->andReturn(factory(Pack::class)->make()); $this->assertInstanceOf(Pack::class, $this->service->handle(1, $this->file)); @@ -94,7 +93,7 @@ class TemplateUploadServiceTest extends TestCase $this->archive->shouldReceive('locateName')->with('import.json')->once()->andReturn(true); $this->archive->shouldReceive('locateName')->with('archive.tar.gz')->once()->andReturn(true); $this->archive->shouldReceive('getFromName')->with('import.json')->once()->andReturn(self::JSON_FILE_CONTENTS); - $this->creationService->shouldReceive('handle')->with(['test_content' => 'value', 'option_id' => 1]) + $this->creationService->shouldReceive('handle')->with(['test_content' => 'value', 'egg_id' => 1]) ->once()->andReturn($model); $this->archive->shouldReceive('extractTo')->with(storage_path('app/packs/' . $model->uuid), 'archive.tar.gz') ->once()->andReturn(true); diff --git a/tests/Unit/Services/Servers/ServerCreationServiceTest.php b/tests/Unit/Services/Servers/ServerCreationServiceTest.php index 0b22486e5..da2e33af2 100644 --- a/tests/Unit/Services/Servers/ServerCreationServiceTest.php +++ b/tests/Unit/Services/Servers/ServerCreationServiceTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Services\Servers; use Mockery as m; use Tests\TestCase; use phpmock\phpunit\PHPMock; +use Tests\Traits\MocksUuids; use GuzzleHttp\Exception\RequestException; use Illuminate\Database\ConnectionInterface; use Pterodactyl\Exceptions\PterodactylException; @@ -32,7 +33,7 @@ use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonS */ class ServerCreationServiceTest extends TestCase { - use PHPMock; + use MocksUuids, PHPMock; /** * @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock @@ -72,8 +73,8 @@ class ServerCreationServiceTest extends TestCase 'environment' => [ 'TEST_VAR_1' => 'var1-value', ], - 'service_id' => 1, - 'option_id' => 1, + 'nest_id' => 1, + 'egg_id' => 1, 'startup' => 'startup-param', 'docker_image' => 'some/image', ]; @@ -118,11 +119,6 @@ class ServerCreationServiceTest extends TestCase */ protected $validatorService; - /** - * @var \Ramsey\Uuid\Uuid|\Mockery\Mock - */ - protected $uuid; - /** * Setup tests. */ @@ -141,7 +137,6 @@ class ServerCreationServiceTest extends TestCase $this->userRepository = m::mock(UserRepositoryInterface::class); $this->usernameService = m::mock(UsernameGenerationService::class); $this->validatorService = m::mock(VariableValidatorService::class); - $this->uuid = m::mock('overload:Ramsey\Uuid\Uuid'); $this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'str_random') ->expects($this->any())->willReturn('random_string'); @@ -167,14 +162,19 @@ class ServerCreationServiceTest extends TestCase { $this->validatorService->shouldReceive('isAdmin')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('setFields')->with($this->data['environment'])->once()->andReturnSelf() - ->shouldReceive('validate')->with($this->data['option_id'])->once()->andReturnSelf(); + ->shouldReceive('validate')->with($this->data['egg_id'])->once()->andReturnSelf(); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); - $this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-0000'); $this->usernameService->shouldReceive('generate')->with($this->data['name'], 'random_string') ->once()->andReturn('user_name'); - $this->repository->shouldReceive('create')->withAnyArgs()->once()->andReturn((object) [ + $this->repository->shouldReceive('create')->with(m::subset([ + 'uuid' => $this->getKnownUuid(), + 'node_id' => $this->data['node_id'], + 'owner_id' => 1, + 'nest_id' => 1, + 'egg_id' => 1, + ]))->once()->andReturn((object) [ 'node_id' => 1, 'id' => 1, ]); @@ -212,7 +212,6 @@ class ServerCreationServiceTest extends TestCase $this->validatorService->shouldReceive('isAdmin->setFields->validate->getResults')->once()->andReturn([]); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->usernameService->shouldReceive('generate')->once()->andReturn('user_name'); - $this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-0000'); $this->repository->shouldReceive('create')->once()->andReturn((object) [ 'node_id' => 1, 'id' => 1, diff --git a/tests/Unit/Services/Servers/VariableValidatorServiceTest.php b/tests/Unit/Services/Servers/VariableValidatorServiceTest.php index 2e9c5fc60..ce2eaf7d8 100644 --- a/tests/Unit/Services/Servers/VariableValidatorServiceTest.php +++ b/tests/Unit/Services/Servers/VariableValidatorServiceTest.php @@ -115,7 +115,7 @@ class VariableValidatorServiceTest extends TestCase */ public function testEmptyResultSetShouldBeReturnedIfNoVariablesAreFound() { - $this->optionVariableRepository->shouldReceive('findWhere')->with([['option_id', '=', 1]])->andReturn([]); + $this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn([]); $response = $this->service->validate(1); @@ -129,7 +129,7 @@ class VariableValidatorServiceTest extends TestCase */ public function testValidatorShouldNotProcessVariablesSetAsNotUserEditableWhenAdminFlagIsNotPassed() { - $this->optionVariableRepository->shouldReceive('findWhere')->with([['option_id', '=', 1]])->andReturn($this->variables); + $this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($this->variables); $this->validator->shouldReceive('make')->with([ 'variable_value' => 'Test_SomeValue_0', @@ -161,7 +161,7 @@ class VariableValidatorServiceTest extends TestCase */ public function testValidatorShouldProcessAllVariablesWhenAdminFlagIsSet() { - $this->optionVariableRepository->shouldReceive('findWhere')->with([['option_id', '=', 1]])->andReturn($this->variables); + $this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($this->variables); foreach ($this->variables as $key => $variable) { $this->validator->shouldReceive('make')->with([ @@ -198,7 +198,7 @@ class VariableValidatorServiceTest extends TestCase */ public function testValidatorShouldThrowExceptionWhenAValidationErrorIsEncountered() { - $this->optionVariableRepository->shouldReceive('findWhere')->with([['option_id', '=', 1]])->andReturn($this->variables); + $this->optionVariableRepository->shouldReceive('findWhere')->with([['egg_id', '=', 1]])->andReturn($this->variables); $this->validator->shouldReceive('make')->with([ 'variable_value' => null,