laravel passportを利用してapi認証を実装したwebアプリケーションを作成しております。
テストの際にユーザーをログアウトさせる方法がわからず、お力添え頂きたく思います。
test.php
1public function test() 2 { 3 $headerArray = testLoginGetApiHeader($this,2,"testPassword"); 4 $response = $this->get(route('test'),$headerArray); 5 $response->assertStatus(200); 6 $response->assertJson([ 7 "status" => "success", 8 "data" => [ 9 "user_id" => "2", 10 ] 11 ]); 12 13 Auth::logout(); 14 15 $headerArray = testLoginGetApiHeader($this,3,"testPassword"); 16 $response = $this->get(route('test'),$headerArray); 17 $response->assertStatus(200); 18 $response->assertJson([ 19 "status" => "success", 20 "data" => [ 21 "user_id" => "3", 22 ] 23 ]); 24 }
testLoginGetApiHeader
1function testLoginGetApiHeader(\Tests\TestCase $testCase, $userId, $password) 2 { 3 $login = $testCase->post('/login', [ 4 "email" => User::find($userId)->email, 5 "password" => $password, 6 ]); 7 8 $accessToken = User::find($userId)->access_token; 9 $headerArray = ["Accept" => "application/json", "Authorization" => 'Bearer ' . $accessToken]; 10 11 return $headerArray; 12 }
実際のコードから不要な部分は省略していますが、以上が問題のあるコードです。
因みに上記のテストを走らせた際のエラーは以下になります。
BadMethodCallException: Method logout does not exist.
実際にアプリケーションからのユーザー切り替えは問題なく行えます。
テスト内で、ユーザーをログインさせた上でapiにアクセスせずにログアウトさせる場合は、
問題なくユーザー切り替えが行えました。
同じような実装経験のある方、どのように実装すべきかご教授頂きたいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。