misc_pterodactyl-panel/tests/Unit/Services/Packs/PackUpdateServiceTest.php

100 lines
3.2 KiB
PHP
Raw Normal View History

2017-08-22 02:00:14 +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-22 02:00:14 +00:00
*/
namespace Tests\Unit\Services\Packs;
use Mockery as m;
2017-08-27 19:55:25 +00:00
use Tests\TestCase;
2017-08-22 02:00:14 +00:00
use Pterodactyl\Models\Pack;
use Pterodactyl\Services\Packs\PackUpdateService;
2017-08-27 19:55:25 +00:00
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
2017-08-22 02:00:14 +00:00
class PackUpdateServiceTest extends TestCase
{
/**
2017-10-08 20:44:28 +00:00
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface|\Mockery\Mock
2017-08-22 02:00:14 +00:00
*/
protected $repository;
/**
2017-10-08 20:44:28 +00:00
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
2017-08-22 02:00:14 +00:00
*/
protected $serverRepository;
/**
* @var \Pterodactyl\Services\Packs\PackUpdateService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->repository = m::mock(PackRepositoryInterface::class);
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
$this->service = new PackUpdateService($this->repository, $this->serverRepository);
}
/**
* Test that a pack is updated.
*/
public function testPackIsUpdated()
{
$model = factory(Pack::class)->make();
2018-01-05 22:33:50 +00:00
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
2017-08-22 02:00:14 +00:00
'locked' => false,
'visible' => false,
'selectable' => false,
2017-08-22 03:10:48 +00:00
'test-data' => 'value',
2017-08-22 02:00:14 +00:00
])->once()->andReturn(1);
$this->assertEquals(1, $this->service->handle($model, ['test-data' => 'value']));
}
/**
* Test that an exception is thrown if the pack option ID is changed while servers are using the pack.
*/
2017-10-08 20:44:28 +00:00
public function testExceptionIsThrownIfModifyingEggIdWhenServersAreAttached()
2017-08-22 02:00:14 +00:00
{
$model = factory(Pack::class)->make();
$this->serverRepository->shouldReceive('findCountWhere')->with([['pack_id', '=', $model->id]])->once()->andReturn(1);
try {
2017-10-08 20:44:28 +00:00
$this->service->handle($model, ['egg_id' => 0]);
2017-08-22 02:00:14 +00:00
} catch (HasActiveServersException $exception) {
$this->assertEquals(trans('exceptions.packs.update_has_servers'), $exception->getMessage());
2017-08-22 02:00:14 +00:00
}
}
/**
* Test that an ID for a pack can be passed in place of the model.
*/
public function testPackIdCanBePassedInPlaceOfModel()
{
$model = factory(Pack::class)->make();
$this->repository->shouldReceive('setColumns')->with(['id', 'egg_id'])->once()->andReturnSelf()
2017-08-22 02:00:14 +00:00
->shouldReceive('find')->with($model->id)->once()->andReturn($model);
2018-01-05 22:33:50 +00:00
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
2017-08-22 02:00:14 +00:00
'locked' => false,
'visible' => false,
'selectable' => false,
2017-08-22 03:10:48 +00:00
'test-data' => 'value',
2017-08-22 02:00:14 +00:00
])->once()->andReturn(1);
$this->assertEquals(1, $this->service->handle($model->id, ['test-data' => 'value']));
}
}