Skip to content

Commit 7873465

Browse files
committed
Update a batch of failing tests
1 parent c19c423 commit 7873465

13 files changed

+216
-193
lines changed

app/Services/Eggs/Sharing/EggImporterService.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,33 @@ class EggImporterService
3131
protected $eggVariableRepository;
3232

3333
/**
34-
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
34+
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
3535
*/
36-
protected $repository;
36+
protected $nestRepository;
3737

3838
/**
39-
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
39+
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
4040
*/
41-
protected $serviceRepository;
41+
protected $repository;
4242

4343
/**
4444
* EggImporterService constructor.
4545
*
4646
* @param \Illuminate\Database\ConnectionInterface $connection
4747
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $repository
4848
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $eggVariableRepository
49-
* @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $serviceRepository
49+
* @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $nestRepository
5050
*/
5151
public function __construct(
5252
ConnectionInterface $connection,
5353
EggRepositoryInterface $repository,
5454
EggVariableRepositoryInterface $eggVariableRepository,
55-
NestRepositoryInterface $serviceRepository
55+
NestRepositoryInterface $nestRepository
5656
) {
5757
$this->connection = $connection;
58-
$this->repository = $repository;
59-
$this->serviceRepository = $serviceRepository;
6058
$this->eggVariableRepository = $eggVariableRepository;
59+
$this->repository = $repository;
60+
$this->nestRepository = $nestRepository;
6161
}
6262

6363
/**
@@ -74,16 +74,16 @@ public function __construct(
7474
public function handle(UploadedFile $file, int $nest): Egg
7575
{
7676
if (! $file->isValid() || ! $file->isFile()) {
77-
throw new InvalidFileUploadException(trans('exceptions.egg.importer.file_error'));
77+
throw new InvalidFileUploadException(trans('exceptions.nest.importer.file_error'));
7878
}
7979

8080
$parsed = json_decode($file->openFile()->fread($file->getSize()));
8181

8282
if (object_get($parsed, 'meta.version') !== 'PTDL_v1') {
83-
throw new InvalidFileUploadException(trans('exceptions.egg.importer.invalid_json_provided'));
83+
throw new InvalidFileUploadException(trans('exceptions.nest.importer.invalid_json_provided'));
8484
}
8585

86-
$nest = $this->serviceRepository->getWithEggs($nest);
86+
$nest = $this->nestRepository->getWithEggs($nest);
8787
$this->connection->beginTransaction();
8888

8989
$egg = $this->repository->create([

app/Services/Eggs/Variables/VariableCreationService.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,24 @@
1010
namespace Pterodactyl\Services\Eggs\Variables;
1111

1212
use Pterodactyl\Models\EggVariable;
13-
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
1413
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
1514
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
1615

1716
class VariableCreationService
1817
{
19-
/**
20-
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
21-
*/
22-
protected $eggRepository;
23-
2418
/**
2519
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
2620
*/
27-
protected $variableRepository;
21+
protected $repository;
2822

