2
2
3
3
namespace Tests \Feature ;
4
4
5
+ use Illuminate \Foundation \Auth \User ;
5
6
use Illuminate \Routing \Router ;
6
7
use LaravelDomainOriented \Tests \Cases \DBTestCase ;
7
8
@@ -24,6 +25,7 @@ public function setUp(): void
24
25
/** @test **/
25
26
public function it_should_call_list_route_and_assert_count_of_items ()
26
27
{
28
+ $ this ->loginWithFakeUser ();
27
29
$ response = $ this ->getJson ('tests ' );
28
30
$ response ->assertOk ();
29
31
@@ -32,9 +34,17 @@ public function it_should_call_list_route_and_assert_count_of_items()
32
34
$ this ->assertCount (count ($ this ->data ), $ data ['data ' ]);
33
35
}
34
36
37
+ /** @test **/
38
+ public function it_should_try_call_list_route_without_login_and_get_403 ()
39
+ {
40
+ $ response = $ this ->getJson ('tests ' );
41
+ $ response ->assertForbidden ();
42
+ }
43
+
35
44
/** @test **/
36
45
public function it_should_call_find_route_and_assert_item ()
37
46
{
47
+ $ this ->loginWithFakeUser ();
38
48
$ response = $ this ->getJson ('tests/1 ' );
39
49
$ response ->assertOk ();
40
50
@@ -46,13 +56,15 @@ public function it_should_call_find_route_and_assert_item()
46
56
/** @test **/
47
57
public function it_should_call_find_route_with_non_existent_id_and_assert_status_404 ()
48
58
{
59
+ $ this ->loginWithFakeUser ();
49
60
$ response = $ this ->getJson ('tests/15 ' );
50
61
$ response ->assertStatus (404 );
51
62
}
52
63
53
64
/** @test **/
54
65
public function it_should_create_a_item ()
55
66
{
67
+ $ this ->loginWithFakeUser ();
56
68
$ response = $ this ->postJson ('tests ' , [
57
69
'name ' => 'XXX '
58
70
]);
@@ -66,6 +78,7 @@ public function it_should_create_a_item()
66
78
/** @test **/
67
79
public function it_should_try_create_a_item_and_assert_status_422 ()
68
80
{
81
+ $ this ->loginWithFakeUser ();
69
82
$ response = $ this ->postJson ('tests ' , [
70
83
'name ' => 1
71
84
]);
@@ -75,6 +88,7 @@ public function it_should_try_create_a_item_and_assert_status_422()
75
88
/** @test **/
76
89
public function it_should_update_a_item ()
77
90
{
91
+ $ this ->loginWithFakeUser ();
78
92
$ updateName = 'XXX ' ;
79
93
$ response = $ this ->putJson ('tests/1 ' , [
80
94
'name ' => $ updateName
@@ -89,6 +103,7 @@ public function it_should_update_a_item()
89
103
/** @test **/
90
104
public function it_should_try_update_a_item_and_assert_status_422 ()
91
105
{
106
+ $ this ->loginWithFakeUser ();
92
107
$ response = $ this ->putJson ('tests/1 ' , [
93
108
'name ' => 1
94
109
]);
@@ -98,6 +113,7 @@ public function it_should_try_update_a_item_and_assert_status_422()
98
113
/** @test **/
99
114
public function it_should_delete_a_item ()
100
115
{
116
+ $ this ->loginWithFakeUser ();
101
117
$ this ->withoutMiddleware ();
102
118
$ response = $ this ->deleteJson ('tests/1 ' );
103
119
$ response ->assertOk ();
@@ -107,4 +123,21 @@ public function it_should_delete_a_item()
107
123
$ this ->assertTrue ($ data ['data ' ]['isDeleted ' ]);
108
124
$ this ->assertSoftDeleted ('tests ' );
109
125
}
126
+
127
+ private function loginWithFakeUser ()
128
+ {
129
+ $ user = new MyUserModel ([
130
+ 'id ' => 1 ,
131
+ 'name ' => 'test user ' ,
132
+ ]);
133
+
134
+ $ this ->be ($ user );
135
+ }
136
+ }
137
+
138
+ class MyUserModel extends User {
139
+ protected $ fillable = [
140
+ 'id ' ,
141
+ 'name ' ,
142
+ ];
110
143
}
0 commit comments