1 | <?php |
||
19 | trait TestsAuthHelperTrait |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * Logged in user object. |
||
24 | * |
||
25 | * @var User |
||
26 | */ |
||
27 | protected $testingUser; |
||
28 | |||
29 | /** |
||
30 | * Roles and permissions, to be attached on the user |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $access = [ |
||
35 | 'permissions' => '', |
||
36 | 'roles' => '', |
||
37 | ]; |
||
38 | |||
39 | /** |
||
40 | * Try to get the last logged in User, if not found then create new one. |
||
41 | * Note: if $userDetails are provided it will always create new user, even |
||
42 | * if another one was previously created during the execution of your test. |
||
43 | * |
||
44 | * By default Users will be given the Roles and Permissions found int he class |
||
45 | * `$access` property. But the $access parameter can be used to override the |
||
46 | * defined roles and permissions in the `$access` property of your class. |
||
47 | * |
||
48 | * @param null $access roles and permissions you'd like to provide this user with |
||
49 | * @param null $userDetails what to be attached on the User object |
||
50 | * |
||
51 | * @return \App\Containers\User\Models\User |
||
52 | */ |
||
53 | public function getTestingUser($userDetails = null, $access = null) |
||
58 | |||
59 | /** |
||
60 | * Same as `getTestingUser()` but always overrides the User Access |
||
61 | * (roles and permissions) with null. So the user can be used to test |
||
62 | * if unauthorized user tried to access your protected endpoint. |
||
63 | * |
||
64 | * @param null $userDetails |
||
65 | * |
||
66 | * @return \App\Containers\User\Models\User |
||
67 | */ |
||
68 | public function getTestingUserWithoutAccess($userDetails = null) |
||
72 | |||
73 | /** |
||
74 | * @param $userDetails |
||
75 | * @param $access |
||
76 | * |
||
77 | * @return \App\Containers\User\Models\User |
||
78 | */ |
||
79 | private function findOrCreateTestingUser($userDetails, $access) |
||
83 | |||
84 | /** |
||
85 | * @param null $access |
||
86 | * @param null $userDetails |
||
87 | * |
||
88 | * @return User |
||
89 | */ |
||
90 | private function createTestingUser($userDetails = null, $access = null) |
||
104 | |||
105 | /** |
||
106 | * @param null $userDetails |
||
107 | * |
||
108 | * @return User |
||
109 | */ |
||
110 | private function factoryCreateUser($userDetails = null) |
||
114 | |||
115 | /** |
||
116 | * @param null $userDetails |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | private function prepareUserDetails($userDetails = null) |
||
131 | |||
132 | /** |
||
133 | * @param $userDetails |
||
134 | * |
||
135 | * @return null |
||
136 | */ |
||
137 | private function prepareUserPassword($userDetails) |
||
147 | |||
148 | /** |
||
149 | * @return array|null |
||
150 | */ |
||
151 | private function getAccess() |
||
155 | |||
156 | /** |
||
157 | * @param $user |
||
158 | * @param $access |
||
159 | * |
||
160 | * @return mixed |
||
161 | */ |
||
162 | private function setupTestingUserAccess($user, $access) |
||
171 | |||
172 | /** |
||
173 | * @param $user |
||
174 | * @param $access |
||
175 | * |
||
176 | * @return mixed |
||
177 | */ |
||
178 | private function setupTestingUserRoles($user, $access) |
||
188 | |||
189 | /** |
||
190 | * @param $user |
||
191 | * @param $access |
||
192 | * |
||
193 | * @return mixed |
||
194 | */ |
||
195 | private function setupTestingUserPermissions($user, $access) |
||
203 | |||
204 | |||
205 | /** |
||
206 | * @return array |
||
207 | */ |
||
208 | private function getNullAccess() |
||
215 | |||
216 | } |
||
217 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: