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

120 lines
3.6 KiB
PHP
Raw Normal View History

2017-08-13 19:55:09 +00:00
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
2017-08-13 19:55:09 +00:00
*/
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\Egg;
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()
{
$data = ['env_variable' => 'TEST_VAR_123'];
2017-10-08 04:29:08 +00:00
$this->repository->shouldReceive('create')->with([
'egg_id' => 1,
'user_viewable' => false,
'user_editable' => false,
'env_variable' => 'TEST_VAR_123',
])->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']];
2017-10-08 04:29:08 +00:00
$this->repository->shouldReceive('create')->with([
'egg_id' => 1,
2017-08-13 19:55:09 +00:00
'user_viewable' => true,
'user_editable' => true,
'env_variable' => 'TEST_VAR_123',
'options' => ['user_viewable', 'user_editable'],
])->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 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'];
$this->repository->shouldReceive('create')->with([
'egg_id' => 1,
2017-08-13 19:55:09 +00:00
'user_viewable' => false,
'user_editable' => false,
'env_variable' => 'TEST_VAR_123',
])->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;
}
}