teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

全体的に修正

2021/06/06 18:29

投稿

shoco
shoco

スコア170

title CHANGED
@@ -1,1 +1,1 @@
1
- LaravelでphpUnitで "Target [・・・] is not instantiable."のエラーが出る
1
+ LaravelでphpUnitでsetUp()追加するとコンパイルエラーが出る
body CHANGED
@@ -1,26 +1,10 @@
1
- LaravelでphpUnitを書いています。
1
+ LaravelでphpUnitを書いています。
2
2
 
3
3
  最初にsetUp()でDB関係のクラスをnewしてクラス変数を作って、
4
- テストコードを実行、
5
- 最後にテスト用データを削除するためにtearDown()でDBをdelete。
4
+ $this->drinkRepository = new DrinkRepository();
5
+ $lastId = $this->drinkRepository->all()->last()->id;
6
- といういうような流れ作ろうとしてます。
6
+ を一つにまめたのですが、setUp()追加してもコンパイルエラーに失敗して困ってます。
7
7
 
8
- ですが、setUp()を追加して実行したときだけエラーが出て困ってます。
9
- ```error
10
- There was 1 error:
11
-
12
- 1) Tests\Unit\Services\Drink\DrinkServiceBuyTest::test_buy_should_throws_no_stock_exception_if_stock_0_or_less
13
- Illuminate\Container\EntryNotFoundException: Illuminate\Database\ConnectionInterface
14
-
15
- /Users/tom/dev/nextat/src/vendor/laravel/framework/src/Illuminate/Container/Container.php:666
16
- /Users/tom/dev/nextat/src/app/Repositories/DrinkRepository.php:29
17
- /Users/tom/dev/nextat/src/tests/Unit/Services/Drink/DrinkServiceBuyTest.php:32
18
-
19
- Caused by
20
- Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Database\ConnectionInterface] is not instantiable.
21
-
22
- ```
23
-
24
8
  詳しい方、よろしくお願いします。
25
9
 
26
10
  ##ソースコード
@@ -31,7 +15,9 @@
31
15
 
32
16
  namespace Tests\Unit\Services\Drink;
33
17
 
18
+ use App\Exceptions\Drink\Buy\GuiltyException;
34
19
  use App\Exceptions\Drink\Buy\NoStockException;
20
+ use App\Exceptions\Drink\Buy\NotEnoughMoneyException;
35
21
  use App\Exceptions\Drink\NotFoundException;
36
22
  use App\Models\User;
37
23
  use App\Services\Drink\DrinkService;
@@ -42,38 +28,107 @@
42
28
 
43
29
  class DrinkServiceBuyTest extends TestCase
