Skip to content

Commit ba61b09

Browse files
authored
Merge pull request #25 from flyingluscas/feature/categories-controller-tests
Categories controller tests
2 parents 9393b75 + 5f0dfdd commit ba61b09

File tree

3 files changed

+185
-11
lines changed

3 files changed

+185
-11
lines changed

webservice/app/Http/Controllers/CategoriesController.php

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,96 @@ public function all(Request $request)
1212
{
1313
try {
1414
$pager = Category::paginate(10);
15+
1516
return response()->json(compact('pager'), 200);
1617
} catch(\Exception $e) {
17-
return response()->json(['messages' => ['List of categories not available']], 404);
18+
return response()->json([
19+
'messages' => ['Failed to fetch categories'],
20+
], 500);
1821
}
1922
}
2023

2124
public function get($id)
2225
{
2326
try {
2427
$category = Category::find($id);
25-
return response()->json(['result' => 'success', 'category' => $category], 200);
26-
} catch(\Exception $e) {
27-
return response()->json(['messages' => ['Category could not be fetched']], 404);
28+
29+
if (! $category) {
30+
return response()->json([
31+
'messages' => ['Category not found'],
32+
], 404);
33+
}
34+
35+
return response()->json([
36+
'result' => 'success',
37+
'category' => $category,
38+
], 200);
39+
} catch (\Exception $e) {
40+
return response()->json([
41+
'messages' => ['Failed to fetch category'],
42+
], 500);
2843
}
2944
}
3045

3146
public function create(CategoryRequest $request)
3247
{
3348
try {
3449
Category::create($request->only('name'));
35-
return response()->json(['result' => 'success'], 200);
50+
51+
return response()->json([
52+
'result' => 'success',
53+
], 200);
3654
} catch(\Exception $e) {
37-
return response()->json(['messages' => ['Failed to create category']], 422);
55+
return response()->json([
56+
'messages' => ['Failed to create category'],
57+
], 500);
3858
}
3959
}
4060

4161
public function update($id, CategoryRequest $request)
4262
{
4363
try {
4464
$category = Category::find($id);
65+
66+
if (! $category) {
67+
return response()->json([
68+
'messages' => ['Category not found'],
69+
], 404);
70+
}
71+
4572
$category->name = $request->get('name');
4673
$category->save();
47-
return response()->json(['result' => 'success'], 200);
74+
75+
return response()->json([
76+
'result' => 'success',
77+
], 200);
4878
} catch (\Exception $e) {
49-
return response()->json(['messages' => ['Failed to update category']], 422);
79+
return response()->json([
80+
'messages' => ['Failed to update category'],
81+
], 500);
5082
}
5183
}
5284

5385
public function remove($id)
5486
{
5587
try {
5688
$category = Category::find($id);
89+
90+
if (! $category) {
91+
return response()->json([
92+
'messages' => ['Category not found'],
93+
], 404);
94+
}
95+
5796
$category->delete();
58-
return response()->json(['result' => 'success'], 200);
97+
98+
return response()->json([
99+
'result' => 'success',
100+
], 200);
59101
} catch (\Exception $e) {
60-
return response()->json(['messages' => ['Failed to remove category']], 422);
102+
return response()->json([
103+
'messages' => ['Failed to remove category'],
104+
], 500);
61105
}
62106
}
63107
}

webservice/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
5151
"php artisan optimize"
5252
],
53-
"test": "phpunit"
53+
"test": "phpunit --colors=always"
5454
},
5555
"config": {
5656
"preferred-install": "dist"
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
use App\Category;
4+
use Illuminate\Foundation\Testing\WithoutMiddleware;
5+
use Illuminate\Foundation\Testing\DatabaseMigrations;
6+
use Illuminate\Foundation\Testing\DatabaseTransactions;
7+
8+
class CategoriesControllerTest extends ApiTestCase
9+
{
10+
use DatabaseMigrations, WithoutMiddleware, Factory;
11+
12+
/**
13+
* @test
14+
*/
15+
public function can_delete_category()
16+
{
17+
$this->create(Category::class);
18+
19+
$this->json('DELETE', '/api/categories/1/remove');
20+
21+
$this->assertResponseOk();
22+
$this->dontSeeInDatabase('categories', ['id' => 1]);
23+
}
24+
25+
/**
26+
* @test
27+
*/
28+
public function can_update_category()
29+
{
30+
$this->create(Category::class);
31+
32+
$this->json('PUT', '/api/categories/1/update', [
33+
'name' => 'Dummy',
34+
]);
35+
36+
$this->assertResponseOk();
37+
$this->seeInDatabase('categories', [
38+
'id' => 1,
39+
'name' => 'Dummy',
40+
]);
41+
}
42+
43+
/**
44+
* @test
45+
* @dataProvider urlProvider
46+
*/
47+
public function get_not_found_if_category_dont_exist($method, $url)
48+
{
49+
$this->json($method, $url, [
50+
'name' => 'Dummy',
51+
]);
52+
53+
$this->assertResponseStatus(404);
54+
$this->seeJsonStructure([
55+
'messages' => [[]],
56+
]);
57+
}
58+
59+
/**
60+
* Url data privider.
61+
*
62+
* @return array
63+
*/
64+
public function urlProvider()
65+
{
66+
return [
67+
['GET', '/api/categories/1/get'],
68+
['PUT', '/api/categories/1/update'],
69+
['DELETE', '/api/categories/1/remove'],
70+
];
71+
}
72+
73+
/**
74+
* @test
75+
*/
76+
public function can_get_category()
77+
{
78+
$this->create(Category::class, [
79+
'name' => 'Dummy',
80+
]);
81+
82+
$this->json('GET', '/api/categories/1/get');
83+
84+
$this->assertResponseOk();
85+
86+
$this->seeJsonStructure([
87+
'result', 'category' => [
88+
'id', 'name',
89+
],
90+
]);
91+
92+
$this->seeJson([
93+
'name' => 'Dummy',
94+
]);
95+
}
96+
97+
/**
98+
* @test
99+
*/
100+
public function can_create_category()
101+
{
102+
$this->json('POST', '/api/categories/create', [
103+
'name' => 'Dummy name',
104+
]);
105+
106+
$this->assertResponseOk();
107+
$this->seeInDatabase('categories', [
108+
'name' => 'Dummy name',
109+
]);
110+
}
111+
112+
/**
113+
* @test
114+
*/
115+
public function can_get_categories()
116+
{
117+
$this->times(3)->create(Category::class);
118+
119+
$this->json('GET', '/api/categories');
120+
121+
$this->assertResponseOk();
122+
$this->seeJsonStructure([
123+
'pager' => [
124+
'data' => [
125+
'*' => ['id', 'name'],
126+
],
127+
],
128+
]);
129+
}
130+
}

0 commit comments

Comments
 (0)