forked from point-red/point
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestCase.php
190 lines (161 loc) · 5.43 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Tests;
use App\Model\Accounting\ChartOfAccount;
use App\Model\Accounting\ChartOfAccountType;
use App\Model\Master\Branch;
use App\Model\Package;
use App\Model\Project\Project;
use App\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Log;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use WithFaker;
/**
* Database migration can be so slow on local machine
* Instead run migrate on each test, you can run manually
* "php artisan migrate:fresh --env=testing"
* "php artisan migrate:fresh --env=testing --database=tenant --path=database/migrations/tenant"
* and comment this code below "use RefreshTenantDatabase;"
* and uncomment "use DatabaseTransactions;".
*
* By default we still use "use RefreshTenantDatabase;" for integration with travis, etc
* So you shouldn't commit this change
*/
use RefreshTenantDatabase;
// use DatabaseTransactions;
// Setting this allows both DB connections to be reset between tests
protected $connectionsToTransact = ['mysql', 'tenant'];
/**
* @var null|User
*/
protected $user;
protected $account = null;
/**
* Set up the test.
*/
public function setUp(): void
{
parent::setUp();
if (! defined('LARAVEL_START')) {
define('LARAVEL_START', microtime(true));
}
$this->headers = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Tenant' => 'test',
'Timezone' => 'asia/jakarta',
];
\DB::beginTransaction();
}
protected function tearDown(): void
{
\DB::rollback();
$this->logRequestTime();
parent::tearDown();
}
protected function signIn()
{
$this->user = factory(User::class)->create();
$this->actingAs($this->user, 'api');
$this->connectTenantUser();
}
protected function connectTenantUser()
{
$tenantUser = new \App\Model\Master\User();
$tenantUser->id = $this->user->id;
$tenantUser->name = $this->user->name;
$tenantUser->email = $this->user->email;
$tenantUser->save();
$this->userBranch($tenantUser);
}
protected function userBranch($tenantUser)
{
$branch = $this->createBranch();
$tenantUser->branches()->syncWithoutDetaching($branch->id);
foreach ($tenantUser->branches as $branch) {
$branch->pivot->is_default = true;
$branch->pivot->save();
}
}
protected function createBranch()
{
$branch = new Branch();
$branch->name = 'Test branch';
$branch->save();
return $branch;
}
protected function logRequestTime()
{
$start = LARAVEL_START;
$end = microtime(true);
$diff = $end - $start;
Log::channel('testing')->info('['.app('request')->method().'] '
.app('request')->url()
.' '
.$diff);
}
protected function createSampleChartAccountType()
{
$chartOfAccountType = new ChartOfAccountType();
$chartOfAccountType->name = 'Test Type';
$chartOfAccountType->alias = 'Testing one';
$chartOfAccountType->is_debit = true;
$chartOfAccountType->save();
$this->createSampleChartAccount($chartOfAccountType);
}
protected function createSampleChartAccount($chartOfAccountType)
{
$chartOfAccount = new ChartOfAccount();
$chartOfAccount->type_id = $chartOfAccountType->id;
$chartOfAccount->number = 'TEST01';
$chartOfAccount->name = 'Testing Chart';
$chartOfAccount->alias = 'Testing chart';
$chartOfAccount->save();
$this->account = $chartOfAccount;
}
protected function setRole()
{
$role = \App\Model\Auth\Role::createIfNotExists('super admin');
$hasRole = new \App\Model\Auth\ModelHasRole();
$hasRole->role_id = $role->id;
$hasRole->model_type = 'App\Model\Master\User';
$hasRole->model_id = $this->user->id;
$hasRole->save();
}
protected function setPermission()
{
$permission = \App\Model\Auth\Permission::createIfNotExists('read pin point sales visitation form');
$hasPermission = new \App\Model\Auth\ModelHasPermission();
$hasPermission->permission_id = $permission->id;
$hasPermission->model_type = 'App\Model\Master\User';
$hasPermission->model_id = $this->user->id;
$hasPermission->save();
}
protected function setProject()
{
$package = new Package();
$package->code = 'code';
$package->name = 'test';
$package->description = 'test';
$package->max_user = 3;
$package->price = 3000000;
$package->price_per_user = 50000;
$package->is_active = true;
$package->save();
$project = new Project();
$project->code = 'test';
$project->name = 'testing';
$project->total_user = 10;
$project->timezone = 'asia/jakarta';
$project->owner_id = $this->user->id;
$project->invitation_code_enabled = 0;
$project->is_generated = 0;
$project->package_id = $package->id;
$project->expired_date = date('Y-m-d H:i:s');
$project->save();
}
}