forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBulkPowerActionCommandTest.php
More file actions
164 lines (138 loc) · 5.05 KB
/
BulkPowerActionCommandTest.php
File metadata and controls
164 lines (138 loc) · 5.05 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Tests\Unit\Commands\Server;
use Mockery as m;
use Pterodactyl\Models\Server;
use Illuminate\Validation\Factory;
use Tests\Unit\Commands\CommandTestCase;
use Pterodactyl\Console\Commands\Server\BulkPowerActionCommand;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
class BulkPowerActionCommandTest extends CommandTestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface|\Mockery\Mock
*/
private $powerRepository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* Setup test.
*/
public function setUp()
{
parent::setUp();
$this->powerRepository = m::mock(PowerRepositoryInterface::class);
$this->repository = m::mock(ServerRepositoryInterface::class);
}
/**
* Test that an action can be sent to all servers.
*/
public function testSendAction()
{
$servers = factory(Server::class)->times(2)->make();
$this->repository->shouldReceive('getServersForPowerActionCount')
->once()
->with([], [])
->andReturn(2);
$this->repository->shouldReceive('getServersForPowerAction')
->once()
->with([], [])
->andReturn($servers);
for ($i = 0; $i < count($servers); $i++) {
$this->powerRepository->shouldReceive('setServer->sendSignal')
->once()
->with('kill')
->andReturnNull();
}
$display = $this->runCommand($this->getCommand(), ['action' => 'kill'], ['yes']);
$this->assertNotEmpty($display);
$this->assertContains('2/2', $display);
$this->assertContains(trans('command/messages.server.power.confirm', ['action' => 'kill', 'count' => 2]), $display);
}
/**
* Test filtering servers and nodes.
*/
public function testSendWithFilters()
{
$server = factory(Server::class)->make();
$this->repository->shouldReceive('getServersForPowerActionCount')
->once()
->with([1, 2], [3, 4])
->andReturn(1);
$this->repository->shouldReceive('getServersForPowerAction')
->once()
->with([1, 2], [3, 4])
->andReturn([$server]);
$this->powerRepository->shouldReceive('setServer->sendSignal')
->once()
->with('kill')
->andReturnNull();
$display = $this->runCommand($this->getCommand(), [
'action' => 'kill',
'--servers' => '1,2',
'--nodes' => '3,4',
], ['yes']);
$this->assertNotEmpty($display);
$this->assertContains('1/1', $display);
$this->assertContains(trans('command/messages.server.power.confirm', ['action' => 'kill', 'count' => 1]), $display);
}
/**
* Test that sending empty options returns the expected results.
*/
public function testSendWithEmptyOptions()
{
$server = factory(Server::class)->make();
$this->repository->shouldReceive('getServersForPowerActionCount')
->once()
->with([], [])
->andReturn(1);
$this->repository->shouldReceive('getServersForPowerAction')->once()->with([], [])->andReturn([$server]);
$this->powerRepository->shouldReceive('setServer->sendSignal')->once()->with('kill')->andReturnNull();
$display = $this->runCommand($this->getCommand(), [
'action' => 'kill',
'--servers' => '',
'--nodes' => '',
], ['yes']);
$this->assertNotEmpty($display);
$this->assertContains('1/1', $display);
$this->assertContains(trans('command/messages.server.power.confirm', ['action' => 'kill', 'count' => 1]), $display);
}
/**
* Test that validation occurrs correctly.
*
* @param array $data
*
* @dataProvider validationFailureDataProvider
* @expectedException \Illuminate\Validation\ValidationException
*/
public function testValidationErrors(array $data)
{
$this->runCommand($this->getCommand(), $data);
}
/**
* Provide invalid data for the command.
*
* @return array
*/
public function validationFailureDataProvider(): array
{
return [
[['action' => 'hodor']],
[['action' => 'hodor', '--servers' => 'hodor']],
[['action' => 'kill', '--servers' => 'hodor']],
[['action' => 'kill', '--servers' => '1,2,3', '--nodes' => 'hodor']],
[['action' => 'kill', '--servers' => '1,2,3', '--nodes' => '1,2,test']],
];
}
/**
* Return an instance of the command with mocked dependencies.
*
* @return \Pterodactyl\Console\Commands\Server\BulkPowerActionCommand
*/
private function getCommand(): BulkPowerActionCommand
{
return new BulkPowerActionCommand($this->powerRepository, $this->repository, $this->app->make(Factory::class));
}
}