Skip to content

Commit 609bf32

Browse files
committed
Add test for service option exporter
1 parent d95a63c commit 609bf32

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

app/helpers.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,31 @@ function is_digit($value)
4343
return is_bool($value) ? false : ctype_digit(strval($value));
4444
}
4545
}
46+
47+
if (! function_exists('object_get_strict')) {
48+
/**
49+
* Get an object using dot notation. An object key with a value of null is still considered valid
50+
* and will not trigger the response of a default value (unlike object_get).
51+
*
52+
* @param object $object
53+
* @param string $key
54+
* @param null $default
55+
* @return mixed
56+
*/
57+
function object_get_strict($object, $key, $default = null)
58+
{
59+
if (is_null($key) || trim($key) == '') {
60+
return $object;
61+
}
62+
63+
foreach (explode('.', $key) as $segment) {
64+
if (! is_object($object) || ! property_exists($object, $segment)) {
65+
return value($default);
66+
}
67+
68+
$object = $object->{$segment};
69+
}
70+
71+
return $object;
72+
}
73+
}

database/factories/ModelFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
'service_id' => $faker->unique()->randomNumber(),
108108
'name' => $faker->name,
109109
'description' => implode(' ', $faker->sentences(3)),
110+
'startup' => 'java -jar test.jar',
110111
'tag' => 'test@testfactory.com:' . $faker->unique()->randomNumber(8),
111112
];
112113
});
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\Assertions;
11+
12+
use PHPUnit\Framework\Assert;
13+
use PHPUnit_Util_InvalidArgumentHelper;
14+
15+
trait NestedObjectAssertionsTrait
16+
{
17+
/**
18+
* Assert that an object value matches an expected value.
19+
*
20+
* @param string $key
21+
* @param mixed $expected
22+
* @param object $object
23+
*/
24+
public function assertObjectNestedValueEquals(string $key, $expected, $object)
25+
{
26+
if (! is_object($object)) {
27+
throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'object');
28+
}
29+
30+
Assert::assertEquals($expected, object_get_strict($object, $key, '__TEST_FAILURE'), 'Assert that an object value equals a provided value.');
31+
}
32+
33+
/**
34+
* Assert that an object contains a nested key.
35+
*
36+
* @param string $key
37+
* @param object $object
38+
*/
39+
public function assertObjectHasNestedAttribute(string $key, $object)
40+
{
41+
if (! is_object($object)) {
42+
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
43+
}
44+
45+
Assert::assertNotEquals('__TEST_FAILURE', object_get_strict($object, $key, '__TEST_FAILURE'), 'Assert that an object contains a nested key.');
46+
}
47+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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\Unit\Services\Services\Sharing;
11+
12+
use Mockery as m;
13+
use Carbon\Carbon;
14+
use Tests\TestCase;
15+
use Pterodactyl\Models\ServiceOption;
16+
use Pterodactyl\Models\ServiceVariable;
17+
use Tests\Assertions\NestedObjectAssertionsTrait;
18+
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
19+
use Pterodactyl\Services\Services\Sharing\ServiceOptionExporterService;
20+
21+
class ServiceOptionExporterServiceTest extends TestCase
22+
{
23+
use NestedObjectAssertionsTrait;
24+
25+
/**
26+
* @var \Carbon\Carbon
27+
*/
28+
protected $carbon;
29+
30+
/**
31+
* @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface|\Mockery\Mock
32+
*/
33+
protected $repository;
34+
35+
/**
36+
* @var \Pterodactyl\Services\Services\Sharing\ServiceOptionExporterService
37+
*/
38+
protected $service;
39+
40+
/**
41+
* Setup tests.
42+
*/
43+
public function setUp()
44+
{
45+
parent::setUp();
46+
47+
Carbon::setTestNow(Carbon::now());
48+
$this->carbon = new Carbon();
49+
$this->repository = m::mock(ServiceOptionRepositoryInterface::class);
50+
51+
$this->service = new ServiceOptionExporterService($this->carbon, $this->repository);
52+
}
53+
54+
public function testJsonStructureIsExported()
55+
{
56+
$option = factory(ServiceOption::class)->make();
57+
$option->variables = collect([$variable = factory(ServiceVariable::class)->make()]);
58+
59+
$this->repository->shouldReceive('getWithExportAttributes')->with($option->id)->once()->andReturn($option);
60+
61+
$response = $this->service->handle($option->id);
62+
$this->assertNotEmpty($response);
63+
64+
$data = json_decode($response);
65+
$this->assertEquals(JSON_ERROR_NONE, json_last_error());
66+
$this->assertObjectHasNestedAttribute('meta.version', $data);
67+
$this->assertObjectNestedValueEquals('meta.version', 'PTDL_v1', $data);
68+
$this->assertObjectHasNestedAttribute('exported_at', $data);
69+
$this->assertObjectNestedValueEquals('exported_at', Carbon::now()->toIso8601String(), $data);
70+
$this->assertObjectHasNestedAttribute('scripts.installation.script', $data);
71+
$this->assertObjectHasNestedAttribute('scripts.installation.container', $data);
72+
$this->assertObjectHasNestedAttribute('scripts.installation.entrypoint', $data);
73+
$this->assertObjectHasAttribute('variables', $data);
74+
$this->assertArrayHasKey('0', $data->variables);
75+
$this->assertObjectHasAttribute('name', $data->variables[0]);
76+
$this->assertObjectNestedValueEquals('name', $variable->name, $data->variables[0]);
77+
}
78+
}

0 commit comments

Comments
 (0)