Skip to content

Commit c46f2cb

Browse files
committed
More command tests
1 parent 12ba96b commit c46f2cb

File tree

3 files changed

+266
-6
lines changed

3 files changed

+266
-6
lines changed

app/Console/Commands/Location/DeleteLocationCommand.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ class DeleteLocationCommand extends Command
4848
/**
4949
* @var string
5050
*/
51-
protected $signature = 'p:location:delete
52-
{--minimal : Passing this flag will hide the list of current locations.}
53-
{--short= : The short code of the location to delete.}';
51+
protected $signature = 'p:location:delete {--short= : The short code of the location to delete.}';
5452

5553
/**
5654
* DeleteLocationCommand constructor.
@@ -76,22 +74,22 @@ public function __construct(
7674
*/
7775
public function handle()
7876
{
79-
$this->locations = $this->locations ?? $this->repository->getAllWithNodes();
77+
$this->locations = $this->locations ?? $this->repository->all();
8078
$short = $this->option('short') ?? $this->anticipate(
8179
trans('command/messages.location.ask_short'), $this->locations->pluck('short')->toArray()
8280
);
8381

8482
$location = $this->locations->where('short', $short)->first();
8583
if (is_null($location)) {
8684
$this->error(trans('command/messages.location.no_location_found'));
87-
if (is_null($this->option('short')) && ! $this->option('no-interaction')) {
85+
if ($this->input->isInteractive()) {
8886
$this->handle();
8987
}
9088

9189
return;
9290
}
9391

94-
$this->line(trans('command/messages.location.deleted'));
9592
$this->deletionService->handle($location->id);
93+
$this->line(trans('command/messages.location.deleted'));
9694
}
9795
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Tests\Unit\Commands\Location;
26+
27+
use Mockery as m;
28+
use Tests\TestCase;
29+
use Pterodactyl\Models\Location;
30+
use Symfony\Component\Console\Tester\CommandTester;
31+
use Pterodactyl\Services\Locations\LocationDeletionService;
32+
use Pterodactyl\Console\Commands\Location\DeleteLocationCommand;
33+
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
34+
35+
class DeleteLocationCommandTest extends TestCase
36+
{
37+
/**
38+
* @var \Pterodactyl\Console\Commands\Location\DeleteLocationCommand
39+
*/
40+
protected $command;
41+
42+
/**
43+
* @var \Pterodactyl\Services\Locations\LocationDeletionService
44+
*/
45+
protected $deletionService;
46+
47+
/**
48+
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
49+
*/
50+
protected $repository;
51+
52+
/**
53+
* Setup tests.
54+
*/
55+
public function setUp()
56+
{
57+
parent::setUp();
58+
59+
$this->deletionService = m::mock(LocationDeletionService::class);
60+
$this->repository = m::mock(LocationRepositoryInterface::class);
61+
62+
$this->command = new DeleteLocationCommand($this->deletionService, $this->repository);
63+
$this->command->setLaravel($this->app);
64+
}
65+
66+
/**
67+
* Test that a location can be deleted.
68+
*/
69+
public function testLocationIsDeleted()
70+
{
71+
$locations = collect([
72+
$location1 = factory(Location::class)->make(),
73+
$location2 = factory(Location::class)->make(),
74+
]);
75+
76+
$this->repository->shouldReceive('all')->withNoArgs()->once()->andReturn($locations);
77+
$this->deletionService->shouldReceive('handle')->with($location2->id)->once()->andReturnNull();
78+
79+
$response = new CommandTester($this->command);
80+
$response->setInputs([$location2->short]);
81+
$response->execute([]);
82+
83+
$display = $response->getDisplay();
84+
$this->assertNotEmpty($display);
85+
$this->assertContains(trans('command/messages.location.deleted'), $display);
86+
}
87+
88+
/**
89+
* Test that a location is deleted if passed in as an option.
90+
*/
91+
public function testLocationIsDeletedIfPassedInOption()
92+
{
93+
$locations = collect([
94+
$location1 = factory(Location::class)->make(),
95+
$location2 = factory(Location::class)->make(),
96+
]);
97+
98+
$this->repository->shouldReceive('all')->withNoArgs()->once()->andReturn($locations);
99+
$this->deletionService->shouldReceive('handle')->with($location2->id)->once()->andReturnNull();
100+
101+
$response = new CommandTester($this->command);
102+
$response->execute([
103+
'--short' => $location2->short,
104+
]);
105+
106+
$display = $response->getDisplay();
107+
$this->assertNotEmpty($display);
108+
$this->assertContains(trans('command/messages.location.deleted'), $display);
109+
}
110+
111+
/**
112+
* Test that prompt shows back up if the user enters the wrong parameters.
113+
*/
114+
public function testInteractiveEnvironmentAllowsReAttemptingSearch()
115+
{
116+
$locations = collect([
117+
$location1 = factory(Location::class)->make(),
118+
$location2 = factory(Location::class)->make(),
119+
]);
120+
121+
$this->repository->shouldReceive('all')->withNoArgs()->once()->andReturn($locations);
122+
$this->deletionService->shouldReceive('handle')->with($location2->id)->once()->andReturnNull();
123+
124+
$response = new CommandTester($this->command);
125+
$response->setInputs(['123_not_exist', 'another_not_exist', $location2->short]);
126+
$response->execute([]);
127+
128+
$display = $response->getDisplay();
129+
$this->assertNotEmpty($display);
130+
$this->assertContains(trans('command/messages.location.no_location_found'), $display);
131+
$this->assertContains(trans('command/messages.location.deleted'), $display);
132+
}
133+
134+
/**
135+
* Test that no re-attempt is performed in a non-interactive environment.
136+
*/
137+
public function testNonInteractiveEnvironmentThrowsErrorIfNoLocationIsFound()
138+
{
139+
$locations = collect([
140+
$location1 = factory(Location::class)->make(),
141+
$location2 = factory(Location::class)->make(),
142+
]);
143+
144+
$this->repository->shouldReceive('all')->withNoArgs()->once()->andReturn($locations);
145+
$this->deletionService->shouldNotReceive('handle');
146+
147+
$response = new CommandTester($this->command);
148+
$response->execute(['--short' => 'randomTestString'], ['interactive' => false]);
149+
150+
$display = $response->getDisplay();
151+
$this->assertNotEmpty($display);
152+
$this->assertContains(trans('command/messages.location.no_location_found'), $display);
153+
}
154+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Tests\Unit\Commands\Location;
26+
27+
use Mockery as m;
28+
use Tests\TestCase;
29+
use Pterodactyl\Models\Location;
30+
use Symfony\Component\Console\Tester\CommandTester;
31+
use Pterodactyl\Services\Locations\LocationCreationService;
32+
use Pterodactyl\Console\Commands\Location\MakeLocationCommand;
33+
34+
class MakeLocationCommandTest extends TestCase
35+
{
36+
/**
37+
* @var \Pterodactyl\Console\Commands\Location\MakeLocationCommand
38+
*/
39+
protected $command;
40+
41+
/**
42+
* @var \Pterodactyl\Services\Locations\LocationCreationService
43+
*/
44+
protected $creationService;
45+
46+
/**
47+
* Setup tests.
48+
*/
49+
public function setUp()
50+
{
51+
parent::setUp();
52+
53+
$this->creationService = m::mock(LocationCreationService::class);
54+
55+
$this->command = new MakeLocationCommand($this->creationService);
56+
$this->command->setLaravel($this->app);
57+
}
58+
59+
/**
60+
* Test that a location can be created when no options are passed.
61+
*/
62+
public function testLocationIsCreatedWithNoOptionsPassed()
63+
{
64+
$location = factory(Location::class)->make();
65+
66+
$this->creationService->shouldReceive('handle')->with([
67+
'short' => $location->short,
68+
'long' => $location->long,
69+
])->once()->andReturn($location);
70+
71+
$response = new CommandTester($this->command);
72+
$response->setInputs([$location->short, $location->long]);
73+
$response->execute([]);
74+
75+
$display = $response->getDisplay();
76+
$this->assertNotEmpty($display);
77+
$this->assertContains(trans('command/messages.location.created', [
78+
'name' => $location->short,
79+
'id' => $location->id,
80+
]), $display);
81+
}
82+
83+
/**
84+
* Test that a location is created when options are passed.
85+
*/
86+
public function testLocationIsCreatedWhenOptionsArePassed()
87+
{
88+
$location = factory(Location::class)->make();
89+
90+
$this->creationService->shouldReceive('handle')->with([
91+
'short' => $location->short,
92+
'long' => $location->long,
93+
])->once()->andReturn($location);
94+
95+
$response = new CommandTester($this->command);
96+
$response->execute([
97+
'--short' => $location->short,
98+
'--long' => $location->long,
99+
]);
100+
101+
$display = $response->getDisplay();
102+
$this->assertNotEmpty($display);
103+
$this->assertContains(trans('command/messages.location.created', [
104+
'name' => $location->short,
105+
'id' => $location->id,
106+
]), $display);
107+
}
108+
}

0 commit comments

Comments
 (0)