Fix unit tests for eggs

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

View file

@ -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);

View file

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

View file

@ -1,13 +1,6 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Services\Options;
namespace Tests\Unit\Services\Eggs\Scripts;
use Exception;
use 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);
}
}

View file

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

View file

@ -1,13 +1,6 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Services\Sharing;
namespace Tests\Unit\Services\Eggs\Sharing;
use Mockery as m;
use 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);
}
}

View file

@ -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', [