Skip to content

Commit 6e02e94

Browse files
committed
Egg tests updated
1 parent 4d52ba2 commit 6e02e94

File tree

7 files changed

+93
-96
lines changed

7 files changed

+93
-96
lines changed

app/Services/Eggs/EggCreationService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function handle(array $data): Egg
5959
]);
6060

6161
if ($results !== 1) {
62-
throw new NoParentConfigurationFoundException(trans('exceptions.service.options.must_be_child'));
62+
throw new NoParentConfigurationFoundException(trans('exceptions.nest.egg.must_be_child'));
6363
}
6464
}
6565

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Ramsey\Uuid\Uuid;
1414
use Ramsey\Uuid\UuidFactory;
1515

16-
trait KnownUuid
16+
trait MocksUuids
1717
{
1818
/**
1919
* The known UUID string.

tests/Unit/Services/Services/Options/OptionCreationServiceTest.php renamed to tests/Unit/Services/Eggs/EggCreationServiceTest.php

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
use Exception;
1313
use Mockery as m;
1414
use Tests\TestCase;
15-
use Ramsey\Uuid\Uuid;
1615
use Pterodactyl\Models\Egg;
16+
use Tests\Traits\MocksUuids;
1717
use Illuminate\Contracts\Config\Repository;
18+
use Pterodactyl\Exceptions\PterodactylException;
19+
use Pterodactyl\Services\Eggs\EggCreationService;
1820
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
19-
use Pterodactyl\Services\Services\Options\EggCreationService;
20-
use Pterodactyl\Exceptions\Service\ServiceOption\NoParentConfigurationFoundException;
21+
use Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException;
2122

22-
class OptionCreationServiceTest extends TestCase
23+
class EggCreationServiceTest extends TestCase
2324
{
25+
use MocksUuids;
26+
2427
/**
2528
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
2629
*/
@@ -32,15 +35,10 @@ class OptionCreationServiceTest extends TestCase
3235
protected $repository;
3336

3437
/**
35-
* @var \Pterodactyl\Services\Services\Options\EggCreationService
38+
* @var \Pterodactyl\Services\Eggs\EggCreationService
3639
*/
3740
protected $service;
3841

39-
/**
40-
* @var \Ramsey\Uuid\Uuid|\Mockery\Mock
41-
*/
42-
protected $uuid;
43-
4442
/**
4543
* Setup tests.
4644
*/
@@ -50,7 +48,6 @@ public function setUp()
5048

5149
$this->config = m::mock(Repository::class);
5250
$this->repository = m::mock(EggRepositoryInterface::class);
53-
$this->uuid = m::mock('overload:' . Uuid::class);
5451

5552
$this->service = new EggCreationService($this->config, $this->repository);
5653
}
@@ -60,80 +57,73 @@ public function setUp()
6057
*/
6158
public function testCreateNewModelWithoutUsingConfigFrom()
6259
{
63-
$model = factory(Egg::class)->make([
64-
'tag' => str_random(10),
65-
]);
60+
$model = factory(Egg::class)->make();
6661

6762
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com');
68-
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-string');
6963
$this->repository->shouldReceive('create')->with([
70-
'name' => $model->name,
71-
'tag' => 'test@example.com:' . $model->tag,
64+
'uuid' => $this->getKnownUuid(),
65+
'author' => 'test@example.com',
7266
'config_from' => null,
73-
'uuid' => 'uuid-string',
67+
'name' => $model->name,
7468
], true, true)->once()->andReturn($model);
7569

76-
$response = $this->service->handle(['name' => $model->name, 'tag' => $model->tag]);
70+
$response = $this->service->handle(['name' => $model->name]);
7771

7872
$this->assertNotEmpty($response);
7973
$this->assertNull(object_get($response, 'config_from'));
8074
$this->assertEquals($model->name, $response->name);
8175
}
8276

