|
| 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 | +} |
0 commit comments