前提
Laravelにて、以下に示すクラスとそれに対するユニットテストを実行したのですが、
PHP Fatal error: Uncaught Error: Class "PHPUnit\TextUI\Configuration\Registry" not found in Standard input code:107
というエラーが出て困っています。解決策を教えてください。
該当のソースコード
ユニットテスト対象のクラス
php
1<?php 2// Tweetクラスに直接依存しないためのクラス(テストの観点で良い) 3 4namespace App\Services; 5 6use App\Models\Tweet; 7use Carbon\Carbon; 8use App\Models\Image; 9use Illuminate\Support\Facades\DB; 10use Illuminate\Support\Facades\Storage; 11 12class TweetService 13{ 14 public function getTweets() 15 { 16 return Tweet::with('images')->orderBy('created_at', 'DESC')->get(); 17 } 18 19 // 自分のtweetかどうかチェックするメソッド 20 public function checkOwnTweet(int $userId, int $tweetId): bool 21 { 22 $tweet = Tweet::where('id', $tweetId)->first(); 23 if (!$tweet) { 24 return false; 25 } 26 27 return $tweet->user_id === $userId; 28 } 29 30 public function countYesterdayTweets(): int 31 { 32 return Tweet::whereDate('created_at', '>=', Carbon::yesterday()->toDateTimeString()) 33 ->whereDate('created_at', '<', Carbon::today()->toDateTimeString()) 34 ->count(); 35 } 36 37 public function saveTweet(int $userId, string $content, array $images) 38 { 39 DB::transaction(function () use ($userId, $content, $images) { 40 $tweet = new Tweet; 41 $tweet->user_id = $userId; 42 $tweet->content = $content; 43 $tweet->save(); 44 foreach ($images as $image) { 45 Storage::putFile('public/images', $image); 46 $imageModel = new Image; 47 $imageModel->name = $image->hashName(); 48 $imageModel->save(); 49 $tweet->images()->attach($imageModel->id); 50 } 51 }); 52 } 53 54 public function deleteTweet(int $tweetId) 55 { 56 DB::transaction(function () use ($tweetId){ 57 $tweet = Tweet::where('id', $tweetId)->firstOrFail(); 58 $tweet->images()->each(function ($image) use ($tweet){ 59 $filePath = 'public/images/' . $image->name; 60 if(Storage::exists($filePath)){ 61 Storage::delete($filePath); 62 } 63 $tweet->images()->detach($image->id); 64 $image->delete(); 65 }); 66 67 $tweet->delete(); 68 }); 69 } 70}
ユニットテスト
php
1<?php 2 3namespace Tests\Unit\Services; 4 5use PHPUnit\Framework\TestCase; 6use App\Services\TweetService; 7use Mockery; 8 9class TweetServiceTest extends TestCase 10{ 11 /** 12 * A basic unit test example. 13 * @runInSeparateProcess 14 */ 15 public function test_check_own_tweet(): void 16 { 17 $tweetService = new TweetService; 18 19 $mock = Mockery::mock('alias:App\Models\Tweet'); 20 $mock->shouldReceive('where->first')->andReturn((object)[ 21 'id' => 1, 22 'user_id' => 1 23 ]); 24 25 $result = $tweetService->checkOwnTweet(1, 1); 26 $this->assertTrue($result); 27 28 $result = $tweetService->checkOwnTweet(2, 1); 29 $this->assertFalse($result); 30 } 31}
発生している問題・エラーメッセージ
以下、テストを実行するコマンドを入力するところから始まります。
abc@LAPTOP-M4DE6JIK:~/example-app$ sail test tests/Unit/Services/TweetServiceTest.php FAIL Tests\Unit\Services\TweetServiceTest ⨯ check own tweet ────────────────────────────────────────────────────────────────────────────────────────────────────────────── FAILED Tests\Unit\Services\TweetServiceTest > check own tweet Exception PHP Fatal error: Uncaught Error: Class "PHPUnit\TextUI\Configuration\Registry" not found in Standard input code:107 Stack trace: #0 {main} thrown in Standard input code on line 107 Fatal error: Uncaught Error: Class "PHPUnit\TextUI\Configuration\Registry" not found in Standard input code:107 Stack trace: #0 {main} thrown in Standard input code on line 107 Tests: 1 failed (0 assertions) Duration: 0.14s
試したこと
ChatGPTに聞きましたが、具体的に何をすればよいかわかりませんでした。
ChatGPTの回答
1 2エラーメッセージを見る限り、PHPUnitの設定に問題がある可能性があります。エラーはPHPUnit\TextUI\Configuration\Registryクラスが見つからないというものです。 3 4PHPUnitのバージョンが古くなっているか、PHPUnitの依存関係が正しく解決されていない可能性があります。 5 6まず、composer.jsonファイルを確認して、PHPUnitのバージョンが適切に指定されているかを確認してください。次に、プロジェクトのルートディレクトリで以下のコマンドを実行して、PHPUnitの依存関係を更新します。 7 8sql 9Copy code 10composer update --lock 11これによって依存関係が最新の状態に更新され、エラーが解決するかもしれません。 12 13また、PHPUnitのバージョンが最新であることを確認することも重要です。.envファイルやphpunit.xmlファイルに関連する設定が正しく行われているかも確認してください。 14 15これらの手順を試してもエラーが解決しない場合は、PHPUnitのインストールを確認し、必要に応じて再インストールすることをおすすめします。
補足情報(FW/ツールのバージョンなど)
PHP8.1
laravelFramework10.10

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。