misc_pterodactyl-panel/tests/Unit/Services/Nodes/NodeDeletionServiceTest.php

102 lines
3.2 KiB
PHP
Raw Normal View History

<?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
*/
namespace Tests\Unit\Services\Nodes;
use Mockery as m;
2017-08-05 22:26:30 +00:00
use Tests\TestCase;
use Pterodactyl\Models\Node;
2020-06-25 04:54:56 +00:00
use Pterodactyl\Exceptions\DisplayException;
2017-08-05 22:26:30 +00:00
use Illuminate\Contracts\Translation\Translator;
2017-08-31 02:14:20 +00:00
use Pterodactyl\Services\Nodes\NodeDeletionService;
2017-08-05 22:26:30 +00:00
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class NodeDeletionServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $serverRepository;
/**
* @var \Illuminate\Contracts\Translation\Translator
*/
protected $translator;
/**
* @var \Pterodactyl\Services\Nodes\NodeDeletionService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
$this->repository = m::mock(NodeRepositoryInterface::class);
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
$this->translator = m::mock(Translator::class);
$this->service = new NodeDeletionService(
$this->repository,
$this->serverRepository,
$this->translator
);
}
/**
* Test that a node is deleted if there are no servers attached to it.
*/
public function testNodeIsDeletedIfNoServersAreAttached()
{
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(0);
$this->repository->shouldReceive('delete')->with(1)->once()->andReturn(1);
$this->assertEquals(1, $this->service->handle(1));
}
/**
* Test that an exception is thrown if servers are attached to the node.
*/
public function testExceptionIsThrownIfServersAreAttachedToNode()
{
2020-06-25 04:54:56 +00:00
$this->expectException(DisplayException::class);
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(1);
$this->translator->shouldReceive('trans')->with('exceptions.node.servers_attached')->once()->andReturnNull();
$this->repository->shouldNotReceive('delete');
$this->service->handle(1);
}
/**
* Test that a model can be passed into the handle function rather than an ID.
*/
public function testModelCanBePassedToFunctionInPlaceOfNodeId()
{
$node = factory(Node::class)->make();
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([['node_id', '=', $node->id]])->once()->andReturn(0);
$this->repository->shouldReceive('delete')->with($node->id)->once()->andReturn(1);
$this->assertEquals(1, $this->service->handle($node));
}
}