8377
/**
84-
* Test that passing a bad tag into the function will set the correct tag.
78+
* Test that a new model is created when using the config from attribute.
8579
*/
86-
public function testCreateNewModelUsingLongTagForm()
80+
public function testCreateNewModelUsingConfigFrom()
8781
{
88-
$model = factory(Egg::class)->make([
89-
'tag' => 'test@example.com:tag',
90-
]);
82+
$model = factory(Egg::class)->make();
83+
84+
$this->repository->shouldReceive('findCountWhere')->with([
85+
['nest_id', '=', $model->nest_id],
86+
['id', '=', 12345],
87+
])->once()->andReturn(1);
9188

9289
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com');
93-
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-string');
9490
$this->repository->shouldReceive('create')->with([
95-
'name' => $model->name,
96-
'config_from' => null,
97-
'tag' => $model->tag,
98-
'uuid' => 'uuid-string',
91+
'nest_id' => $model->nest_id,
92+
'config_from' => 12345,
93+
'uuid' => $this->getKnownUuid(),
94+
'author' => 'test@example.com',
9995
], true, true)->once()->andReturn($model);
10096

101-
$response = $this->service->handle(['name' => $model->name, 'tag' => 'bad@example.com:tag']);
97+
$response = $this->service->handle([
98+
'nest_id' => $model->nest_id,
99+
'config_from' => 12345,
100+
]);
102101

103102
$this->assertNotEmpty($response);
104-
$this->assertNull(object_get($response, 'config_from'));
105-
$this->assertEquals($model->name, $response->name);
106-
$this->assertEquals('test@example.com:tag', $response->tag);
103+
$this->assertEquals($response, $model);
107104
}
108105

109106
/**
110-
* Test that a new model is created when using the config from attribute.
107+
* Test that certain data, such as the UUID or author takes priority over data
108+
* that is passed into the function.
111109
*/
112-
public function testCreateNewModelUsingConfigFrom()
110+
public function testDataProvidedByHandlerTakesPriorityOverPassedData()
113111
{
114112
$model = factory(Egg::class)->make();
115113

116-
$data = [
117-
'name' => $model->name,
118-
'service_id' => $model->service_id,
119-
'tag' => 'test@example.com:tag',
120-
'config_from' => 1,
121-
'uuid' => 'uuid-string',
122-
];
123-
124-
$this->repository->shouldReceive('findCountWhere')->with([
125-
['service_id', '=', $data['service_id']],
126-
['id', '=', $data['config_from']],
127-
])->once()->andReturn(1);
128-
129114
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com');
130-
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-string');
131-
$this->repository->shouldReceive('create')->with($data, true, true)->once()->andReturn($model);
115+
$this->repository->shouldReceive('create')->with([
116+
'uuid' => $this->getKnownUuid(),
117+
'author' => 'test@example.com',
118+
'config_from' => null,
119+
'name' => $model->name,
120+
], true, true)->once()->andReturn($model);
132121

133-
$response = $this->service->handle($data);
122+
$response = $this->service->handle(['name' => $model->name, 'uuid' => 'should-be-ignored', 'author' => 'should-be-ignored']);
134123

135124
$this->assertNotEmpty($response);
136-
$this->assertEquals($response, $model);
125+
$this->assertNull(object_get($response, 'config_from'));
126+
$this->assertEquals($model->name, $response->name);
137127
}
138128

139129
/**
@@ -142,15 +132,15 @@ public function testCreateNewModelUsingConfigFrom()
142132
public function testExceptionIsThrownIfNoParentConfigurationIsFound()
143133
{
144134
$this->repository->shouldReceive('findCountWhere')->with([
145-
['service_id', '=', null],
135+
['nest_id', '=', null],
146136
['id', '=', 1],
147137
])->once()->andReturn(0);
148138

149139
try {
150140
$this->service->handle(['config_from' => 1]);
151-
} catch (Exception $exception) {
141+
} catch (PterodactylException $exception) {
152142
$this->assertInstanceOf(NoParentConfigurationFoundException::class, $exception);
153-
$this->assertEquals(trans('exceptions.service.options.must_be_child'), $exception->getMessage());
143+
$this->assertEquals(trans('exceptions.nest.egg.must_be_child'), $exception->getMessage());
154144
}
155145
}
156146
}

tests/Unit/Services/Services/Options/OptionDeletionServiceTest.php renamed to tests/Unit/Services/Eggs/EggDeletionServiceTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
use Mockery as m;
1313
use Tests\TestCase;
14-
use Pterodactyl\Exceptions\DisplayException;
14+
use Pterodactyl\Exceptions\PterodactylException;
15+
use Pterodactyl\Services\Eggs\EggDeletionService;
1516
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
17+
use Pterodactyl\Exceptions\Service\Egg\HasChildrenException;
1618
use Pterodactyl\Exceptions\Service\HasActiveServersException;
17-
use Pterodactyl\Services\Services\Options\EggDeletionService;
1819
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
19-
use Pterodactyl\Exceptions\Service\ServiceOption\HasChildrenException;
2020

21-
class OptionDeletionServiceTest extends TestCase
21+
class EggDeletionServiceTest extends TestCase
2222
{
2323
/**
2424
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
@@ -31,7 +31,7 @@ class OptionDeletionServiceTest extends TestCase
3131
protected $serverRepository;
3232

3333
/**
34-
* @var \Pterodactyl\Services\Services\Options\EggDeletionService
34+
* @var \Pterodactyl\Services\Eggs\EggDeletionService
3535
*/
3636
protected $service;
3737

