misc_pterodactyl-panel/tests/Unit/Services/Eggs/Variables/VariableCreationServiceTest.php

128 lines
4 KiB
PHP
Raw Normal View History

2017-08-13 19:55:09 +00:00
<?php
2017-10-08 04:29:08 +00:00
namespace Tests\Unit\Services\Eggs\Variables;
2017-08-13 19:55:09 +00:00
use Mockery as m;
2017-08-16 04:21:01 +00:00
use Tests\TestCase;
use Pterodactyl\Models\EggVariable;
2017-10-08 04:29:08 +00:00
use Pterodactyl\Services\Eggs\Variables\VariableCreationService;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
2017-08-13 19:55:09 +00:00
class VariableCreationServiceTest extends TestCase
{
/**
2017-10-08 04:29:08 +00:00
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
2017-08-13 19:55:09 +00:00
*/
2017-10-08 04:29:08 +00:00
protected $repository;
2017-08-13 19:55:09 +00:00
/**
2017-10-08 04:29:08 +00:00
* @var \Pterodactyl\Services\Eggs\Variables\VariableCreationService
2017-08-13 19:55:09 +00:00
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
2017-10-08 04:29:08 +00:00
$this->repository = m::mock(EggVariableRepositoryInterface::class);
2017-08-13 19:55:09 +00:00
2017-10-08 04:29:08 +00:00
$this->service = new VariableCreationService($this->repository);
2017-08-13 19:55:09 +00:00
}
/**
* Test basic functionality, data should be stored in the database.
*/
public function testVariableIsCreatedAndStored()
{
2018-02-17 19:37:53 +00:00
$data = ['env_variable' => 'TEST_VAR_123', 'default_value' => 'test'];
$this->repository->shouldReceive('create')->with(m::subset([
2017-10-08 04:29:08 +00:00
'egg_id' => 1,
2018-02-17 19:37:53 +00:00
'default_value' => 'test',
2017-10-08 04:29:08 +00:00
'user_viewable' => false,
'user_editable' => false,
'env_variable' => 'TEST_VAR_123',
2018-02-17 19:37:53 +00:00
]))->once()->andReturn(new EggVariable);
2017-08-13 19:55:09 +00:00
$this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data));
2017-08-13 19:55:09 +00:00
}
/**
* Test that the option key in the data array is properly parsed.
*/
public function testOptionsPassedInArrayKeyAreParsedProperly()
{
$data = ['env_variable' => 'TEST_VAR_123', 'options' => ['user_viewable', 'user_editable']];
2018-02-17 19:37:53 +00:00
$this->repository->shouldReceive('create')->with(m::subset([
'default_value' => '',
2017-08-13 19:55:09 +00:00
'user_viewable' => true,
'user_editable' => true,
'env_variable' => 'TEST_VAR_123',
2018-02-17 19:37:53 +00:00
]))->once()->andReturn(new EggVariable);
2017-08-13 19:55:09 +00:00
$this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data));
2017-08-13 19:55:09 +00:00
}
2017-12-31 01:56:42 +00:00
/**
* Test that an empty (null) value passed in the option key is handled
2018-02-17 19:37:53 +00:00
* properly as an array. Also tests the same case aganist the default_value.
2017-12-31 01:56:42 +00:00
*
* @see https://github.com/Pterodactyl/Panel/issues/841
2018-02-17 19:37:53 +00:00
* @see https://github.com/Pterodactyl/Panel/issues/943
2017-12-31 01:56:42 +00:00
*/
public function testNullOptionValueIsPassedAsArray()
{
2018-02-17 19:37:53 +00:00
$data = ['env_variable' => 'TEST_VAR_123', 'options' => null, 'default_value' => null];
$this->repository->shouldReceive('create')->with(m::subset([
'default_value' => '',
2017-12-31 01:56:42 +00:00
'user_viewable' => false,
'user_editable' => false,
2018-02-17 19:37:53 +00:00
]))->once()->andReturn(new EggVariable);
2017-12-31 01:56:42 +00:00
$this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data));
}
2017-08-13 19:55:09 +00:00
/**
* Test that all of the reserved variables defined in the model trigger an exception.
*
* @dataProvider reservedNamesProvider
2017-10-08 04:29:08 +00:00
* @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
2017-08-13 19:55:09 +00:00
*/
2017-10-08 04:29:08 +00:00
public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames(string $variable)
2017-08-13 19:55:09 +00:00
{
$this->service->handle(1, ['env_variable' => $variable]);
}
/**
2017-10-08 04:29:08 +00:00
* Test that the egg ID applied in the function takes higher priority than an
* ID passed into the handler.
2017-08-13 19:55:09 +00:00
*/
2017-10-08 04:29:08 +00:00
public function testEggIdPassedInDataIsNotApplied()
2017-08-13 19:55:09 +00:00
{
2017-10-08 04:29:08 +00:00
$data = ['egg_id' => 123456, 'env_variable' => 'TEST_VAR_123'];
2018-02-17 19:37:53 +00:00
$this->repository->shouldReceive('create')->with(m::subset([
2017-10-08 04:29:08 +00:00
'egg_id' => 1,
2018-02-17 19:37:53 +00:00
]))->once()->andReturn(new EggVariable);
2017-08-13 19:55:09 +00:00
2017-10-08 04:29:08 +00:00
$this->assertInstanceOf(EggVariable::class, $this->service->handle(1, $data));
2017-08-13 19:55:09 +00:00
}
/**
* Provides the data to be used in the tests.
*
* @return array
*/
public function reservedNamesProvider()
{
$data = [];
$exploded = explode(',', EggVariable::RESERVED_ENV_NAMES);
2017-08-13 19:55:09 +00:00
foreach ($exploded as $e) {
$data[] = [$e];
}
return $data;
}
}