I'm using Laravel 7 and I'm unable to do integration tests with Socialite. I have seen that there many examples of getting it to work on Laravel 5.x but can't find any for version 7.
The test fails because the mocked user info object is returning null variables.
Controller:
public function callback($provider)
{
$getInfo = Socialite::driver($provider)->user();
\Illuminate\Support\Facades\Log::debug(gettype($getInfo->name));
$user = $this->createUser($getInfo, $provider);
auth()->login($user);
return redirect()->to('/home');
}
Route:
Route::get('/callback/{provider}', 'SocialController@callback')->name('SocialiteCallback');
Test:
use RefreshDatabase;
/**
* @test
*/
public function testSocialiteTwitterLogin() {
$abstractUser = Mockery::mock('Laravel\Socialite\Two\User');
$abstractUser
->shouldReceive('getId')
->andReturn(rand())
->shouldReceive('getNickName')
->andReturn(uniqid())
->shouldReceive('getName')
->andReturn(uniqid())
->shouldReceive('getEmail')
->andReturn(uniqid() . '@gmail.com')
->shouldReceive('getAvatar')
->andReturn('https://en.gravatar.com/userimage');
$provider = Mockery::mock('Laravel\Socialite\Contracts\Provider');
$provider->shouldReceive('user')
->andReturn($abstractUser);
Socialite::shouldReceive('driver')
->with('twitter')
->andReturn($provider);
//Socialite::shouldReceive('driver->user')->andReturn($abstractUser);
$this->get(route('SocialiteCallback', ['provider' => 'twitter']))
->assertStatus(302)
->assertRedirect(route('home'));
}
The actual fail error log is:
[2020-06-16 16:44:48] testing.ERROR: SQLSTATE[HY000]: General error: 1 no such table: users (SQL: select * from "users" where "provider_id" is null limit 1) {"exception":"[object] (Illuminate\Database\QueryException(code: HY000): SQLSTATE[HY000]: General error: 1 no such table: users (SQL: select * from "users" where "provider_id" is null limit 1) at /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:671)
My other tests for user authentication with Database Factory work fine so I don't know why the above doesn't work.