misc_pterodactyl-panel/tests/Unit/Services/Services/Variables/VariableUpdateServiceTest.php

152 lines
5.2 KiB
PHP
Raw Normal View History

2017-08-13 14:55:09 -05:00
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-25 21:43:01 -05:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
2017-08-13 14:55:09 -05:00
*/
namespace Tests\Unit\Services\Services\Variables;
use Exception;
use Mockery as m;
2017-08-15 23:21:01 -05:00
use Tests\TestCase;
2017-08-13 14:55:09 -05:00
use Pterodactyl\Models\ServiceVariable;
2017-08-15 23:21:01 -05:00
use Pterodactyl\Exceptions\DisplayException;
2017-08-13 14:55:09 -05:00
use Pterodactyl\Services\Services\Variables\VariableUpdateService;
use Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface;
class VariableUpdateServiceTest extends TestCase
{
/**
2017-09-26 22:16:26 -05:00
* @var \Pterodactyl\Models\ServiceVariable|\Mockery\Mock
2017-08-13 14:55:09 -05:00
*/
protected $model;
/**
2017-09-26 22:16:26 -05:00
* @var \Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface|\Mockery\Mock
2017-08-13 14:55:09 -05:00
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Services\Variables\VariableUpdateService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->model = factory(ServiceVariable::class)->make();
$this->repository = m::mock(ServiceVariableRepositoryInterface::class);
$this->service = new VariableUpdateService($this->repository);
}
/**
* Test the function when no env_variable key is passed into the function.
*/
public function testVariableIsUpdatedWhenNoEnvironmentVariableIsPassed()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, [
'user_viewable' => false,
'user_editable' => false,
'test-data' => 'test-value',
])->once()->andReturn(true);
$this->assertTrue($this->service->handle($this->model, ['test-data' => 'test-value']));
}
2017-09-26 22:16:26 -05:00
/**
* Test that a service variable ID can be passed in place of the model.
*/
public function testVariableIdCanBePassedInPlaceOfModel()
{
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, [
'user_viewable' => false,
'user_editable' => false,
'test-data' => 'test-value',
])->once()->andReturn(true);
$this->assertTrue($this->service->handle($this->model->id, ['test-data' => 'test-value']));
}
2017-08-13 14:55:09 -05:00
/**
* Test the function when a valid env_variable key is passed into the function.
*/
public function testVariableIsUpdatedWhenValidEnvironmentVariableIsPassed()
{
$this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'],
['option_id', '=', $this->model->option_id],
2017-08-15 23:21:01 -05:00
['id', '!=', $this->model->id],
2017-08-13 14:55:09 -05:00
])->once()->andReturn(0);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, [
'user_viewable' => false,
'user_editable' => false,
'env_variable' => 'TEST_VAR_123',
])->once()->andReturn(true);
$this->assertTrue($this->service->handle($this->model, ['env_variable' => 'TEST_VAR_123']));
}
/**
* Test that a non-unique environment variable triggers an exception.
*/
public function testExceptionIsThrownIfEnvironmentVariableIsNotUnique()
{
$this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'],
['option_id', '=', $this->model->option_id],
2017-08-15 23:21:01 -05:00
['id', '!=', $this->model->id],
2017-08-13 14:55:09 -05:00
])->once()->andReturn(1);
try {
$this->service->handle($this->model, ['env_variable' => 'TEST_VAR_123']);
} catch (Exception $exception) {
$this->assertInstanceOf(DisplayException::class, $exception);
$this->assertEquals(trans('exceptions.service.variables.env_not_unique', [
2017-08-13 14:55:09 -05:00
'name' => 'TEST_VAR_123',
]), $exception->getMessage());
}
}
/**
* Test that all of the reserved variables defined in the model trigger an exception.
*
* @dataProvider reservedNamesProvider
* @expectedException \Pterodactyl\Exceptions\Service\ServiceVariable\ReservedVariableNameException
2017-08-13 14:55:09 -05:00
*/
public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames($variable)
{
$this->service->handle($this->model, ['env_variable' => $variable]);
}
/**
* Provides the data to be used in the tests.
*
* @return array
*/
public function reservedNamesProvider()
{
$data = [];
$exploded = explode(',', ServiceVariable::RESERVED_ENV_NAMES);
foreach ($exploded as $e) {
$data[] = [$e];
}
return $data;
}
}