@@ -49,45 +49,45 @@ public function setUp()
4949
}
5050

5151
/**
52-
* Test that option is deleted if no servers are found.
52+
* Test that Egg is deleted if no servers are found.
5353
*/
54-
public function testOptionIsDeletedIfNoServersAreFound()
54+
public function testEggIsDeletedIfNoServersAreFound()
5555
{
56-
$this->serverRepository->shouldReceive('findCountWhere')->with([['option_id', '=', 1]])->once()->andReturn(0);
56+
$this->serverRepository->shouldReceive('findCountWhere')->with([['egg_id', '=', 1]])->once()->andReturn(0);
5757
$this->repository->shouldReceive('findCountWhere')->with([['config_from', '=', 1]])->once()->andReturn(0);
5858
$this->repository->shouldReceive('delete')->with(1)->once()->andReturn(1);
5959

6060
$this->assertEquals(1, $this->service->handle(1));
6161
}
6262

6363
/**
64-
* Test that option is not deleted if servers are found.
64+
* Test that Egg is not deleted if servers are found.
6565
*/
6666
public function testExceptionIsThrownIfServersAreFound()
6767
{
68-
$this->serverRepository->shouldReceive('findCountWhere')->with([['option_id', '=', 1]])->once()->andReturn(1);
68+
$this->serverRepository->shouldReceive('findCountWhere')->with([['egg_id', '=', 1]])->once()->andReturn(1);
6969

7070
try {
7171
$this->service->handle(1);
72-
} catch (DisplayException $exception) {
72+
} catch (PterodactylException $exception) {
7373
$this->assertInstanceOf(HasActiveServersException::class, $exception);
74-
$this->assertEquals(trans('exceptions.service.options.delete_has_servers'), $exception->getMessage());
74+
$this->assertEquals(trans('exceptions.nest.egg.delete_has_servers'), $exception->getMessage());
7575
}
7676
}
7777

7878
/**
79-
* Test that an exception is thrown if children options exist.
79+
* Test that an exception is thrown if children Eggs exist.
8080
*/
8181
public function testExceptionIsThrownIfChildrenArePresent()
8282
{
83-
$this->serverRepository->shouldReceive('findCountWhere')->with([['option_id', '=', 1]])->once()->andReturn(0);
83+
$this->serverRepository->shouldReceive('findCountWhere')->with([['egg_id', '=', 1]])->once()->andReturn(0);
8484
$this->repository->shouldReceive('findCountWhere')->with([['config_from', '=', 1]])->once()->andReturn(1);
8585

8686
try {
8787
$this->service->handle(1);
88-
} catch (DisplayException $exception) {
88+
} catch (PterodactylException $exception) {
8989
$this->assertInstanceOf(HasChildrenException::class, $exception);
90-
$this->assertEquals(trans('exceptions.service.options.has_children'), $exception->getMessage());
90+
$this->assertEquals(trans('exceptions.nest.egg.has_children'), $exception->getMessage());
9191
}
9292
}
9393
}

tests/Unit/Services/Services/Options/OptionUpdateServiceTest.php renamed to tests/Unit/Services/Eggs/EggUpdateServiceTest.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,25 @@
1313
use Mockery as m;
1414
use Tests\TestCase;
1515
use Pterodactyl\Models\Egg;
16-
use Pterodactyl\Services\Services\Options\EggUpdateService;
16+
use Pterodactyl\Services\Eggs\EggUpdateService;
17+
use Pterodactyl\Exceptions\PterodactylException;
1718
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
18-
use Pterodactyl\Exceptions\Service\ServiceOption\NoParentConfigurationFoundException;
19+
use Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException;
1920

