Skip to content

Commit 0135f7e

Browse files
committed
Add test for new command
1 parent 021710a commit 0135f7e

File tree

2 files changed

+167
-1
lines changed

2 files changed

+167
-1
lines changed

app/Console/Commands/Server/BulkPowerActionCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use GuzzleHttp\Exception\RequestException;
7+
use Illuminate\Validation\ValidationException;
78
use Illuminate\Validation\Factory as ValidatorFactory;
89
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
910
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
@@ -60,6 +61,7 @@ public function __construct(
6061
/**
6162
* Handle the bulk power request.
6263
*
64+
* @throws \Illuminate\Validation\ValidationException
6365
* @throws \Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException
6466
*/
6567
public function handle()
@@ -85,7 +87,7 @@ public function handle()
8587
$this->output->error($message);
8688
}
8789

88-
return;
90+
throw new ValidationException($validator);
8991
}
9092

9193
$count = $this->repository->getServersForPowerActionCount($servers, $nodes);
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
namespace Tests\Unit\Commands\Server;
4+
5+
use Mockery as m;
6+
use Pterodactyl\Models\Server;
7+
use Illuminate\Validation\Factory;
8+
use Tests\Unit\Commands\CommandTestCase;
9+
use Pterodactyl\Console\Commands\Server\BulkPowerActionCommand;
10+
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
11+
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
12+
13+
class BulkPowerActionCommandTest extends CommandTestCase
14+
{
15+
/**
16+
* @var \Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface|\Mockery\Mock
17+
*/
18+
private $powerRepository;
19+
20+
/**
21+
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
22+
*/
23+
private $repository;
24+
25+
/**
26+
* Setup test.
27+
*/
28+
public function setUp()
29+
{
30+
parent::setUp();
31+
32+
$this->powerRepository = m::mock(PowerRepositoryInterface::class);
33+
$this->repository = m::mock(ServerRepositoryInterface::class);
34+
}
35+
36+
/**
37+
* Test that an action can be sent to all servers.
38+
*/
39+
public function testSendAction()
40+
{
41+
$servers = factory(Server::class)->times(2)->make();
42+
43+
$this->repository->shouldReceive('getServersForPowerActionCount')
44+
->once()
45+
->with([], [])
46+
->andReturn(2);
47+
48+
$this->repository->shouldReceive('getServersForPowerAction')
49+
->once()
50+
->with([], [])
51+
->andReturn($servers);
52+
53+
for ($i = 0; $i < count($servers); $i++) {
54+
$this->powerRepository->shouldReceive('setServer->sendSignal')
55+
->once()
56+
->with('kill')
57+
->andReturnNull();
58+
}
59+
60+
$display = $this->runCommand($this->getCommand(), ['action' => 'kill'], ['yes']);
61+
62+
$this->assertNotEmpty($display);
63+
$this->assertContains('2/2', $display);
64+
$this->assertContains(trans('command/messages.server.power.confirm', ['action' => 'kill', 'count' => 2]), $display);
65+
}
66+
67+
/**
68+
* Test filtering servers and nodes.
69+
*/
70+
public function testSendWithFilters()
71+
{
72+
$server = factory(Server::class)->make();
73+
74+
$this->repository->shouldReceive('getServersForPowerActionCount')
75+
->once()
76+
->with([1, 2], [3, 4])
77+
->andReturn(1);
78+
79+
$this->repository->shouldReceive('getServersForPowerAction')
80+
->once()
81+
->with([1, 2], [3, 4])
82+
->andReturn([$server]);
83+
84+
$this->powerRepository->shouldReceive('setServer->sendSignal')
85+
->once()
86+
->with('kill')
87+
->andReturnNull();
88+
89+
$display = $this->runCommand($this->getCommand(), [
90+
'action' => 'kill',
91+
'--servers' => '1,2',
92+
'--nodes' => '3,4',
93+
], ['yes']);
94+
95+
$this->assertNotEmpty($display);
96+
$this->assertContains('1/1', $display);
97+
$this->assertContains(trans('command/messages.server.power.confirm', ['action' => 'kill', 'count' => 1]), $display);
98+
}
99+
100+
/**
101+
* Test that sending empty options returns the expected results.
102+
*/
103+
public function testSendWithEmptyOptions()
104+
{
105+
$server = factory(Server::class)->make();
106+
107+
$this->repository->shouldReceive('getServersForPowerActionCount')
108+
->once()
109+
->with([], [])
110+
->andReturn(1);
111+
112+
$this->repository->shouldReceive('getServersForPowerAction')->once()->with([], [])->andReturn([$server]);
113+
$this->powerRepository->shouldReceive('setServer->sendSignal')->once()->with('kill')->andReturnNull();
114+
115+
$display = $this->runCommand($this->getCommand(), [
116+
'action' => 'kill',
117+
'--servers' => '',
118+
'--nodes' => '',
119+
], ['yes']);
120+
121+
$this->assertNotEmpty($display);
122+
$this->assertContains('1/1', $display);
123+
$this->assertContains(trans('command/messages.server.power.confirm', ['action' => 'kill', 'count' => 1]), $display);
124+
}
125+
126+
/**
127+
* Test that validation occurrs correctly.
128+
*
129+
* @param array $data
130+
*
131+
* @dataProvider validationFailureDataProvider
132+
* @expectedException \Illuminate\Validation\ValidationException
133+
*/
134+
public function testValidationErrors(array $data)
135+
{
136+
$this->runCommand($this->getCommand(), $data);
137+
}
138+
139+
/**
140+
* Provide invalid data for the command.
141+
*
142+
* @return array
143+
*/
144+
public function validationFailureDataProvider(): array
145+
{
146+
return [
147+
[['action' => 'hodor']],
148+
[['action' => 'hodor', '--servers' => 'hodor']],
149+
[['action' => 'kill', '--servers' => 'hodor']],
150+
[['action' => 'kill', '--servers' => '1,2,3', '--nodes' => 'hodor']],
151+
[['action' => 'kill', '--servers' => '1,2,3', '--nodes' => '1,2,test']],
152+
];
153+
}
154+
155+
/**
156+
* Return an instance of the command with mocked dependencies.
157+
*
158+
* @return \Pterodactyl\Console\Commands\Server\BulkPowerActionCommand
159+
*/
160+
private function getCommand(): BulkPowerActionCommand
161+
{
162+
return new BulkPowerActionCommand($this->powerRepository, $this->repository, $this->app->make(Factory::class));
163+
}
164+
}

0 commit comments

Comments
 (0)