|
| 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\Feature\Services; |
| 26 | + |
| 27 | +use Illuminate\Validation\ValidationException; |
| 28 | +use Tests\TestCase; |
| 29 | +use Pterodactyl\Models\Location; |
| 30 | +use Pterodactyl\Services\LocationService; |
| 31 | + |
| 32 | +class LocationServiceTest extends TestCase |
| 33 | +{ |
| 34 | + /** |
| 35 | + * @var \Pterodactyl\Services\LocationService |
| 36 | + */ |
| 37 | + protected $service; |
| 38 | + |
| 39 | + /** |
| 40 | + * Setup the test instance. |
| 41 | + */ |
| 42 | + public function setUp() |
| 43 | + { |
| 44 | + parent::setUp(); |
| 45 | + |
| 46 | + $this->service = $this->app->make(LocationService::class); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Test that a new location can be successfully added to the database. |
| 51 | + */ |
| 52 | + public function testShouldCreateANewLocation() |
| 53 | + { |
| 54 | + $data = [ |
| 55 | + 'long' => 'Long Name', |
| 56 | + 'short' => 'short', |
| 57 | + ]; |
| 58 | + |
| 59 | + $response = $this->service->create($data); |
| 60 | + |
| 61 | + $this->assertInstanceOf(Location::class, $response); |
| 62 | + $this->assertEquals($data['long'], $response->long); |
| 63 | + $this->assertEquals($data['short'], $response->short); |
| 64 | + $this->assertDatabaseHas('locations', [ |
| 65 | + 'short' => $data['short'], |
| 66 | + 'long' => $data['long'] |
| 67 | + ]); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Test that a validation error is thrown if a required field is missing. |
| 72 | + * |
| 73 | + * @expectedException \Watson\Validating\ValidationException |
| 74 | + */ |
| 75 | + public function testShouldFailToCreateLocationIfMissingParameter() |
| 76 | + { |
| 77 | + $data = ['long' => 'Long Name']; |
| 78 | + |
| 79 | + try { |
| 80 | + $this->service->create($data); |
| 81 | + } catch (\Exception $ex) { |
| 82 | + $this->assertInstanceOf(ValidationException::class, $ex); |
| 83 | + |
| 84 | + $bag = $ex->getMessageBag()->messages(); |
| 85 | + $this->assertArraySubset(['short' => [0]], $bag); |
| 86 | + $this->assertEquals('The short field is required.', $bag['short'][0]); |
| 87 | + |
| 88 | + throw $ex; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test that a validation error is thrown if the short code provided is already in use. |
| 94 | + * |
| 95 | + * @expectedException \Watson\Validating\ValidationException |
| 96 | + */ |
| 97 | + public function testShouldFailToCreateLocationIfShortCodeIsAlreadyInUse() |
| 98 | + { |
| 99 | + factory(Location::class)->create(['short' => 'inuse']); |
| 100 | + $data = [ |
| 101 | + 'long' => 'Long Name', |
| 102 | + 'short' => 'inuse', |
| 103 | + ]; |
| 104 | + |
| 105 | + try { |
| 106 | + $this->service->create($data); |
| 107 | + } catch (\Exception $ex) { |
| 108 | + $this->assertInstanceOf(ValidationException::class, $ex); |
| 109 | + |
| 110 | + $bag = $ex->getMessageBag()->messages(); |
| 111 | + $this->assertArraySubset(['short' => [0]], $bag); |
| 112 | + $this->assertEquals('The short has already been taken.', $bag['short'][0]); |
| 113 | + |
| 114 | + throw $ex; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Test that a validation error is thrown if the short code is too long. |
| 120 | + * |
| 121 | + * @expectedException \Watson\Validating\ValidationException |
| 122 | + */ |
| 123 | + public function testShouldFailToCreateLocationIfShortCodeIsTooLong() |
| 124 | + { |
| 125 | + $data = [ |
| 126 | + 'long' => 'Long Name', |
| 127 | + 'short' => str_random(200), |
| 128 | + ]; |
| 129 | + |
| 130 | + try { |
| 131 | + $this->service->create($data); |
| 132 | + } catch (\Exception $ex) { |
| 133 | + $this->assertInstanceOf(ValidationException::class, $ex); |
| 134 | + |
| 135 | + $bag = $ex->getMessageBag()->messages(); |
| 136 | + $this->assertArraySubset(['short' => [0]], $bag); |
| 137 | + $this->assertEquals('The short must be between 1 and 60 characters.', $bag['short'][0]); |
| 138 | + |
| 139 | + throw $ex; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Test that updating a model returns the updated data in a persisted form. |
| 145 | + */ |
| 146 | + public function testShouldUpdateLocationModelInDatabase() |
| 147 | + { |
| 148 | + $location = factory(Location::class)->create(); |
| 149 | + $data = ['short' => 'test_short']; |
| 150 | + |
| 151 | + $model = $this->service->update($location->id, $data); |
| 152 | + |
| 153 | + $this->assertInstanceOf(Location::class, $model); |
| 154 | + $this->assertEquals($data['short'], $model->short); |
| 155 | + $this->assertNotEquals($model->short, $location->short); |
| 156 | + $this->assertEquals($location->long, $model->long); |
| 157 | + $this->assertDatabaseHas('locations', [ |
| 158 | + 'short' => $data['short'], |
| 159 | + 'long' => $location->long, |
| 160 | + ]); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Test that passing the same short-code into the update function as the model |
| 165 | + * is currently using will not throw a validation exception. |
| 166 | + */ |
| 167 | + public function testShouldUpdateModelWithoutErrorWhenValidatingShortCodeIsUnique() |
| 168 | + { |
| 169 | + $location = factory(Location::class)->create(); |
| 170 | + $data = ['short' => $location->short]; |
| 171 | + |
| 172 | + $model = $this->service->update($location->id, $data); |
| 173 | + |
| 174 | + $this->assertInstanceOf(Location::class, $model); |
| 175 | + $this->assertEquals($model->short, $location->short); |
| 176 | + |
| 177 | + // Timestamps don't change if no data is modified. |
| 178 | + $this->assertEquals($model->updated_at, $location->updated_at); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Test that passing invalid data to the update method will throw a validation |
| 183 | + * exception. |
| 184 | + * |
| 185 | + * @expectedException \Watson\Validating\ValidationException |
| 186 | + */ |
| 187 | + public function testShouldNotUpdateModelIfPassedDataIsInvalid() |
| 188 | + { |
| 189 | + $location = factory(Location::class)->create(); |
| 190 | + $data = ['short' => str_random(200)]; |
| 191 | + |
| 192 | + $this->service->update($location->id, $data); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Test that an invalid model exception is thrown if a model doesn't exist. |
| 197 | + * |
| 198 | + * @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException |
| 199 | + */ |
| 200 | + public function testShouldThrowExceptionIfInvalidModelIdIsProvided() |
| 201 | + { |
| 202 | + $this->service->update(0, []); |
| 203 | + } |
| 204 | +} |
0 commit comments