|
| 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