Skip to content

Commit 760525a

Browse files
committed
Push more tests for location services, setup travis CI integration
1 parent 13cd01c commit 760525a

File tree

8 files changed

+279
-69
lines changed

8 files changed

+279
-69
lines changed

.env.travis

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
APP_ENV=testing
2+
APP_DEBUG=true
3+
APP_KEY=SomeRandomString32SomeRandomString32
4+
5+
DB_CONNECTION=tests
6+
DB_TEST_USERNAME=root
7+
DB_TEST_PASSWORD=
8+
9+
CACHE_DRIVER=array
10+
SESSION_DRIVER=array
11+
QUEUE_DRIVER=sync
12+
MAIL_DRIVER=array

.travis.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
langauge: php
2+
3+
dist: trusty
4+
5+
php:
6+
- 7.0
7+
- 7.1
8+
- 7.2
9+
10+
sudo: required
11+
12+
cache:
13+
directories:
14+
- $HOME/.composer/cache
15+
16+
services:
17+
- mysql
18+
19+
before_install:
20+
- mysql -e 'CREATE DATABASE travis;'
21+
22+
before_script:
23+
- phpenv config-rm xdebug.ini
24+
- cp .env.travis .env
25+
- composer self-update
26+
- composer install --no-interaction
27+
- php artisan key:generate --force
28+
- php artisan migrate --force
29+
- php artisan db:seed --force
30+
31+
script:
32+
- vendor/bin/phpunit --coverage-clover=coverage.xml

app/Services/LocationService.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,16 @@ public function create(array $data)
6464
/**
6565
* Update location model in the DB.
6666
*
67-
* @param \Pterodactyl\Models\Location $location
68-
* @param array $data
67+
* @param int $id
68+
* @param array $data
6969
* @return \Pterodactyl\Models\Location
7070
*
7171
* @throws \Throwable
7272
* @throws \Watson\Validating\ValidationException
7373
*/
74-
public function update(Location $location, array $data)
74+
public function update($id, array $data)
7575
{
76+
$location = $this->model->findOrFail($id);
7677
$location->fill($data)->saveOrFail();
7778

7879
return $location;
@@ -81,15 +82,15 @@ public function update(Location $location, array $data)
8182
/**
8283
* Delete a model from the DB.
8384
*
84-
* @param \Pterodactyl\Models\Location $location
85+
* @param int $id
8586
* @return bool
86-
*
87-
* @throws \Exception
8887
* @throws \Pterodactyl\Exceptions\DisplayException
8988
*/
90-
public function delete(Location $location)
89+
public function delete($id)
9190
{
92-
if ($location->nodes()->count() > 0) {
91+
$location = $this->model->withCount('nodes')->findOrFail($id);
92+
93+
if ($location->nodes_count > 0) {
9394
throw new DisplayException('Cannot delete a location that has nodes assigned to it.');
9495
}
9596

config/database.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@
4444
'prefix' => '',
4545
'strict' => false,
4646
],
47+
48+
'tests' => [
49+
'driver' => 'mysql',
50+
'host' => env('DB_HOST', 'localhost'),
51+
'port' => env('DB_PORT', '3306'),
52+
'database' => env('DB_DATABASE', 'travis'),
53+
'username' => env('DB_USERNAME', 'root'),
54+
'password' => env('DB_PASSWORD', ''),
55+
'charset' => 'utf8',
56+
'collation' => 'utf8_unicode_ci',
57+
'prefix' => '',
58+
'strict' => false,
59+
],
4760
],
4861

4962
/*

database/factories/ModelFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@
3131
'root_admin' => true,
3232
];
3333
});
34+
35+
$factory->define(Pterodactyl\Models\Location::class, function (Faker\Generator $faker) {
36+
return [
37+
'short' => $faker->domainWord,
38+
'long' => $faker->catchPhrase,
39+
];
40+
});

phpunit.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
</filter>
2525
<php>
2626
<env name="APP_ENV" value="testing"/>
27+
<env name="DB_CONNECTION" value="tests"/>
2728
<env name="CACHE_DRIVER" value="array"/>
2829
<env name="SESSION_DRIVER" value="array"/>
2930
<env name="QUEUE_DRIVER" value="sync"/>
31+
<env name="MAIL_DRIVER" value="array"/>
3032
</php>
3133
</phpunit>
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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+
}

tests/Unit/Services/UserServiceTest.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)