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 Mockery as m;
|
2017-08-16 04:21:01 +00:00
|
|
|
use Tests\TestCase;
|
2017-10-08 20:29:46 +00:00
|
|
|
use Pterodactyl\Exceptions\PterodactylException;
|
|
|
|
use Pterodactyl\Services\Eggs\EggDeletionService;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
2017-10-08 20:29:46 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\Egg\HasChildrenException;
|
2017-08-19 03:19:06 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\HasActiveServersException;
|
2017-08-16 04:21:01 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2017-08-12 21:30:27 +00:00
|
|
|
|
2017-10-08 20:29:46 +00:00
|
|
|
class EggDeletionServiceTest extends TestCase
|
2017-08-12 21:30:27 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-10-07 04:57:53 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
|
2017-08-12 21:30:27 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
2017-09-30 17:54:09 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
2017-08-12 21:30:27 +00:00
|
|
|
*/
|
|
|
|
protected $serverRepository;
|
|
|
|
|
|
|
|
/**
|
2017-10-08 20:29:46 +00:00
|
|
|
* @var \Pterodactyl\Services\Eggs\EggDeletionService
|
2017-08-12 21:30:27 +00:00
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
2017-09-30 17:54:09 +00:00
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2017-08-12 21:30:27 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-10-07 04:57:53 +00:00
|
|
|
$this->repository = m::mock(EggRepositoryInterface::class);
|
2017-08-12 21:30:27 +00:00
|
|
|
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
|
|
|
|
|
2017-10-07 21:16:51 +00:00
|
|
|
$this->service = new EggDeletionService($this->serverRepository, $this->repository);
|
2017-08-12 21:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-08 20:29:46 +00:00
|
|
|
* Test that Egg is deleted if no servers are found.
|
2017-08-12 21:30:27 +00:00
|
|
|
*/
|
2017-10-08 20:29:46 +00:00
|
|
|
public function testEggIsDeletedIfNoServersAreFound()
|
2017-08-12 21:30:27 +00:00
|
|
|
{
|
2017-10-08 20:29:46 +00:00
|
|
|
$this->serverRepository->shouldReceive('findCountWhere')->with([['egg_id', '=', 1]])->once()->andReturn(0);
|
2017-09-30 17:54:09 +00:00
|
|
|
$this->repository->shouldReceive('findCountWhere')->with([['config_from', '=', 1]])->once()->andReturn(0);
|
2017-08-12 21:30:27 +00:00
|
|
|
$this->repository->shouldReceive('delete')->with(1)->once()->andReturn(1);
|
|
|
|
|
|
|
|
$this->assertEquals(1, $this->service->handle(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-08 20:29:46 +00:00
|
|
|
* Test that Egg is not deleted if servers are found.
|
2017-08-12 21:30:27 +00:00
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfServersAreFound()
|
|
|
|
{
|
2017-10-08 20:29:46 +00:00
|
|
|
$this->serverRepository->shouldReceive('findCountWhere')->with([['egg_id', '=', 1]])->once()->andReturn(1);
|
2017-08-12 21:30:27 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->service->handle(1);
|
2017-10-08 20:29:46 +00:00
|
|
|
} catch (PterodactylException $exception) {
|
2017-08-12 21:30:27 +00:00
|
|
|
$this->assertInstanceOf(HasActiveServersException::class, $exception);
|
2017-10-08 20:29:46 +00:00
|
|
|
$this->assertEquals(trans('exceptions.nest.egg.delete_has_servers'), $exception->getMessage());
|
2017-08-12 21:30:27 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-30 17:54:09 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-08 20:29:46 +00:00
|
|
|
* Test that an exception is thrown if children Eggs exist.
|
2017-09-30 17:54:09 +00:00
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfChildrenArePresent()
|
|
|
|
{
|
2017-10-08 20:29:46 +00:00
|
|
|
$this->serverRepository->shouldReceive('findCountWhere')->with([['egg_id', '=', 1]])->once()->andReturn(0);
|
2017-09-30 17:54:09 +00:00
|
|
|
$this->repository->shouldReceive('findCountWhere')->with([['config_from', '=', 1]])->once()->andReturn(1);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->service->handle(1);
|
2017-10-08 20:29:46 +00:00
|
|
|
} catch (PterodactylException $exception) {
|
2017-09-30 17:54:09 +00:00
|
|
|
$this->assertInstanceOf(HasChildrenException::class, $exception);
|
2017-10-08 20:29:46 +00:00
|
|
|
$this->assertEquals(trans('exceptions.nest.egg.has_children'), $exception->getMessage());
|
2017-09-30 17:54:09 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-12 21:30:27 +00:00
|
|
|
}
|