-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTestCase.php
More file actions
91 lines (67 loc) · 2.54 KB
/
Copy pathTestCase.php
File metadata and controls
91 lines (67 loc) · 2.54 KB
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
<?php
namespace Tests;
use App\Models\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Http\Response;
use Illuminate\Testing\TestResponse;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected $base_route = null;
protected $base_model = null;
protected function setBaseRoute($route)
{
$this->base_route = $route;
}
protected function setBaseModel($model)
{
$this->base_model = $model;
}
protected function raw($class, $attributes = [], $times = null)
{
return $class::factory()->count($times)->raw($attributes);
}
protected function rawForUser(User $user, $class, $attributes = [], $times = null)
{
$attributes['user_id'] = $user->id;
return $class::factory()->count($times)->raw($attributes);
}
protected function create($class, $attributes = [], $times = null)
{
return $class::factory()->count($times)->create($attributes);
}
protected function createForUser(User $user, $class, $attributes = [], $times = null)
{
return $class::factory()->for($user)->count($times)->create($attributes);
}
protected function assertCreateForUser(User $user, $attributes = [], $model = '', $route = ''): TestResponse
{
$route = $this->base_route ? "{$this->base_route}.store" : $route;
$model = $this->base_model ?? $model;
$attributes = $this->rawForUser($user, $model, $attributes);
$response = $this->actingAs($user)->postJson(route($route), $attributes);
$response->assertRedirect($this->base_route ? "{$this->base_route}" : $route);
$model = new $model();
$this->assertDatabaseHas($model->getTable(), $attributes);
return $response;
}
protected function assertDestroyWithUser(User $user, $model = '', $route = '')
{
$route = $this->base_route ? "{$this->base_route}.destroy" : $route;
$model = $this->base_model ?? $model;
$model = $this->createForUser($user, $model);
$response = $this->actingAs($user)->deleteJson(route($route, $model->id));
$this->assertDatabaseMissing($model->getTable(), ['id' => $model->id]);
return $response;
}
protected function assertUserNotAuthorized(TestResponse $response)
{
$this->assertThat(
$response->status(),
$this->logicalOr(
$this->equalTo(Response::HTTP_UNAUTHORIZED),
$this->equalTo(Response::HTTP_FORBIDDEN)
)
);
}
}