forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackDeletionServiceTest.php
More file actions
120 lines (101 loc) · 4.08 KB
/
PackDeletionServiceTest.php
File metadata and controls
120 lines (101 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Packs;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Pack;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Packs\PackDeletionService;
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
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(): void
{
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()->andReturn(1);
$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('setColumns')->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()->andReturn(1);
$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());
}
}
}