misc_pterodactyl-panel/tests/Unit/Services/Eggs/Scripts/InstallScriptServiceTest.php

114 lines
3.5 KiB
PHP
Raw Normal View History

2017-08-12 21:30:27 +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-12 21:30:27 +00:00
*/
namespace Tests\Unit\Services\Services\Options;
use Exception;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Egg;
2017-10-08 04:29:08 +00:00
use Pterodactyl\Services\Eggs\Scripts\InstallScriptService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
2017-10-08 04:29:08 +00:00
use Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException;
2017-08-12 21:30:27 +00:00
2017-10-08 04:29:08 +00:00
class InstallScriptServiceTest extends TestCase
2017-08-12 21:30:27 +00:00
{
/**
* @var array
*/
protected $data = [
'script_install' => 'test-script',
'script_is_privileged' => true,
'script_entry' => '/bin/bash',
'script_container' => 'ubuntu',
'copy_script_from' => null,
];
/**
* @var \Pterodactyl\Models\Egg
2017-08-12 21:30:27 +00:00
*/
protected $model;
/**
2017-10-08 04:29:08 +00:00
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
2017-08-12 21:30:27 +00:00
*/
protected $repository;
/**
2017-10-08 04:29:08 +00:00
* @var \Pterodactyl\Services\Eggs\Scripts\InstallScriptService
2017-08-12 21:30:27 +00:00
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->model = factory(Egg::class)->make();
$this->repository = m::mock(EggRepositoryInterface::class);
2017-08-12 21:30:27 +00:00
$this->service = new InstallScriptService($this->repository);
2017-08-12 21:30:27 +00:00
}
/**
* Test that passing a new copy_script_from attribute works properly.
*/
public function testUpdateWithValidCopyScriptFromAttribute()
{
$this->data['copy_script_from'] = 1;
2017-10-08 04:29:08 +00:00
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(true);
2018-01-05 22:33:50 +00:00
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
2017-08-12 21:30:27 +00:00
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
$this->service->handle($this->model, $this->data);
}
/**
* Test that an exception gets raised when the script is not copiable.
*/
public function testUpdateWithInvalidCopyScriptFromAttribute()
{
$this->data['copy_script_from'] = 1;
2017-10-08 04:29:08 +00:00
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(false);
2017-08-12 21:30:27 +00:00
try {
$this->service->handle($this->model, $this->data);
} catch (Exception $exception) {
$this->assertInstanceOf(InvalidCopyFromException::class, $exception);
2017-10-08 04:29:08 +00:00
$this->assertEquals(trans('exceptions.nest.egg.invalid_copy_id'), $exception->getMessage());
2017-08-12 21:30:27 +00:00
}
}
/**
* Test standard functionality.
*/
public function testUpdateWithoutNewCopyScriptFromAttribute()
{
2018-01-05 22:33:50 +00:00
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
2017-08-12 21:30:27 +00:00
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
$this->service->handle($this->model, $this->data);
}
/**
* Test that an integer can be passed in place of a model.
*/
public function testFunctionAcceptsIntegerInPlaceOfModel()
{
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
2018-01-05 22:33:50 +00:00
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
2017-08-12 21:30:27 +00:00
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
$this->service->handle($this->model->id, $this->data);
}
}