Skip to content

Commit dcbc136

Browse files
authored
Improve test coverage for LocationController (pterodactyl#3779)
By adding tests for create, update, delete
1 parent 622b939 commit dcbc136

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tests/Integration/Api/Application/Location/LocationControllerTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Pterodactyl\Models\Node;
66
use Illuminate\Http\Response;
77
use Pterodactyl\Models\Location;
8+
use Pterodactyl\Transformers\Api\Application\LocationTransformer;
89
use Pterodactyl\Transformers\Api\Application\NodeTransformer;
910
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
1011
use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;
@@ -88,6 +89,77 @@ public function testGetSingleLocation()
8889
], true);
8990
}
9091

92+
/**
93+
* Test that a location can be created.
94+
*/
95+
public function testCreateLocation()
96+
{
97+
$response = $this->postJson('/api/application/locations', [
98+
'short' => 'inhouse',
99+
'long' => 'This is my inhouse location',
100+
]);
101+
102+
$response->assertStatus(Response::HTTP_CREATED);
103+
$response->assertJsonCount(3);
104+
$response->assertJsonStructure([
105+
'object',
106+
'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at'],
107+
'meta' => ['resource'],
108+
]);
109+
110+
$this->assertDatabaseHas('locations', ['short' => 'inhouse', 'long' => 'This is my inhouse location']);
111+
112+
$location = Location::where('short', 'inhouse')->first();
113+
$response->assertJson([
114+
'object' => 'location',
115+
'attributes' => $this->getTransformer(LocationTransformer::class)->transform($location),
116+
'meta' => [
117+
'resource' => route('api.application.locations.view', $location->id),
118+
],
119+
], true);
120+
}
121+
122+
/**
123+
* Test that a location can be updated.
124+
*/
125+
public function testUpdateLocation()
126+
{
127+
$location = Location::factory()->create();
128+
129+
$response = $this->patchJson('/api/application/locations/' . $location->id, [
130+
'short' => 'new inhouse',
131+
'long' => 'This is my new inhouse location'
132+
]);
133+
$response->assertStatus(Response::HTTP_OK);
134+
$response->assertJsonCount(2);
135+
$response->assertJsonStructure([
136+
'object',
137+
'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at']
138+
]);
139+
140+
$this->assertDatabaseHas('locations', ['short' => 'new inhouse', 'long' => 'This is my new inhouse location']);
141+
$location = $location->fresh();
142+
143+
$response->assertJson([
144+
'object' => 'location',
145+
'attributes' => $this->getTransformer(LocationTransformer::class)->transform($location),
146+
]);
147+
}
148+
149+
/**
150+
* Test that a location can be deleted from the database.
151+
*/
152+
public function testDeleteLocation()
153+
{
154+
$location = Location::factory()->create();
155+
$this->assertDatabaseHas('locations', ['id' => $location->id]);
156+
157+
$response = $this->delete('/api/application/locations/' . $location->id);
158+
$response->assertStatus(Response::HTTP_NO_CONTENT);
159+
160+
$this->assertDatabaseMissing('locations', ['id' => $location->id]);
161+
}
162+
91163
/**
92164
* Test that all of the defined relationships for a location can be loaded successfully.
93165
*/

0 commit comments

Comments
 (0)