misc_pterodactyl-panel/tests/Unit/Services/Servers/ServerConfigurationStructureServiceTest.php

109 lines
3.7 KiB
PHP
Raw Normal View History

2017-10-27 01:23:43 +00:00
<?php
namespace Tests\Unit\Services\Servers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Services\Servers\EnvironmentService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
class ServerConfigurationStructureServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Services\Servers\EnvironmentService|\Mockery\Mock
*/
private $environment;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* Setup tests.
*/
public function setUp(): void
2017-10-27 01:23:43 +00:00
{
parent::setUp();
$this->environment = m::mock(EnvironmentService::class);
$this->repository = m::mock(ServerRepositoryInterface::class);
}
/**
* Test that a configuration is returned in the proper format when passed a
* server model that is missing required relationships.
*/
public function testCorrectStructureIsReturned()
{
2020-06-25 05:18:48 +00:00
/** @var \Pterodactyl\Models\Server $model */
2017-10-27 01:23:43 +00:00
$model = factory(Server::class)->make();
2017-11-04 17:49:05 +00:00
$model->setRelation('allocation', factory(Allocation::class)->make());
$model->setRelation('allocations', collect(factory(Allocation::class)->times(2)->make()));
$model->setRelation('egg', factory(Egg::class)->make());
2017-10-27 01:23:43 +00:00
2020-06-25 05:18:48 +00:00
$this->environment->expects('handle')->with($model)->andReturn(['environment_array']);
2017-10-27 01:23:43 +00:00
$response = $this->getService()->handle($model);
$this->assertNotEmpty($response);
$this->assertArrayNotHasKey('user', $response);
$this->assertArrayNotHasKey('keys', $response);
2020-06-25 05:18:48 +00:00
2017-10-27 01:23:43 +00:00
$this->assertArrayHasKey('uuid', $response);
2020-06-25 05:18:48 +00:00
$this->assertArrayHasKey('suspended', $response);
$this->assertArrayHasKey('environment', $response);
$this->assertArrayHasKey('invocation', $response);
2017-10-27 01:23:43 +00:00
$this->assertArrayHasKey('build', $response);
$this->assertArrayHasKey('service', $response);
2020-06-25 05:18:48 +00:00
$this->assertArrayHasKey('container', $response);
$this->assertArrayHasKey('allocations', $response);
2017-10-27 01:23:43 +00:00
2020-06-25 05:18:48 +00:00
$this->assertSame([
2017-10-27 01:23:43 +00:00
'default' => [
'ip' => $model->allocation->ip,
'port' => $model->allocation->port,
],
2020-06-25 05:18:48 +00:00
'mappings' => $model->getAllocationMappings(),
], $response['allocations']);
$this->assertSame([
'memory_limit' => $model->memory,
'swap' => $model->swap,
'io_weight' => $model->io,
'cpu_limit' => $model->cpu,
'threads' => $model->threads,
'disk_space' => $model->disk,
], $response['build']);
2017-10-27 01:23:43 +00:00
2020-06-25 05:18:48 +00:00
$this->assertSame([
2017-10-27 01:23:43 +00:00
'egg' => $model->egg->uuid,
'skip_scripts' => $model->skip_scripts,
], $response['service']);
2020-06-25 05:18:48 +00:00
$this->assertSame([
'image' => $model->image,
'oom_disabled' => $model->oom_disabled,
'requires_rebuild' => false,
], $response['container']);
$this->assertSame($model->uuid, $response['uuid']);
$this->assertSame((bool) $model->suspended, $response['suspended']);
$this->assertSame(['environment_array'], $response['environment']);
$this->assertSame($model->startup, $response['invocation']);
2017-10-27 01:23:43 +00:00
}
/**
* Return an instance of the service with mocked dependencies.
*
* @return \Pterodactyl\Services\Servers\ServerConfigurationStructureService
*/
private function getService(): ServerConfigurationStructureService
{
return new ServerConfigurationStructureService($this->repository, $this->environment);
}
}