misc_pterodactyl-panel/tests/Unit/Services/Eggs/Sharing/EggExporterServiceTest.php

70 lines
2.4 KiB
PHP
Raw Normal View History

2017-10-05 04:42:04 +00:00
<?php
2017-10-08 04:29:08 +00:00
namespace Tests\Unit\Services\Eggs\Sharing;
2017-10-05 04:42:04 +00:00
use Mockery as m;
use Carbon\Carbon;
use Tests\TestCase;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\EggVariable;
2017-10-05 04:42:04 +00:00
use Tests\Assertions\NestedObjectAssertionsTrait;
2017-10-08 04:29:08 +00:00
use Pterodactyl\Services\Eggs\Sharing\EggExporterService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
2017-10-05 04:42:04 +00:00
2017-10-08 04:29:08 +00:00
class EggExporterServiceTest extends TestCase
2017-10-05 04:42:04 +00:00
{
use NestedObjectAssertionsTrait;
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
2017-10-05 04:42:04 +00:00
*/
protected $repository;
/**
* Setup tests.
*/
public function setUp(): void
2017-10-05 04:42:04 +00:00
{
parent::setUp();
Carbon::setTestNow(Carbon::now());
2020-10-06 04:29:35 +00:00
$this->repository = m::mock(EggRepositoryInterface::class);
2017-10-05 04:42:04 +00:00
}
2017-10-05 04:52:25 +00:00
/**
* Test that a JSON structure is returned.
*/
2017-10-05 04:42:04 +00:00
public function testJsonStructureIsExported()
{
2020-10-06 04:29:35 +00:00
$egg = factory(Egg::class)->make([
'id' => 123,
'nest_id' => 456,
]);
2017-10-08 04:29:08 +00:00
$egg->variables = collect([$variable = factory(EggVariable::class)->make()]);
2017-10-05 04:42:04 +00:00
2017-10-08 04:29:08 +00:00
$this->repository->shouldReceive('getWithExportAttributes')->with($egg->id)->once()->andReturn($egg);
2017-10-05 04:42:04 +00:00
2020-10-06 04:29:35 +00:00
$service = new EggExporterService($this->repository);
$response = $service->handle($egg->id);
2017-10-05 04:42:04 +00:00
$this->assertNotEmpty($response);
$data = json_decode($response);
$this->assertEquals(JSON_ERROR_NONE, json_last_error());
$this->assertObjectHasNestedAttribute('meta.version', $data);
$this->assertObjectNestedValueEquals('meta.version', 'PTDL_v1', $data);
2017-10-08 04:29:08 +00:00
$this->assertObjectHasNestedAttribute('author', $data);
$this->assertObjectNestedValueEquals('author', $egg->author, $data);
2017-10-05 04:42:04 +00:00
$this->assertObjectHasNestedAttribute('exported_at', $data);
$this->assertObjectNestedValueEquals('exported_at', Carbon::now()->toIso8601String(), $data);
$this->assertObjectHasNestedAttribute('scripts.installation.script', $data);
$this->assertObjectHasNestedAttribute('scripts.installation.container', $data);
$this->assertObjectHasNestedAttribute('scripts.installation.entrypoint', $data);
$this->assertObjectHasAttribute('variables', $data);
$this->assertArrayHasKey('0', $data->variables);
$this->assertObjectHasAttribute('name', $data->variables[0]);
$this->assertObjectNestedValueEquals('name', $variable->name, $data->variables[0]);
}
}