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

122 lines
4.1 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 Exception;
2017-08-27 19:55:25 +00:00
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Pack;
2017-08-22 02:00:14 +00:00
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Database\ConnectionInterface;
2017-08-27 19:55:25 +00:00
use Pterodactyl\Services\Packs\PackDeletionService;
2017-08-22 02:00:14 +00:00
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
use Pterodactyl\Exceptions\Service\HasActiveServersException;
2017-08-27 19:55:25 +00:00
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
2017-08-22 02:00:14 +00:00
class PackDeletionServiceTest extends TestCase
{
/**
* @var \Illuminate\Database\ConnectionInterface
*/
protected $connection;
/**
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $serverRepository;
/**
* @var \Pterodactyl\Services\Packs\PackDeletionService
*/
protected $service;
/**
* @var \Illuminate\Contracts\Filesystem\Factory
*/
protected $storage;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->repository = m::mock(PackRepositoryInterface::class);
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
$this->storage = m::mock(Factory::class);
$this->service = new PackDeletionService(
$this->connection,
$this->storage,
$this->repository,
$this->serverRepository
);
}
/**
* Test that a pack is deleted.
*/
public function testPackIsDeleted()
{
$model = factory(Pack::class)->make();
$this->serverRepository->shouldReceive('findCountWhere')->with([['pack_id', '=', $model->id]])->once()->andReturn(0);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturnNull();
$this->storage->shouldReceive('disk')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('deleteDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($model);
}
/**
* Test that a pack ID can be passed in place of the model.
*/
public function testPackIdCanBePassedInPlaceOfModel()
{
$model = factory(Pack::class)->make();
$this->repository->shouldReceive('withColumns')->with(['id', 'uuid'])->once()->andReturnSelf()
->shouldReceive('find')->with($model->id)->once()->andReturn($model);
$this->serverRepository->shouldReceive('findCountWhere')->with([['pack_id', '=', $model->id]])->once()->andReturn(0);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturnNull();
$this->storage->shouldReceive('disk')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('deleteDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($model->id);
}
/**
* Test that an exception gets thrown if a server is attached to a pack.
*/
public function testExceptionIsThrownIfServerIsAttachedToPack()
{
$model = factory(Pack::class)->make();
$this->serverRepository->shouldReceive('findCountWhere')->with([['pack_id', '=', $model->id]])->once()->andReturn(1);
try {
$this->service->handle($model);
} catch (HasActiveServersException $exception) {
$this->assertEquals(trans('exceptions.packs.delete_has_servers'), $exception->getMessage());
2017-08-22 02:00:14 +00:00
}
}
}