2923
/**
3024
* VariableCreationService constructor.
3125
*
32-
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $eggRepository
33-
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $variableRepository
26+
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $repository
3427
*/
35-
public function __construct(
36-
EggRepositoryInterface $eggRepository,
37-
EggVariableRepositoryInterface $variableRepository
38-
) {
39-
$this->eggRepository = $eggRepository;
40-
$this->variableRepository = $variableRepository;
28+
public function __construct(EggVariableRepositoryInterface $repository)
29+
{
30+
$this->repository = $repository;
4131
}
4232

4333
/**
@@ -61,10 +51,10 @@ public function handle(int $egg, array $data): EggVariable
6151

6252
$options = array_get($data, 'options', []);
6353

64-
return $this->variableRepository->create(array_merge([
54+
return $this->repository->create(array_merge($data, [
6555
'egg_id' => $egg,
6656
'user_viewable' => in_array('user_viewable', $options),
6757
'user_editable' => in_array('user_editable', $options),
68-
], $data));
58+
]));
6959
}
7060
}

app/Services/Nests/NestCreationService.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ class NestCreationService
3232
* @param \Illuminate\Contracts\Config\Repository $config
3333
* @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $repository
3434
*/
35-
public function __construct(
36-
ConfigRepository $config,
37-
NestRepositoryInterface $repository
38-
) {
35+
public function __construct(ConfigRepository $config, NestRepositoryInterface $repository)
36+
{
3937
$this->config = $config;
4038
$this->repository = $repository;
4139
}

tests/TestCase.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,22 @@ abstract class TestCase extends BaseTestCase
88
{
99
use CreatesApplication;
1010

11+
/**
12+
* Setup tests.
13+
*/
1114
public function setUp()
1215
{
1316
parent::setUp();
17+
18+
$this->setKnownUuidFactory();
19+
}
20+
21+
/**
22+
* Handles the known UUID handling in certain unit tests. Use the "KnownUuid" trait
23+
* in order to enable this ability.
24+
*/
25+
public function setKnownUuidFactory()
26+
{
27+
// do nothing
1428
}
1529
}

tests/Traits/KnownUuid.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* This software is licensed under the terms of the MIT license.
7+
* https://opensource.org/licenses/MIT
8+
*/
9+
10+
namespace Tests\Traits;
11+
12+
use Mockery as m;
13+
use Ramsey\Uuid\Uuid;
14+
use Ramsey\Uuid\UuidFactory;
15+
16+
trait KnownUuid
17+
{
18+
/**
19+
* The known UUID string.
20+
*
21+
* @var string
22+
*/
23+
protected $knownUuid = 'ffb5c3a6-ab17-43ab-97f0-8ff37ccd7f5f';
24+
25+
/**
26+
* Setup a factory mock to produce the same UUID whenever called.
27+
*/
28+
public function setKnownUuidFactory()
29+
{
30+
$uuid = Uuid::fromString($this->getKnownUuid());
31+
$factoryMock = m::mock(UuidFactory::class . '[uuid4]', [
32+
'uuid4' => $uuid,
33+
]);
34+
35+
Uuid::setFactory($factoryMock);
36+
}
37+
38+
/**
39+
* Returns the known UUID for tests to use.
40+
*
41+
* @return string
42+
*/
43+
public function getKnownUuid(): string
44+
{
45+
return $this->knownUuid;
46+
}
47+
}

tests/Unit/Services/Services/Options/InstallScriptUpdateServiceTest.php renamed to tests/Unit/Services/Eggs/Scripts/InstallScriptServiceTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
use Mockery as m;
1414
use Tests\TestCase;
1515
use Pterodactyl\Models\Egg;
16+
use Pterodactyl\Services\Eggs\Scripts\InstallScriptService;
1617
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
17-
use Pterodactyl\Services\Services\Options\InstallScriptService;
18-
use Pterodactyl\Exceptions\Service\ServiceOption\InvalidCopyFromException;
18+
use Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException;
1919

20-
class InstallScriptUpdateServiceTest extends TestCase
20+
class InstallScriptServiceTest extends TestCase
2121
{
2222
/**
2323
* @var array
@@ -36,12 +36,12 @@ class InstallScriptUpdateServiceTest extends TestCase
3636
protected $model;
3737

3838
/**
39-
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
39+
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
4040
*/
4141
protected $repository;
4242

4343
/**
44-
* @var \Pterodactyl\Services\Services\Options\InstallScriptService
44+
* @var \Pterodactyl\Services\Eggs\Scripts\InstallScriptService
4545
*/
4646
protected $service;
4747

@@ -65,7 +65,7 @@ public function testUpdateWithValidCopyScriptFromAttribute()
6565
{
6666
$this->data['copy_script_from'] = 1;
6767

68-
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->service_id)->once()->andReturn(true);
68+
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(true);
6969
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
7070
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
7171

@@ -79,12 +79,12 @@ public function testUpdateWithInvalidCopyScriptFromAttribute()
7979
{
8080
$this->data['copy_script_from'] = 1;
8181

82-
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->service_id)->once()->andReturn(false);
82+
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(false);
8383
try {
8484
$this->service->handle($this->model, $this->data);
8585
} catch (Exception $exception) {
8686
$this->assertInstanceOf(InvalidCopyFromException::class, $exception);
87-
$this->assertEquals(trans('exceptions.service.options.invalid_copy_id'), $exception->getMessage());
87+
$this->assertEquals(trans('exceptions.nest.egg.invalid_copy_id'), $exception->getMessage());
8888
}
8989
}
9090

tests/Unit/Services/Services/Sharing/ServiceOptionExporterServiceTest.php renamed to tests/Unit/Services/Eggs/Sharing/EggExporterServiceTest.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
* https://opensource.org/licenses/MIT
88
*/
99

10-
namespace Tests\Unit\Services\Services\Sharing;
10+
namespace Tests\Unit\Services\Eggs\Sharing;
1111

1212
use Mockery as m;
1313
use Carbon\Carbon;
1414
use Tests\TestCase;
1515
use Pterodactyl\Models\Egg;
1616
use Pterodactyl\Models\EggVariable;
1717
use Tests\Assertions\NestedObjectAssertionsTrait;
18+
use Pterodactyl\Services\Eggs\Sharing\EggExporterService;
1819
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
19-
use Pterodactyl\Services\Services\Sharing\ServiceOptionExporterService;
2020

21-
class ServiceOptionExporterServiceTest extends TestCase
21+
class EggExporterServiceTest extends TestCase
2222
{
2323
use NestedObjectAssertionsTrait;
2424

@@ -33,7 +33,7 @@ class ServiceOptionExporterServiceTest extends TestCase
3333
protected $repository;
3434

3535
/**
36-
* @var \Pterodactyl\Services\Services\Sharing\ServiceOptionExporterService
36+
* @var \Pterodactyl\Services\Eggs\Sharing\EggExporterService
3737
*/
3838
protected $service;
3939

@@ -48,26 +48,28 @@ public function setUp()
4848
$this->carbon = new Carbon();
4949
$this->repository = m::mock(EggRepositoryInterface::class);
5050

51-
$this->service = new ServiceOptionExporterService($this->carbon, $this->repository);
51+
$this->service = new EggExporterService($this->repository);
5252
}
5353

5454
/**
5555
* Test that a JSON structure is returned.
5656
*/
5757
public function testJsonStructureIsExported()
5858
{
59-
$option = factory(Egg::class)->make();
60-
$option->variables = collect([$variable = factory(EggVariable::class)->make()]);
59+
$egg = factory(Egg::class)->make();
60+
$egg->variables = collect([$variable = factory(EggVariable::class)->make()]);
6161

62-
$this->repository->shouldReceive('getWithExportAttributes')->with($option->id)->once()->andReturn($option);
62+
$this->repository->shouldReceive('getWithExportAttributes')->with($egg->id)->once()->andReturn($egg);
6363

64-
$response = $this->service->handle($option->id);
64+
$response = $this->service->handle($egg->id);
6565
$this->assertNotEmpty($response);
6666

6767
$data = json_decode($response);
6868
$this->assertEquals(JSON_ERROR_NONE, json_last_error());
6969
$this->assertObjectHasNestedAttribute('meta.version', $data);
7070
$this->assertObjectNestedValueEquals('meta.version', 'PTDL_v1', $data);
71+
$this->assertObjectHasNestedAttribute('author', $data);
72+
$this->assertObjectNestedValueEquals('author', $egg->author, $data);
7173
$this->assertObjectHasNestedAttribute('exported_at', $data);
7274
$this->assertObjectNestedValueEquals('exported_at', Carbon::now()->toIso8601String(), $data);
7375
$this->assertObjectHasNestedAttribute('scripts.installation.script', $data);

0 commit comments

Comments
 (0)