Skip to content

Commit 29ac166

Browse files
committed
Fix failing tests
1 parent 8952043 commit 29ac166

File tree

2 files changed

+73
-18
lines changed

2 files changed

+73
-18
lines changed

tests/Unit/Services/Services/Options/OptionCreationServiceTest.php

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@
1212
use Exception;
1313
use Mockery as m;
1414
use Tests\TestCase;
15+
use Ramsey\Uuid\Uuid;
1516
use Pterodactyl\Models\ServiceOption;
17+
use Illuminate\Contracts\Config\Repository;
1618
use Pterodactyl\Services\Services\Options\OptionCreationService;
1719
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
1820
use Pterodactyl\Exceptions\Service\ServiceOption\NoParentConfigurationFoundException;
1921

2022
class OptionCreationServiceTest extends TestCase
2123
{
2224
/**
23-
* @var \Pterodactyl\Models\ServiceOption
25+
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
2426
*/
25-
protected $model;
27+
protected $config;
2628

2729
/**
28-
* @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface
30+
* @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface|\Mockery\Mock
2931
*/
3032
protected $repository;
3133

@@ -34,57 +36,102 @@ class OptionCreationServiceTest extends TestCase
3436
*/
3537
protected $service;
3638

39+
/**
40+
* @var \Ramsey\Uuid\Uuid|\Mockery\Mock
41+
*/
42+
protected $uuid;
43+
3744
/**
3845
* Setup tests.
3946
*/
4047
public function setUp()
4148
{
4249
parent::setUp();
4350

44-
$this->model = factory(ServiceOption::class)->make();
51+
$this->config = m::mock(Repository::class);
4552
$this->repository = m::mock(ServiceOptionRepositoryInterface::class);
53+
$this->uuid = m::mock('overload:' . Uuid::class);
4654

47-
$this->service = new OptionCreationService($this->repository);
55+
$this->service = new OptionCreationService($this->config, $this->repository);
4856
}
4957

5058
/**
5159
* Test that a new model is created when not using the config from attribute.
5260
*/
5361
public function testCreateNewModelWithoutUsingConfigFrom()
5462
{
55-
$this->repository->shouldReceive('create')->with(['name' => $this->model->name, 'config_from' => null])
56-
->once()->andReturn($this->model);
63+
$model = factory(ServiceOption::class)->make();
64+
65+
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com');
66+
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-string');
67+
$this->repository->shouldReceive('create')->with([
68+
'name' => $model->name,
69+
'config_from' => null,
70+
'tag' => 'test@example.com:' . $model->tag,
71+
'uuid' => 'uuid-string',
72+
], true, true)->once()->andReturn($model);
5773

58-
$response = $this->service->handle(['name' => $this->model->name]);
74+
$response = $this->service->handle(['name' => $model->name, 'tag' => $model->tag]);
5975

6076
$this->assertNotEmpty($response);
6177
$this->assertNull(object_get($response, 'config_from'));
62-
$this->assertEquals($this->model->name, $response->name);
78+
$this->assertEquals($model->name, $response->name);
79+
}
80+
81+
/**
82+
* Test that passing a bad tag into the function will set the correct tag.
83+
*/
84+
public function testCreateNewModelUsingLongTagForm()
85+
{
86+
$model = factory(ServiceOption::class)->make([
87+
'tag' => 'test@example.com:tag',
88+
]);
89+
90+
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com');
91+
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-string');
92+
$this->repository->shouldReceive('create')->with([
93+
'name' => $model->name,
94+
'config_from' => null,
95+
'tag' => $model->tag,
96+
'uuid' => 'uuid-string',
97+
], true, true)->once()->andReturn($model);
98+
99+
$response = $this->service->handle(['name' => $model->name, 'tag' => 'bad@example.com:tag']);
100+
101+
$this->assertNotEmpty($response);
102+
$this->assertNull(object_get($response, 'config_from'));
103+
$this->assertEquals($model->name, $response->name);
104+
$this->assertEquals('test@example.com:tag', $response->tag);
63105
}
64106

65107
/**
66108
* Test that a new model is created when using the config from attribute.
67109
*/
68110
public function testCreateNewModelUsingConfigFrom()
69111
{
112+
$model = factory(ServiceOption::class)->make();
113+
70114
$data = [
71-
'name' => $this->model->name,
72-
'service_id' => $this->model->service_id,
115+
'name' => $model->name,
116+
'service_id' => $model->service_id,
117+
'tag' => 'test@example.com:tag',
73118
'config_from' => 1,
119+
'uuid' => 'uuid-string',
74120
];
75121

76122
$this->repository->shouldReceive('findCountWhere')->with([
77123
['service_id', '=', $data['service_id']],
78124
['id', '=', $data['config_from']],
79125
])->once()->andReturn(1);
80126

81-
$this->repository->shouldReceive('create')->with($data)
82-
->once()->andReturn($this->model);
127+
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('test@example.com');
128+
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-string');
129+
$this->repository->shouldReceive('create')->with($data, true, true)->once()->andReturn($model);
83130

84131
$response = $this->service->handle($data);
85132

86133
$this->assertNotEmpty($response);
87-
$this->assertEquals($response, $this->model);
134+
$this->assertEquals($response, $model);
88135
}
89136

90137
/**

tests/Unit/Services/Services/ServiceCreationServiceTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Mockery as m;
1313
use Tests\TestCase;
14+
use Ramsey\Uuid\Uuid;
1415
use Pterodactyl\Models\Service;
1516
use Illuminate\Contracts\Config\Repository;
1617
use Pterodactyl\Traits\Services\CreatesServiceIndex;
@@ -22,12 +23,12 @@ class ServiceCreationServiceTest extends TestCase
2223
use CreatesServiceIndex;
2324

2425
/**
25-
* @var \Illuminate\Contracts\Config\Repository
26+
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
2627
*/
2728
protected $config;
2829

2930
/**
30-
* @var \Pterodactyl\Contracts\Repository\ServiceRepositoryInterface
31+
* @var \Pterodactyl\Contracts\Repository\ServiceRepositoryInterface|\Mockery\Mock
3132
*/
3233
protected $repository;
3334

@@ -36,6 +37,11 @@ class ServiceCreationServiceTest extends TestCase
3637
*/
3738
protected $service;
3839

40+
/**
41+
* @var \Ramsey\Uuid\Uuid|\Mockery\Mock
42+
*/
43+
protected $uuid;
44+
3945
/**
4046
* Setup tests.
4147
*/
@@ -45,6 +51,7 @@ public function setUp()
4551

4652
$this->config = m::mock(Repository::class);
4753
$this->repository = m::mock(ServiceRepositoryInterface::class);
54+
$this->uuid = m::mock('overload:' . Uuid::class);
4855

4956
$this->service = new ServiceCreationService($this->config, $this->repository);
5057
}
@@ -62,15 +69,16 @@ public function testCreateNewService()
6269
'startup' => $model->startup,
6370
];
6471

72+
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-0000');
6573
$this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('0000-author');
6674
$this->repository->shouldReceive('create')->with([
75+
'uuid' => 'uuid-0000',
6776
'author' => '0000-author',
6877
'name' => $data['name'],
6978
'description' => $data['description'],
70-
'folder' => $data['folder'],
7179
'startup' => $data['startup'],
7280
'index_file' => $this->getIndexScript(),
73-
])->once()->andReturn($model);
81+
], true, true)->once()->andReturn($model);
7482

7583
$response = $this->service->handle($data);
7684
$this->assertInstanceOf(Service::class, $response);

0 commit comments

Comments
 (0)