Created
July 10, 2020 01:48
-
-
Save agm1984/14b6ef4dd04ce6597f9ca6602f5a22de to your computer and use it in GitHub Desktop.
This is not a full set of tests, but it should be enough to see one way to use Mockery. See this repo, where I originally found this code, for more information: https://github.com/cretueusebiu/laravel-vue-spa/blob/master/app/OAuthProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature; | |
use Illuminate\Foundation\Testing\DatabaseTransactions; | |
use App\OauthProvider; | |
use App\User; | |
use Illuminate\Support\Str; | |
use Illuminate\Testing\TestResponse; | |
use Laravel\Socialite\Facades\Socialite; | |
use Laravel\Socialite\Two\User as SocialiteUser; | |
use Mockery as m; | |
use PHPUnit\Framework\Assert as PHPUnit; | |
use Tests\TestCase; | |
class OAuthGitHubTest extends TestCase | |
{ | |
use DatabaseTransactions; | |
public function setUp() : void | |
{ | |
parent::setUp(); | |
TestResponse::macro('assertText', function ($text) { | |
PHPUnit::assertTrue(Str::contains($this->getContent(), $text), "Expected text [{$text}] not found."); | |
return $this; | |
}); | |
TestResponse::macro('assertTextMissing', function ($text) { | |
PHPUnit::assertFalse(Str::contains($this->getContent(), $text), "Expected missing text [{$text}] found."); | |
return $this; | |
}); | |
} | |
public function tearDown() : void | |
{ | |
parent::tearDown(); | |
m::close(); | |
} | |
/** | |
* Mocks the Socialite API that communicates externally. | |
* | |
* @param string $provider | |
* @param array $user | |
* @return void | |
*/ | |
protected function mockSocialite(string $provider, ?array $user = null) | |
{ | |
$mock = Socialite::shouldReceive('stateless') | |
->andReturn(m::self()) | |
->shouldReceive('driver') | |
->with($provider) | |
->andReturn(m::self()); | |
if ($user) { | |
$mock->shouldReceive('user') | |
->andReturn((new SocialiteUser)->setRaw($user)->map($user)); | |
} else { | |
$mock->shouldReceive('redirect') | |
->andReturn(redirect('https://url-to-provider')); | |
} | |
} | |
/** @test */ | |
public function it_redirects_to_github() | |
{ | |
$this->mockSocialite('github'); | |
$this->postJson(route('oauth.redirect', 'github')) | |
->assertStatus(200) | |
->assertJson(['url' => 'https://url-to-provider']); | |
} | |
/** @test */ | |
public function it_can_create_new_user_from_github_identity() | |
{ | |
$github_identity = [ | |
'id' => '123', | |
'name' => 'New GitHub User', | |
'email' => '[email protected]', | |
'token' => 'access-token', | |
'refreshToken' => 'refresh-token', | |
]; | |
$this->mockSocialite('github', $github_identity); | |
$this->withoutExceptionHandling(); | |
$this->get(route('oauth.callback', 'github')) | |
->assertText('user') | |
->assertSuccessful(); | |
$this->assertDatabaseHas('users', [ | |
'name' => $github_identity['name'], | |
'email' => $github_identity['email'], | |
]); | |
$userId = User::query()->firstWhere('email', $github_identity['email'])->id; | |
$this->assertDatabaseHas('oauth_providers', [ | |
'user_id' => $userId, | |
'provider' => 'github', | |
'provider_user_id' => $github_identity['id'], | |
'access_token' => $github_identity['token'], | |
'refresh_token' => $github_identity['refreshToken'], | |
]); | |
$providerCount = OAuthProvider::query()->where('user_id', $userId)->where('provider', 'github')->count(); | |
$this->assertEquals($providerCount, 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment