forked from portabilis/i-educar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceTestCase.php
132 lines (96 loc) · 3.13 KB
/
ResourceTestCase.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
<?php
namespace Tests;
use Database\Factories\LegacyUserFactory;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Testing\TestResponse;
class ResourceTestCase extends TestCase
{
use DatabaseTransactions;
protected string $uri = '/';
protected string $model;
protected string $factory;
protected int $indexFactoryCount = 20;
protected int $indexJsonCount = 15;
protected function setUp(): void
{
parent::setUp();
$this->actingAs(LegacyUserFactory::new()->admin()->create());
}
protected function getUri(array $params = [], array $query = []): string
{
$uri = rtrim($this->uri, '/') . '/' . implode('/', $params);
$query = count($query) ? '?' . http_build_query($query) : '';
return $uri . $query;
}
protected function newFactory()
{
return $this->app->get($this->factory);
}
public function index(): TestResponse
{
$response = $this->get($this->getUri());
$response->assertOk();
$response->assertJsonCount(0, 'data');
$this->newFactory()->count($this->indexFactoryCount)->create();
$response = $this->get($this->getUri());
$response->assertOk();
$response->assertJsonCount($this->indexJsonCount, 'data');
$this->assertDatabaseCount($this->model, $this->indexFactoryCount);
return $response;
}
public function store(): TestResponse
{
$model = $this->newFactory()->make();
$response = $this->post(
$this->getUri(),
$model->toArray()
);
$response->assertCreated();
$response->assertJson([
'data' => $model->toArray(),
]);
$this->assertDatabaseHas($this->model, $model->getAttributes());
return $response;
}
public function show(): TestResponse
{
$model = $this->newFactory()->create();
$response = $this->get(
$this->getUri([$model->getKey()])
);
$response->assertOk();
$response->assertJson([
'data' => $model->toArray(),
]);
return $response;
}
public function update(): TestResponse
{
$model = $this->newFactory()->create();
$updatedModel = $this->newFactory()->make();
$response = $this->patch(
$this->getUri([$model->getKey()]),
$updatedModel->toArray()
);
$response->assertOk();
$response->assertJson([
'data' => $updatedModel->toArray(),
]);
$this->assertDatabaseMissing($this->model, $model->getAttributes());
$this->assertDatabaseHas($this->model, $updatedModel->getAttributes());
return $response;
}
public function destroy(): TestResponse
{
$model = $this->newFactory()->create();
$response = $this->delete(
$this->getUri([$model->getKey()])
);
$response->assertOk();
$response->assertJson([
'data' => $model->toArray(),
]);
$this->assertDatabaseMissing($this->model, $model->getAttributes());
return $response;
}
}