Skip to content

Commit 364adb1

Browse files
committed
Add tests for service option services
1 parent b8d7d99 commit 364adb1

File tree

6 files changed

+475
-7
lines changed

6 files changed

+475
-7
lines changed

app/Services/Services/Options/InstallScriptUpdateService.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ class InstallScriptUpdateService
3333
/**
3434
* @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface
3535
*/
36-
protected $serviceOptionRepository;
36+
protected $repository;
3737

3838
/**
3939
* InstallScriptUpdateService constructor.
4040
*
41-
* @param \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface $serviceOptionRepository
41+
* @param \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface $repository
4242
*/
43-
public function __construct(ServiceOptionRepositoryInterface $serviceOptionRepository)
43+
public function __construct(ServiceOptionRepositoryInterface $repository)
4444
{
45-
$this->serviceOptionRepository = $serviceOptionRepository;
45+
$this->repository = $repository;
4646
}
4747

4848
/**
@@ -52,21 +52,22 @@ public function __construct(ServiceOptionRepositoryInterface $serviceOptionRepos
5252
* @param array $data
5353
*
5454
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
55+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
5556
* @throws \Pterodactyl\Exceptions\Services\ServiceOption\InvalidCopyFromException
5657
*/
5758
public function handle($option, array $data)
5859
{
5960
if (! $option instanceof ServiceOption) {
60-
$option = $this->serviceOptionRepository->find($option);
61+
$option = $this->repository->find($option);
6162
}
6263

6364
if (! is_null(array_get($data, 'copy_script_from'))) {
64-
if (! $this->serviceOptionRepository->isCopiableScript(array_get($data, 'copy_script_from'), $option->service_id)) {
65+
if (! $this->repository->isCopiableScript(array_get($data, 'copy_script_from'), $option->service_id)) {
6566
throw new InvalidCopyFromException(trans('admin/exceptions.service.options.invalid_copy_id'));
6667
}
6768
}
6869

69-
$this->serviceOptionRepository->withoutFresh()->update($option->id, [
70+
$this->repository->withoutFresh()->update($option->id, [
7071
'script_install' => array_get($data, 'script_install'),
7172
'script_is_privileged' => array_get($data, 'script_is_privileged'),
7273
'script_entry' => array_get($data, 'script_entry'),

database/factories/ModelFactory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@
8888
];
8989
});
9090

91+
$factory->define(Pterodactyl\Models\ServiceOption::class, function (Faker\Generator $faker) {
92+
return [
93+
'id' => $faker->unique()->randomNumber(),
94+
'service_id' => $faker->unique()->randomNumber(),
95+
'name' => $faker->name,
96+
'description' => $faker->sentences(3),
97+
'tag' => $faker->unique()->randomNumber(5),
98+
];
99+
});
100+
91101
$factory->define(Pterodactyl\Models\ServiceVariable::class, function (Faker\Generator $faker) {
92102
return [
93103
'id' => $faker->unique()->randomNumber(),
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Tests\Unit\Services\Services\Options;
26+
27+
use Exception;
28+
use Mockery as m;
29+
use Tests\TestCase;
30+
use Pterodactyl\Models\ServiceOption;
31+
use Pterodactyl\Services\Services\Options\InstallScriptUpdateService;
32+
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
33+
use Pterodactyl\Exceptions\Services\ServiceOption\InvalidCopyFromException;
34+
35+
class InstallScriptUpdateServiceTest extends TestCase
36+
{
37+
/**
38+
* @var array
39+
*/
40+
protected $data = [
41+
'script_install' => 'test-script',
42+
'script_is_privileged' => true,
43+
'script_entry' => '/bin/bash',
44+
'script_container' => 'ubuntu',
45+
'copy_script_from' => null,
46+
];
47+
48+
/**
49+
* @var \Pterodactyl\Models\ServiceOption
50+
*/
51+
protected $model;
52+
53+
/**
54+
* @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface
55+
*/
56+
protected $repository;
57+
58+
/**
59+
* @var \Pterodactyl\Services\Services\Options\InstallScriptUpdateService
60+
*/
61+
protected $service;
62+
63+
/**
64+
* Setup tests.
65+
*/
66+
public function setUp()
67+
{
68+
parent::setUp();
69+
70+
$this->model = factory(ServiceOption::class)->make();
71+
$this->repository = m::mock(ServiceOptionRepositoryInterface::class);
72+
73+
$this->service = new InstallScriptUpdateService($this->repository);
74+
}
75+
76+
/**
77+
* Test that passing a new copy_script_from attribute works properly.
78+
*/
79+
public function testUpdateWithValidCopyScriptFromAttribute()
80+
{
81+
$this->data['copy_script_from'] = 1;
82+
83+
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->service_id)->once()->andReturn(true);
84+
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
85+
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
86+
87+
$this->service->handle($this->model, $this->data);
88+
}
89+
90+
/**
91+
* Test that an exception gets raised when the script is not copiable.
92+
*/
93+
public function testUpdateWithInvalidCopyScriptFromAttribute()
94+
{
95+
$this->data['copy_script_from'] = 1;
96+
97+
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->service_id)->once()->andReturn(false);
98+
try {
99+
$this->service->handle($this->model, $this->data);
100+
} catch (Exception $exception) {
101+
$this->assertInstanceOf(InvalidCopyFromException::class, $exception);
102+
$this->assertEquals(trans('admin/exceptions.service.options.invalid_copy_id'), $exception->getMessage());
103+
}
104+
}
105+
106+
/**
107+
* Test standard functionality.
108+
*/
109+
public function testUpdateWithoutNewCopyScriptFromAttribute()
110+
{
111+
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
112+
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
113+
114+
$this->service->handle($this->model, $this->data);
115+
}
116+
117+
/**
118+
* Test that an integer can be passed in place of a model.
119+
*/
120+
public function testFunctionAcceptsIntegerInPlaceOfModel()
121+
{
122+
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
123+
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
124+
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
125+
126+
$this->service->handle($this->model->id, $this->data);
127+
}
128+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Tests\Unit\Services\Services\Options;
26+
27+
use Exception;
28+
use Mockery as m;
29+
use Tests\TestCase;
30+
use Pterodactyl\Models\ServiceOption;
31+
use Pterodactyl\Services\Services\Options\OptionCreationService;
32+
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
33+
use Pterodactyl\Exceptions\Services\ServiceOption\NoParentConfigurationFoundException;
34+
35+
class OptionCreationServiceTest extends TestCase
36+
{
37+
/**
38+
* @var \Pterodactyl\Models\ServiceOption
39+
*/
40+
protected $model;
41+
42+
/**
43+
* @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface
44+
*/
45+
protected $repository;
46+
47+
/**
48+
* @var \Pterodactyl\Services\Services\Options\OptionCreationService
49+
*/
50+
protected $service;
51+
52+
/**
53+
* Setup tests.
54+
*/
55+
public function setUp()
56+
{
57+
parent::setUp();
58+
59+
$this->model = factory(ServiceOption::class)->make();
60+
$this->repository = m::mock(ServiceOptionRepositoryInterface::class);
61+
62+
$this->service = new OptionCreationService($this->repository);
63+
}
64+
65+
/**
66+
* Test that a new model is created when not using the config from attribute.
67+
*/
68+
public function testCreateNewModelWithoutUsingConfigFrom()
69+
{
70+
$this->repository->shouldReceive('create')->with(['name' => $this->model->name, 'config_from' => null])
71+
->once()->andReturn($this->model);
72+
73+
$response = $this->service->handle(['name' => $this->model->name]);
74+
75+
$this->assertNotEmpty($response);
76+
$this->assertNull(object_get($response, 'config_from'));
77+
$this->assertEquals($this->model->name, $response->name);
78+
}
79+
80+
/**
81+
* Test that a new model is created when using the config from attribute.
82+
*/
83+
public function testCreateNewModelUsingConfigFrom()
84+
{
85+
$data = [
86+
'name' => $this->model->name,
87+
'service_id' => $this->model->service_id,
88+
'config_from' => 1,
89+
];
90+
91+
$this->repository->shouldReceive('findCountWhere')->with([
92+
['service_id', '=', $data['service_id']],
93+
['id', '=', $data['config_from']]
94+
])->once()->andReturn(1);
95+
96+
$this->repository->shouldReceive('create')->with($data)
97+
->once()->andReturn($this->model);
98+
99+
$response = $this->service->handle($data);
100+
101+
$this->assertNotEmpty($response);
102+
$this->assertEquals($response, $this->model);
103+
}
104+
105+
/**
106+
* Test that an exception is thrown if no parent configuration can be located.
107+
*/
108+
public function testExceptionIsThrownIfNoParentConfigurationIsFound()
109+
{
110+
$this->repository->shouldReceive('findCountWhere')->with([
111+
['service_id', '=', null],
112+
['id', '=', 1]
113+
])->once()->andReturn(0);
114+
115+
try {
116+
$this->service->handle(['config_from' => 1]);
117+
} catch (Exception $exception) {
118+
$this->assertInstanceOf(NoParentConfigurationFoundException::class, $exception);
119+
$this->assertEquals(trans('admin/exceptions.service.options.must_be_child'), $exception->getMessage());
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)