質問編集履歴
1
全体的に修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
LaravelでphpUnitで
|
1
|
+
LaravelでphpUnitでsetUp()追加するとコンパイルエラーが出る
|
test
CHANGED
@@ -1,157 +1,267 @@
|
|
1
|
-
LaravelでphpUnitを
|
1
|
+
LaravelでphpUnitを書いています。
|
2
2
|
|
3
3
|
|
4
4
|
|
5
5
|
最初にsetUp()でDB関係のクラスをnewしてクラス変数を作って、
|
6
6
|
|
7
|
-
テストコードを実行、
|
8
|
-
|
9
|
-
|
7
|
+
$this->drinkRepository = new DrinkRepository();
|
10
|
-
|
8
|
+
|
11
|
-
|
9
|
+
$lastId = $this->drinkRepository->all()->last()->id;
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
|
15
|
-
ですが、setUp()を追加して
|
11
|
+
を一つにまとめたいのですが、setUp()を追加してもコンパイルエラーに失敗して困ってます。
|
12
|
+
|
13
|
+
|
14
|
+
|
16
|
-
|
15
|
+
詳しい方、よろしくお願いします。
|
16
|
+
|
17
|
+
|
18
|
+
|
17
|
-
|
19
|
+
##ソースコード
|
18
|
-
|
19
|
-
There was 1 error:
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
1) Tests\Unit\Services\Drink\DrinkServiceBuyTest::test_buy_should_throws_no_stock_exception_if_stock_0_or_less
|
24
|
-
|
25
|
-
Illuminate\Container\EntryNotFoundException: Illuminate\Database\ConnectionInterface
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
/Users/tom/dev/nextat/src/vendor/laravel/framework/src/Illuminate/Container/Container.php:666
|
30
|
-
|
31
|
-
/Users/tom/dev/nextat/src/app/Repositories/DrinkRepository.php:29
|
32
|
-
|
33
|
-
/Users/tom/dev/nextat/src/tests/Unit/Services/Drink/DrinkServiceBuyTest.php:32
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
Caused by
|
38
|
-
|
39
|
-
Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Database\ConnectionInterface] is not instantiable.
|
40
|
-
|
41
|
-
|
42
20
|
|
43
21
|
```
|
44
22
|
|
45
|
-
|
23
|
+
<?php
|
24
|
+
|
25
|
+
|
26
|
+
|
46
|
-
|
27
|
+
declare(strict_types=1);
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
namespace Tests\Unit\Services\Drink;
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
use App\Exceptions\Drink\Buy\GuiltyException;
|
36
|
+
|
37
|
+
use App\Exceptions\Drink\Buy\NoStockException;
|
38
|
+
|
39
|
+
use App\Exceptions\Drink\Buy\NotEnoughMoneyException;
|
40
|
+
|
41
|
+
use App\Exceptions\Drink\NotFoundException;
|
42
|
+
|
43
|
+
use App\Models\User;
|
44
|
+
|
45
|
+
use App\Services\Drink\DrinkService;
|
46
|
+
|
47
|
+
use App\Models\Drink;
|
48
|
+
|
49
|
+
use App\Repositories\DrinkRepository;
|
50
|
+
|
51
|
+
use App\Repositories\UserRepository;
|
52
|
+
|
47
|
-
|
53
|
+
use Tests\TestCase;
|
54
|
+
|
55
|
+
|
56
|
+
|
48
|
-
|
57
|
+
class DrinkServiceBuyTest extends TestCase
|
58
|
+
|
49
|
-
|
59
|
+
{
|
50
|
-
|
60
|
+
|
51
|
-
|
61
|
+
/**
|
62
|
+
|
63
|
+
* @testdox 在庫が0以下の場合, NoStockExceptionをスロー
|
64
|
+
|
65
|
+
* @throws NotFoundException
|
66
|
+
|
67
|
+
*/
|
68
|
+
|
69
|
+
public function test_buy_should_throws_no_stock_exception_if_stock_0_or_less(): void
|
70
|
+
|
71
|
+
{
|
72
|
+
|
73
|
+
$this->expectException(NoStockException::class);//期待する例外
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
$this->drinkRepository = new DrinkRepository();//外だしして各メソッドの無駄を無くしたい
|
78
|
+
|
79
|
+
$lastId = $this->drinkRepository->all()->last()->id;//外だしして各メソッドの無駄を無くしたい
|
80
|
+
|
81
|
+
$this->drinkRepository->newDrink(
|
82
|
+
|
83
|
+
new Drink($lastId+1, "テストドリンク", 100, 0, false)//テストデータ挿入
|
84
|
+
|
85
|
+
);
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
$user = new User(1, 'テストユーザー', 20, 100);
|
90
|
+
|
91
|
+
(new DrinkService())->buy($user, $lastId+1);
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
/**
|
98
|
+
|
99
|
+
* @testdox 返り値がnullだった場合, NotFoundExceptionをスロー
|
100
|
+
|
101
|
+
* @throws NotFoundException
|
102
|
+
|
103
|
+
*/
|
104
|
+
|
105
|
+
public function test_buy_should_throws_not_found_exception_if_no_selected_drink(): void
|
106
|
+
|
107
|
+
{
|
108
|
+
|
109
|
+
$this->expectException(NotFoundException::class);//期待する例外
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
$this->drinkRepository = new DrinkRepository();
|
114
|
+
|
115
|
+
$lastId = $this->drinkRepository->all()->last()->id;
|
116
|
+
|
117
|
+
$this->drinkRepository->newDrink(
|
118
|
+
|
119
|
+
new Drink($lastId+1, "テストドリンク", 100, 1, false)//テストデータ挿入
|
120
|
+
|
121
|
+
);
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
$user = new User(1, 'テストユーザー', 20, 100);
|
126
|
+
|
127
|
+
(new DrinkService())->buy($user, $lastId+2);
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
/**
|
134
|
+
|
135
|
+
* @testdox 所持金がドリンクの値段を下回っていた場合, NotEnoughMoneyExceptionをスロー
|
136
|
+
|
137
|
+
* @throws NotEnoughMoneyException
|
138
|
+
|
139
|
+
*/
|
140
|
+
|
141
|
+
public function test_buy_should_throws_not_enough_money_exception_if_not_enough_money(): void
|
142
|
+
|
143
|
+
{
|
144
|
+
|
145
|
+
$this->expectException(NotEnoughMoneyException::class);//期待する例外
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
$this->drinkRepository = new DrinkRepository();
|
150
|
+
|
151
|
+
$lastId = $this->drinkRepository->all()->last()->id;
|
152
|
+
|
153
|
+
$this->drinkRepository->newDrink(
|
154
|
+
|
155
|
+
new Drink($lastId+1, "テストドリンク", 100, 1, false)//テストデータ挿入
|
156
|
+
|
157
|
+
);
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
$user = new User(1, 'テストユーザー', 20, 99);
|
162
|
+
|
163
|
+
(new DrinkService())->buy($user, $lastId+1);
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
/**
|
170
|
+
|
171
|
+
* @testdox 20歳未満のユーザーがアルコール飲料を購入しようとした場合, GuiltyExceptionをスロー
|
172
|
+
|
173
|
+
* @throws GuiltyException
|
174
|
+
|
175
|
+
*/
|
176
|
+
|
177
|
+
public function test_buy_should_throws_guilty_exception_if_guilty_purchase(): void
|
178
|
+
|
179
|
+
{
|
180
|
+
|
181
|
+
$this->expectException(GuiltyException::class);//期待する例外
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
$this->drinkRepository = new DrinkRepository();
|
186
|
+
|
187
|
+
$lastId = $this->drinkRepository->all()->last()->id;
|
188
|
+
|
189
|
+
$this->drinkRepository->newDrink(
|
190
|
+
|
191
|
+
new Drink($lastId+1, "テストドリンク", 100, 1, true)//テストデータ挿入
|
192
|
+
|
193
|
+
);
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
$user = new User(1, 'テストユーザー', 19, 100);
|
198
|
+
|
199
|
+
(new DrinkService())->buy($user, $lastId+1);
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
/**
|
206
|
+
|
207
|
+
* @testdox どの例外もスルーされなかった場合, 正常に購入された結果をDrinkBoughtResultとして返す
|
208
|
+
|
209
|
+
* @throws GuiltyException
|
210
|
+
|
211
|
+
*/
|
212
|
+
|
213
|
+
public function test_buy_correctly(): void
|
214
|
+
|
215
|
+
{
|
216
|
+
|
217
|
+
$this->drinkRepository = new DrinkRepository();
|
218
|
+
|
219
|
+
$lastId = $this->drinkRepository->all()->last()->id;
|
220
|
+
|
221
|
+
$this->drinkRepository->newDrink(
|
222
|
+
|
223
|
+
new Drink($lastId+1, "テストドリンク", 100, 2, true)//テストデータ挿入
|
224
|
+
|
225
|
+
);
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
$user = new User(1, 'テストユーザー', 20, 2000);
|
230
|
+
|
231
|
+
$result = (new DrinkService())->buy($user, $lastId+1);
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
$this->assertEquals($result->drink->stock, 1);
|
236
|
+
|
237
|
+
$this->assertEquals($result->user->wallet, 1900);
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
/**
|
244
|
+
|
245
|
+
* @testdox テストデータ削除
|
246
|
+
|
247
|
+
*/
|
248
|
+
|
249
|
+
public function tearDown():void
|
250
|
+
|
251
|
+
{
|
252
|
+
|
253
|
+
$this->drinkRepository = new DrinkRepository();
|
254
|
+
|
255
|
+
$lastId = $this->drinkRepository->all()->last()->id;
|
256
|
+
|
257
|
+
$this->drinkRepository->deleteDrink($lastId);
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
52
266
|
|
53
267
|
```
|
54
|
-
|
55
|
-
<?php
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
declare(strict_types=1);
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
namespace Tests\Unit\Services\Drink;
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
use App\Exceptions\Drink\Buy\NoStockException;
|
68
|
-
|
69
|
-
use App\Exceptions\Drink\NotFoundException;
|
70
|
-
|
71
|
-
use App\Models\User;
|
72
|
-
|
73
|
-
use App\Services\Drink\DrinkService;
|
74
|
-
|
75
|
-
use App\Models\Drink;
|
76
|
-
|
77
|
-
use App\Repositories\DrinkRepository;
|
78
|
-
|
79
|
-
use App\Repositories\UserRepository;
|
80
|
-
|
81
|
-
use Tests\TestCase;
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
class DrinkServiceBuyTest extends TestCase
|
86
|
-
|
87
|
-
{
|
88
|
-
|
89
|
-
public function setUp():void
|
90
|
-
|
91
|
-
{
|
92
|
-
|
93
|
-
//このメソッドがあるとエラー、消すとエラー起きない????????????????????
|
94
|
-
|
95
|
-
}
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
/**
|
100
|
-
|
101
|
-
* @testdox 在庫が0以下の場合, NoStockExceptionをスローすること
|
102
|
-
|
103
|
-
* @throws NotFoundException
|
104
|
-
|
105
|
-
*/
|
106
|
-
|
107
|
-
public function test_buy_should_throws_no_stock_exception_if_stock_0_or_less(): void
|
108
|
-
|
109
|
-
{
|
110
|
-
|
111
|
-
$this->drinkRepository = new DrinkRepository();//インスタンス化
|
112
|
-
|
113
|
-
$service = new DrinkService();//インスタンス化
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
$lastId = $this->drinkRepository->all()->last()->id;
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
$user = new User(1, 'テストユーザー', 20, 1000);
|
122
|
-
|
123
|
-
$newDrink = new Drink($lastId + 1, "TestJuice", 100, 0, false);//DBに追加の準備
|
124
|
-
|
125
|
-
$this->drinkRepository->newDrink($newDrink);//DBに追加
|
126
|
-
|
127
|
-
$this->expectException(NoStockException::class);//本コードで例外が出た確認
|
128
|
-
|
129
|
-
$service->buy($user, $lastId+1);//本コード実行
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
public function tearDown():void
|
138
|
-
|
139
|
-
{
|
140
|
-
|
141
|
-
$this->drinkRepository = new DrinkRepository();
|
142
|
-
|
143
|
-
$lastId = $this->drinkRepository->all()->last()->id;
|
144
|
-
|
145
|
-
$this->drinkRepository->deleteDrink($lastId);//テスト用データ削除
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
}
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
```
|