1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: arthur |
5
|
|
|
* Date: 09.10.18 |
6
|
|
|
* Time: 21:56. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Foundation\Abstracts\Tests; |
10
|
|
|
|
11
|
|
|
use Foundation\Traits\RefreshDatabase; |
12
|
|
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase; |
13
|
|
|
use Modules\Authorization\Contracts\AuthorizationContract; |
14
|
|
|
use Modules\User\Contracts\UserServiceContract; |
15
|
|
|
use Modules\User\Entities\User; |
16
|
|
|
use Modules\User\Services\UserService; |
17
|
|
|
|
18
|
|
|
abstract class TestCase extends BaseTestCase |
19
|
|
|
{ |
20
|
|
|
use RefreshDatabase, CreatesApplication; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var UserService |
24
|
|
|
*/ |
25
|
|
|
private $userService; |
26
|
|
|
|
27
|
90 |
|
public function setUp(): void |
28
|
|
|
{ |
29
|
90 |
|
parent::setUp(); |
30
|
90 |
|
$this->userService = $this->app->make(UserServiceContract::class); |
31
|
90 |
|
$this->app->make(AuthorizationContract::class)->clearPermissionCache(); |
32
|
90 |
|
$this->app->make(\Spatie\Permission\PermissionRegistrar::class)->registerPermissions(); |
33
|
90 |
|
$this->actingAs($this->actingUser()); |
34
|
90 |
|
$this->seedData(); |
35
|
90 |
|
} |
36
|
|
|
|
37
|
88 |
|
protected function seedData() |
38
|
|
|
{ |
39
|
88 |
|
} |
40
|
|
|
|
41
|
35 |
|
private function createUser() |
42
|
|
|
{ |
43
|
35 |
|
return $this->userService->create(factory(User::class)->raw()); |
44
|
|
|
} |
45
|
|
|
|
46
|
35 |
|
protected function actingUser() |
47
|
|
|
{ |
48
|
35 |
|
return $this->getRandomUser(); |
49
|
|
|
} |
50
|
|
|
|
51
|
90 |
|
public function actingAs($user, $driver = null) |
52
|
|
|
{ |
53
|
90 |
|
parent::actingAs($user, $driver); |
54
|
90 |
|
} |
55
|
|
|
|
56
|
35 |
|
private function getRandomUser(): User |
57
|
|
|
{ |
58
|
35 |
|
$users = User::all(); |
59
|
35 |
|
if ($users->isEmpty()) { |
60
|
35 |
|
$user = $this->createUser(); |
61
|
|
|
} else { |
62
|
9 |
|
$user = $users->random(); |
63
|
|
|
} |
64
|
35 |
|
return $user; |
65
|
|
|
} |
66
|
|
|
|
67
|
9 |
|
protected function actAsRandomUser(): User |
68
|
|
|
{ |
69
|
9 |
|
$user = $this->getRandomUser(); |
70
|
9 |
|
$this->actingAs($user); |
71
|
9 |
|
return $user; |
72
|
|
|
} |
73
|
|
|
|
74
|
58 |
|
protected function getActingUser(): User |
75
|
|
|
{ |
76
|
58 |
|
return auth()->user(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|