1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RafflesArgentina\ResourceController; |
4
|
|
|
|
5
|
|
|
use Orchestra\Testbench\TestCase; |
6
|
|
|
|
7
|
|
|
class ResourceControllerTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
use BaseTest; |
10
|
|
|
|
11
|
|
|
function testIndexRoute() |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
factory(\RafflesArgentina\ResourceController\Models\User::class, 3)->create(); |
14
|
|
|
|
15
|
|
|
$this->get('/test') |
16
|
|
|
->assertViewIs('test.index') |
17
|
|
|
->assertViewHas('items') |
18
|
|
|
->assertStatus(200); |
19
|
|
|
|
20
|
|
|
$this->json('GET', '/test') |
21
|
|
|
->assertStatus(200); |
22
|
|
|
//->assertJsonCount(3, 'data'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
function testIndexRouteWithUseSoftDeletes() |
26
|
|
|
{ |
27
|
|
|
$users = factory(\RafflesArgentina\ResourceController\Models\User::class, 3)->create(); |
28
|
|
|
foreach ($users as $user) $user->delete(); |
29
|
|
|
|
30
|
|
|
$this->get('/test2') |
31
|
|
|
->assertViewIs('test.index') |
32
|
|
|
->assertViewHas('items') |
33
|
|
|
->assertStatus(200); |
34
|
|
|
|
35
|
|
|
$this->json('GET', '/test2') |
36
|
|
|
->assertStatus(200); |
37
|
|
|
//->assertJsonCount(3, 'data'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function testCreateRoute() |
41
|
|
|
{ |
42
|
|
|
$this->get('/test/create') |
43
|
|
|
->assertViewIs('test.create') |
44
|
|
|
->assertViewHas('model') |
45
|
|
|
->assertStatus(200); |
46
|
|
|
|
47
|
|
|
$this->json('GET', '/test/create') |
48
|
|
|
->assertStatus(404); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
function testStoreRoute() |
52
|
|
|
{ |
53
|
|
|
$this->post('/test', ['name' => 'Mario', 'email' => '[email protected]', 'password' => bcrypt(str_random())]) |
54
|
|
|
->assertRedirect('/test') |
55
|
|
|
->assertSessionHas('rafflesargentina.status.success'); |
56
|
|
|
|
57
|
|
|
$this->json('POST', '/test', ['name' => 'Paula', 'email' => '[email protected]', 'password' => bcrypt(str_random())]) |
58
|
|
|
->assertStatus(200); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
function testShowRoute() |
62
|
|
|
{ |
63
|
|
|
$user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create(); |
64
|
|
|
|
65
|
|
|
$this->get('/test/'.$user->id) |
66
|
|
|
->assertViewIs('test.show') |
67
|
|
|
->assertViewHas('model') |
68
|
|
|
->assertStatus(200); |
69
|
|
|
|
70
|
|
|
$this->json('GET', '/test/'.$user->id) |
71
|
|
|
->assertStatus(200); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
function testShowRouteWithUseSoftDeletes() |
75
|
|
|
{ |
76
|
|
|
$user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create(); |
77
|
|
|
$user->delete(); |
78
|
|
|
|
79
|
|
|
$this->get('/test2/'.$user->id) |
80
|
|
|
->assertViewIs('test.show') |
81
|
|
|
->assertViewHas('model') |
82
|
|
|
->assertStatus(200); |
83
|
|
|
|
84
|
|
|
$this->json('GET', '/test2/'.$user->id) |
85
|
|
|
->assertStatus(200); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
function testShowRouteWithInexistentModel() |
89
|
|
|
{ |
90
|
|
|
$this->get('/test/7')->assertStatus(404); |
91
|
|
|
|
92
|
|
|
$this->json('GET', '/test/7')->assertStatus(404); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
function testEditRoute() |
96
|
|
|
{ |
97
|
|
|
$user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create(); |
98
|
|
|
|
99
|
|
|
$this->get('/test/'.$user->id.'/edit') |
100
|
|
|
->assertViewIs('test.edit') |
101
|
|
|
->assertViewHas('model') |
102
|
|
|
->assertStatus(200); |
103
|
|
|
|
104
|
|
|
$this->json('GET', '/test/'.$user->id.'/edit') |
105
|
|
|
->assertStatus(404); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
function testEditRouteWithUseSoftDeletes() |
109
|
|
|
{ |
110
|
|
|
$user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create(); |
111
|
|
|
$user->delete(); |
112
|
|
|
|
113
|
|
|
$this->get('/test2/'.$user->id.'/edit') |
114
|
|
|
->assertViewIs('test.edit') |
115
|
|
|
->assertViewHas('model') |
116
|
|
|
->assertStatus(200); |
117
|
|
|
|
118
|
|
|
$this->json('GET', '/test2/'.$user->id.'/edit') |
119
|
|
|
->assertStatus(404); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
function testEditRouteWithInexistentModel() |
123
|
|
|
{ |
124
|
|
|
$this->get('/test/7/edit')->assertStatus(404); |
125
|
|
|
|
126
|
|
|
$this->json('GET', '/test/7/edit')->assertStatus(404); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
function testUpdateRoute() |
130
|
|
|
{ |
131
|
|
|
$user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create(); |
132
|
|
|
|
133
|
|
|
$this->put('/test/'.$user->id, ['name' => 'Mario', 'email' => '[email protected]', 'password' => bcrypt(str_random())]) |
134
|
|
|
->assertRedirect('/test') |
135
|
|
|
->assertStatus(302) |
136
|
|
|
->assertSessionHas('rafflesargentina.status.success'); |
137
|
|
|
|
138
|
|
|
$this->json('PUT', '/test/'.$user->id, ['name' => 'Mario', 'email' => '[email protected]', 'password' => bcrypt(str_random())]) |
139
|
|
|
->assertStatus(200); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
function testUpdateRouteWithUseSoftDeletes() |
143
|
|
|
{ |
144
|
|
|
$user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create(); |
145
|
|
|
$user->delete(); |
146
|
|
|
|
147
|
|
|
$this->put('/test2/'.$user->id, ['name' => 'Mario', 'email' => '[email protected]', 'password' => bcrypt(str_random())]) |
148
|
|
|
->assertRedirect('/test') |
149
|
|
|
->assertStatus(302) |
150
|
|
|
->assertSessionHas('rafflesargentina.status.success'); |
151
|
|
|
|
152
|
|
|
$this->json('PUT', '/test2/'.$user->id, ['name' => 'Mario', 'email' => '[email protected]', 'password' => bcrypt(str_random())]) |
153
|
|
|
->assertStatus(200); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
function testUpdateRouteWithInexistentModel() |
157
|
|
|
{ |
158
|
|
|
$this->put('/test/7')->assertStatus(404); |
159
|
|
|
|
160
|
|
|
$this->json('PUT', '/test/7')->assertStatus(404); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
function testDestroyRoute() |
164
|
|
|
{ |
165
|
|
|
$user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create(); |
166
|
|
|
$this->delete('/test/'.$user->id) |
167
|
|
|
->assertRedirect('/test') |
168
|
|
|
->assertStatus(302); |
169
|
|
|
|
170
|
|
|
$user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create(); |
171
|
|
|
$this->json('DELETE', '/test/'.$user->id) |
172
|
|
|
->assertStatus(200); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
function testDestroyRouteWithUseSoftDeletes() |
176
|
|
|
{ |
177
|
|
|
$user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create(); |
178
|
|
|
$user->delete(); |
179
|
|
|
|
180
|
|
|
$this->delete('/test2/'.$user->id) |
181
|
|
|
->assertRedirect('/test') |
182
|
|
|
->assertStatus(302); |
183
|
|
|
|
184
|
|
|
$this->json('DELETE', '/test2/'.$user->id) |
185
|
|
|
->assertStatus(200); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
function testDestroyRouteWithInexistentModel() |
189
|
|
|
{ |
190
|
|
|
$this->delete('/test/7')->assertStatus(404); |
191
|
|
|
|
192
|
|
|
$this->json('DELETE', '/test/7')->assertStatus(404); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.