20-
class OptionUpdateServiceTest extends TestCase
21+
class EggUpdateServiceTest extends TestCase
2122
{
2223
/**
2324
* @var \Pterodactyl\Models\Egg
2425
*/
2526
protected $model;
2627

2728
/**
28-
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
29+
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
2930
*/
3031
protected $repository;
3132

3233
/**
33-
* @var \Pterodactyl\Services\Services\Options\EggUpdateService
34+
* @var \Pterodactyl\Services\Eggs\EggUpdateService
3435
*/
3536
protected $service;
3637

@@ -48,30 +49,34 @@ public function setUp()
4849
}
4950

5051
/**
51-
* Test that an option is updated when no config_from attribute is passed.
52+
* Test that an Egg is updated when no config_from attribute is passed.
5253
*/
53-
public function testOptionIsUpdatedWhenNoConfigFromIsProvided()
54+
public function testEggIsUpdatedWhenNoConfigFromIsProvided()
5455
{
55-
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
56-
->shouldReceive('update')->with($this->model->id, ['test_field' => 'field_value'])->once()->andReturnNull();
56+
$this->repository->shouldReceive('withoutFresh->update')
57+
->with($this->model->id, ['test_field' => 'field_value'])->once()->andReturnNull();
5758

5859
$this->service->handle($this->model, ['test_field' => 'field_value']);
60+
61+
$this->assertTrue(true);
5962
}
6063

6164
/**
62-
* Test that option is updated when a valid config_from attribute is passed.
65+
* Test that Egg is updated when a valid config_from attribute is passed.
6366
*/
6467
public function testOptionIsUpdatedWhenValidConfigFromIsPassed()
6568
{
6669
$this->repository->shouldReceive('findCountWhere')->with([
67-
['service_id', '=', $this->model->service_id],
70+
['nest_id', '=', $this->model->nest_id],
6871
['id', '=', 1],
6972
])->once()->andReturn(1);
7073

71-
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
72-
->shouldReceive('update')->with($this->model->id, ['config_from' => 1])->once()->andReturnNull();
74+
$this->repository->shouldReceive('withoutFresh->update')
75+
->with($this->model->id, ['config_from' => 1])->once()->andReturnNull();
7376

7477
$this->service->handle($this->model, ['config_from' => 1]);
78+
79+
$this->assertTrue(true);
7580
}
7681

7782
/**
@@ -80,27 +85,29 @@ public function testOptionIsUpdatedWhenValidConfigFromIsPassed()
8085
public function testExceptionIsThrownIfInvalidParentConfigIsPassed()
8186
{
8287
$this->repository->shouldReceive('findCountWhere')->with([
83-
['service_id', '=', $this->model->service_id],
88+
['nest_id', '=', $this->model->nest_id],
8489
['id', '=', 1],
8590
])->once()->andReturn(0);
8691

8792
try {
8893
$this->service->handle($this->model, ['config_from' => 1]);
89-
} catch (Exception $exception) {
94+
} catch (PterodactylException $exception) {
9095
$this->assertInstanceOf(NoParentConfigurationFoundException::class, $exception);
91-
$this->assertEquals(trans('exceptions.service.options.must_be_child'), $exception->getMessage());
96+
$this->assertEquals(trans('exceptions.nest.egg.must_be_child'), $exception->getMessage());
9297
}
9398
}
9499

95100
/**
96-
* Test that an integer linking to a model can be passed in place of the ServiceOption model.
101+
* Test that an integer linking to a model can be passed in place of the Egg model.
97102
*/
98103
public function testIntegerCanBePassedInPlaceOfModel()
99104
{
100105
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
101-
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
102-
->shouldReceive('update')->with($this->model->id, ['test_field' => 'field_value'])->once()->andReturnNull();
106+
$this->repository->shouldReceive('withoutFresh->update')
107+
->with($this->model->id, ['test_field' => 'field_value'])->once()->andReturnNull();
103108

104109
$this->service->handle($this->model->id, ['test_field' => 'field_value']);
110+
111+
$this->assertTrue(true);
105112
}
106113
}

0 commit comments

Comments
 (0)