44
30
  {
31
+ /**
32
+ * @testdox 在庫が0以下の場合, NoStockExceptionをスロー
45
- public function setUp():void
33
+ * @throws NotFoundException
34
+ */
35
+ public function test_buy_should_throws_no_stock_exception_if_stock_0_or_less(): void
46
36
  {
37
+ $this->expectException(NoStockException::class);//期待する例外
38
+
39
+ $this->drinkRepository = new DrinkRepository();//外だしして各メソッドの無駄を無くしたい
40
+ $lastId = $this->drinkRepository->all()->last()->id;//外だしして各メソッドの無駄を無くしたい
41
+ $this->drinkRepository->newDrink(
42
+ new Drink($lastId+1, "テストドリンク", 100, 0, false)//テストデータ挿入
43
+ );
44
+
47
- //このメソッドがあるとエラ、消すとエラ起きない????????????????????
45
+ $user = new User(1, 'テストユ', 20, 100);
46
+ (new DrinkService())->buy($user, $lastId+1);
48
47
  }
49
48
 
50
49
  /**
51
- * @testdox 在庫0以下の場合, NoStockExceptionをスローすること
50
+ * @testdox 返り値nullだった場合, NotFoundExceptionをスロー
52
51
  * @throws NotFoundException
53
52
  */
54
- public function test_buy_should_throws_no_stock_exception_if_stock_0_or_less(): void
53
+ public function test_buy_should_throws_not_found_exception_if_no_selected_drink(): void
55
54
  {
56
- $this->drinkRepository = new DrinkRepository();//インスタンス化
55
+ $this->expectException(NotFoundException::class);//期待する例外
57
- $service = new DrinkService();//インスタンス化
58
56
 
57
+ $this->drinkRepository = new DrinkRepository();
59
58
  $lastId = $this->drinkRepository->all()->last()->id;
59
+ $this->drinkRepository->newDrink(
60
+ new Drink($lastId+1, "テストドリンク", 100, 1, false)//テストデータ挿入
61
+ );
60
62
 
61
- $user = new User(1, 'テストユーザー', 20, 1000);
63
+ $user = new User(1, 'テストユーザー', 20, 100);
62
- $newDrink = new Drink($lastId + 1, "TestJuice", 100, 0, false);//DBに追加の準備
63
- $this->drinkRepository->newDrink($newDrink);//DBに追加
64
- $this->expectException(NoStockException::class);//本コードで例外が出た確認
65
- $service->buy($user, $lastId+1);//本コード実行
64
+ (new DrinkService())->buy($user, $lastId+2);
65
+ }
66
66
 
67
+ /**
68
+ * @testdox 所持金がドリンクの値段を下回っていた場合, NotEnoughMoneyExceptionをスロー
69
+ * @throws NotEnoughMoneyException
70
+ */
71
+ public function test_buy_should_throws_not_enough_money_exception_if_not_enough_money(): void
72
+ {
73
+ $this->expectException(NotEnoughMoneyException::class);//期待する例外
74
+
75
+ $this->drinkRepository = new DrinkRepository();
76
+ $lastId = $this->drinkRepository->all()->last()->id;
77
+ $this->drinkRepository->newDrink(
78
+ new Drink($lastId+1, "テストドリンク", 100, 1, false)//テストデータ挿入
79
+ );
80
+
81
+ $user = new User(1, 'テストユーザー', 20, 99);
82
+ (new DrinkService())->buy($user, $lastId+1);
67
83
  }
68
84
 
85
+ /**
86
+ * @testdox 20歳未満のユーザーがアルコール飲料を購入しようとした場合, GuiltyExceptionをスロー
87
+ * @throws GuiltyException
88
+ */
89
+ public function test_buy_should_throws_guilty_exception_if_guilty_purchase(): void
90
+ {
91
+ $this->expectException(GuiltyException::class);//期待する例外
92
+
93
+ $this->drinkRepository = new DrinkRepository();
94
+ $lastId = $this->drinkRepository->all()->last()->id;
95
+ $this->drinkRepository->newDrink(
96
+ new Drink($lastId+1, "テストドリンク", 100, 1, true)//テストデータ挿入
97
+ );
98
+
99
+ $user = new User(1, 'テストユーザー', 19, 100);
100
+ (new DrinkService())->buy($user, $lastId+1);
101
+ }
102
+
103
+ /**
104
+ * @testdox どの例外もスルーされなかった場合, 正常に購入された結果をDrinkBoughtResultとして返す
105
+ * @throws GuiltyException
106
+ */
107
+ public function test_buy_correctly(): void
108
+ {
109
+ $this->drinkRepository = new DrinkRepository();
110
+ $lastId = $this->drinkRepository->all()->last()->id;
111
+ $this->drinkRepository->newDrink(
112
+ new Drink($lastId+1, "テストドリンク", 100, 2, true)//テストデータ挿入
113
+ );
114
+
115
+ $user = new User(1, 'テストユーザー', 20, 2000);
116
+ $result = (new DrinkService())->buy($user, $lastId+1);
117
+
118
+ $this->assertEquals($result->drink->stock, 1);
119
+ $this->assertEquals($result->user->wallet, 1900);
120
+ }
121
+
122
+ /**
123
+ * @testdox テストデータ削除
124
+ */
69
125
  public function tearDown():void
70
126
  {
71
127
  $this->drinkRepository = new DrinkRepository();
72
128
  $lastId = $this->drinkRepository->all()->last()->id;
73
- $this->drinkRepository->deleteDrink($lastId);//テスト用データ削除
129
+ $this->drinkRepository->deleteDrink($lastId);
130
+ }
74
131
 
75
- }
76
-
77
132
  }
78
133
 
79
134
  ```