|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Feature\Api; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Category; |
|
6
|
|
|
use App\Models\User; |
|
7
|
|
|
use Tests\Features\FeatureTestCase; |
|
8
|
|
|
|
|
9
|
|
|
class CategoryControllerTest extends FeatureTestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* A basic test example. |
|
13
|
|
|
* |
|
14
|
|
|
* @test |
|
15
|
|
|
* |
|
16
|
|
|
* @return void |
|
17
|
|
|
*/ |
|
18
|
|
|
public function it_return_all_subcategories_of_a_category() |
|
19
|
|
|
{ |
|
20
|
|
|
// Arrange |
|
21
|
|
|
$category = factory(Category::class)->create(); |
|
22
|
|
|
$subCategory1 = factory(Category::class)->create(['parent_id' => $category->id]); |
|
23
|
|
|
$subCategory2 = factory(Category::class)->create(['parent_id' => $category->id]); |
|
24
|
|
|
|
|
25
|
|
|
$url = '/api/v1/category/%d/subcategories'; |
|
26
|
|
|
$url = sprintf($url, $category->id); |
|
27
|
|
|
|
|
28
|
|
|
// Act |
|
29
|
|
|
$this->ensureAuthenticated(); |
|
30
|
|
|
$response = $this->get($url); |
|
31
|
|
|
|
|
32
|
|
|
// Assert |
|
33
|
|
|
$response->assertStatus(200) |
|
34
|
|
|
->assertJsonFragment([ |
|
35
|
|
|
'id' => $subCategory1->id, |
|
36
|
|
|
'name' => $subCategory1->name, |
|
37
|
|
|
])->assertJsonFragment([ |
|
38
|
|
|
'id' => $subCategory2->id, |
|
39
|
|
|
'name' => $subCategory2->name, |
|
40
|
|
|
]); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* A basic test example. |
|
45
|
|
|
* |
|
46
|
|
|
* @test |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public function it_can_access_just_root_categories_of_a_user() |
|
51
|
|
|
{ |
|
52
|
|
|
// Arrange |
|
53
|
|
|
$category1 = factory(Category::class)->create(['user_id' => $this->user->id]); |
|
54
|
|
|
$category2 = factory(Category::class)->create(['user_id' => $this->user->id]); |
|
55
|
|
|
$subCategory1 = factory(Category::class)->create(['user_id' => $this->user->id, 'parent_id' => $category2->id]); |
|
56
|
|
|
$subCategory2 = factory(Category::class)->create(['user_id' => $this->user->id, 'parent_id' => $category2->id]); |
|
57
|
|
|
|
|
58
|
|
|
$anotherUser = factory(User::class)->create(); |
|
59
|
|
|
$category3 = factory(Category::class)->create(['user_id' => $anotherUser->id]); |
|
60
|
|
|
$category4 = factory(Category::class)->create(['user_id' => $anotherUser->id]); |
|
61
|
|
|
|
|
62
|
|
|
$url = '/api/v1/category'; |
|
63
|
|
|
|
|
64
|
|
|
// Act |
|
65
|
|
|
$this->ensureAuthenticated(); |
|
66
|
|
|
$response = $this->get($url); |
|
67
|
|
|
|
|
68
|
|
|
// Assert |
|
69
|
|
|
$response->assertStatus(200); |
|
70
|
|
|
|
|
71
|
|
|
// no sub categories |
|
72
|
|
|
$response->assertJsonMissing([ |
|
73
|
|
|
'id' => $subCategory1->id, |
|
74
|
|
|
'name' => $subCategory1->name, |
|
75
|
|
|
])->assertJsonMissing([ |
|
76
|
|
|
'id' => $subCategory2->id, |
|
77
|
|
|
'name' => $subCategory2->name, |
|
78
|
|
|
]); |
|
79
|
|
|
|
|
80
|
|
|
// no categories of other users |
|
81
|
|
|
$response->assertJsonMissing([ |
|
82
|
|
|
'id' => $category3->id, |
|
83
|
|
|
'name' => $category3->name, |
|
84
|
|
|
])->assertJsonMissing([ |
|
85
|
|
|
'id' => $category4->id, |
|
86
|
|
|
'name' => $category4->name, |
|
87
|
|
|
]); |
|
88
|
|
|
|
|
89
|
|
|
// just root categories |
|
90
|
|
|
$response->assertJsonFragment([ |
|
91
|
|
|
'id' => $category1->id, |
|
92
|
|
|
'name' => $category1->name, |
|
93
|
|
|
])->assertJsonFragment([ |
|
94
|
|
|
'id' => $category2->id, |
|
95
|
|
|
'name' => $category2->name, |
|
96
|
|
|
]); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|