Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Oct 18, 2024
2 parents 684ce0b + baefebc commit 614ca03
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
Expand Down
6 changes: 3 additions & 3 deletions src/BartenderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace DirectoryTree\Bartender;

use Illuminate\Http\RedirectResponse;
use Laravel\Socialite\Facades\Socialite;
use DirectoryTree\Bartender\Controllers\AuthController;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Route;
use Laravel\Socialite\Facades\Socialite;

class BartenderManager
{
Expand Down Expand Up @@ -39,7 +39,7 @@ public function getUserModel(): string
/**
* Register a new driver handler to serve.
*
* @param class-string $handler
* @param class-string $handler
*/
public function serve(string $driver, string $handler = UserProviderHandler::class): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace DirectoryTree\Bartender\Controllers;

use Illuminate\Http\RedirectResponse;
use DirectoryTree\Bartender\Facades\Bartender;
use Illuminate\Http\RedirectResponse;

class AuthController
{
Expand Down
3 changes: 1 addition & 2 deletions src/Events/UserAuthenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ class UserAuthenticated
*/
public function __construct(
public Authenticatable $user
) {
}
) {}
}
2 changes: 1 addition & 1 deletion src/Facades/Bartender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace DirectoryTree\Bartender\Facades;

use Illuminate\Support\Facades\Facade;
use DirectoryTree\Bartender\BartenderManager;
use Illuminate\Support\Facades\Facade;

/**
* @method static void routes()
Expand Down
2 changes: 1 addition & 1 deletion src/ProviderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace DirectoryTree\Bartender;

use Laravel\Socialite\Contracts\Provider;
use Illuminate\Http\RedirectResponse;
use Laravel\Socialite\Contracts\Provider;

interface ProviderHandler
{
Expand Down
2 changes: 1 addition & 1 deletion src/ProviderRedirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ public function unableToCreateUser(Exception $e, SocialiteUser $user, string $dr
* Handle when the user has been successfully authenticated.
*/
public function userAuthenticated(Authenticatable $user, SocialiteUser $socialite, string $driver): RedirectResponse;
}
}
2 changes: 1 addition & 1 deletion src/ProviderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ public function exists(string $driver, SocialiteUser $user): bool;
* Update or create the socialite user.
*/
public function updateOrCreate(string $driver, SocialiteUser $user): Authenticatable;
}
}
5 changes: 2 additions & 3 deletions src/UserProviderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class UserProviderHandler implements ProviderHandler
public function __construct(
protected ProviderRepository $users,
protected ProviderRedirector $redirector,
) {
}
) {}

/**
* Handle redirecting the user to the OAuth provider.
Expand Down Expand Up @@ -50,4 +49,4 @@ public function callback(Provider $provider, string $driver): RedirectResponse

return $this->redirector->userAuthenticated($user, $socialite, $driver);
}
}
}
16 changes: 8 additions & 8 deletions src/UserProviderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public function updateOrCreate(string $driver, SocialiteUser $user): Authenticat
'provider_id' => $user->id,
'password' => $eloquent->password ?? $this->hash($this->getNewPassword()),
],
$this->isUsingSoftDeletes($model)
? ['deleted_at' => null]
: [],
$this->isVerifyingEmails($model)
? ['email_verified_at' => $eloquent->email_verified_at ?? now()]
: []
$this->isUsingSoftDeletes($model)
? ['deleted_at' => null]
: [],
$this->isVerifyingEmails($model)
? ['email_verified_at' => $eloquent->email_verified_at ?? now()]
: []
)
)->save();

Expand All @@ -80,7 +80,7 @@ protected function getNewPassword(): string
/**
* Get a new user query instance.
*
* @param class-string $model
* @param class-string $model
*/
protected function newUserQuery(string $model): Builder
{
Expand All @@ -106,4 +106,4 @@ protected function isVerifyingEmails(string $model): bool
{
return in_array(MustVerifyEmail::class, class_uses_recursive($model));
}
}
}
6 changes: 3 additions & 3 deletions tests/BartenderManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
});

it('can register handlers', function () {
$manager = new BartenderManager();
$manager = new BartenderManager;

$manager->serve('foo', stdClass::class);

expect($manager->handlers())->toBe(['foo' => stdClass::class]);
});

it('returns new user model', function () {
$manager = new BartenderManager();
$manager = new BartenderManager;

$manager->setUserModel(User::class);

expect($manager->getUserModel())->toBe(User::class);
});

it('registers routes', function () {
$manager = new BartenderManager();
$manager = new BartenderManager;

$manager->routes();

Expand Down
6 changes: 2 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace DirectoryTree\Bartender\Tests;

use Orchestra\Testbench\Attributes\WithEnv;
use Orchestra\Testbench\Attributes\WithMigration;
use Orchestra\Testbench\TestCase as BaseTestCase;
use DirectoryTree\Bartender\BartenderServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;

use function Orchestra\Testbench\laravel_migration_path;
use function Orchestra\Testbench\workbench_path;

class TestCase extends BaseTestCase
{
Expand Down
20 changes: 9 additions & 11 deletions tests/UserProviderHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?php

use DirectoryTree\Bartender\Events\UserAuthenticated;
use DirectoryTree\Bartender\ProviderRepository;
use DirectoryTree\Bartender\ProviderRedirector;
use DirectoryTree\Bartender\ProviderRepository;
use DirectoryTree\Bartender\Tests\User;
use Illuminate\Support\Facades\Event;
use DirectoryTree\Bartender\UserProviderHandler;
use Laravel\Socialite\Contracts\Provider;
use Laravel\Socialite\Two\User as SocialiteUser;
use DirectoryTree\Bartender\UserProviderHandler;

it('can redirect to provider', function () {
$provider = mock(Provider::class);
Expand All @@ -31,7 +29,7 @@

it('can handle when user already exists', function () {
$provider = $this->mock(Provider::class);
$provider->shouldReceive('user')->once()->andReturn(new SocialiteUser());
$provider->shouldReceive('user')->once()->andReturn(new SocialiteUser);

$this->mock(ProviderRepository::class, function ($mock) {
$mock->shouldReceive('exists')->once()->andReturn(true);
Expand All @@ -45,12 +43,12 @@
});

it('can handle exception when unable to create or update user', function () {
$socialite = new SocialiteUser();
$socialite = new SocialiteUser;

$provider = $this->mock(Provider::class);
$provider->shouldReceive('user')->once()->andReturn($socialite);

$this->mock(ProviderRepository::class, function ($mock) use ($socialite) {
$this->mock(ProviderRepository::class, function ($mock) {
$mock->shouldReceive('exists')->once()->andReturn(false);
$mock->shouldReceive('updateOrCreate')->once()->andThrow(Exception::class);
});
Expand All @@ -63,8 +61,8 @@
});

it('can authenticate user', function () {
$user = new User();
$socialite = new SocialiteUser();
$user = new User;
$socialite = new SocialiteUser;

$provider = $this->mock(Provider::class);
$provider->shouldReceive('user')->once()->andReturn($socialite);
Expand All @@ -74,9 +72,9 @@
$mock->shouldReceive('updateOrCreate')->once()->andReturn($user);
});

$this->mock(ProviderRedirector::class, function ($mock) use ($socialite) {
$this->mock(ProviderRedirector::class, function ($mock) {
$mock->shouldReceive('userAuthenticated')->once()->andReturn(redirect('/'));
});

app(UserProviderHandler::class)->callback($provider, 'foo');
});
});
12 changes: 6 additions & 6 deletions tests/UserProviderRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'password' => Hash::make('password'),
]);

$socialite = tap(new SocialiteUser(), function ($user) {
$socialite = tap(new SocialiteUser, function ($user) {
$user->id = '1';
$user->email = '[email protected]';
});
Expand All @@ -35,7 +35,7 @@
'password' => Hash::make('password'),
]);

$socialite = tap(new SocialiteUser(), function ($user) {
$socialite = tap(new SocialiteUser, function ($user) {
$user->id = '1';
$user->email = '[email protected]';
});
Expand All @@ -44,7 +44,7 @@
});

it('creates new user', function () {
$socialite = tap(new SocialiteUser(), function (SocialiteUser $user) {
$socialite = tap(new SocialiteUser, function (SocialiteUser $user) {
$user->id = '1';
$user->name = 'foo';
$user->email = '[email protected]';
Expand All @@ -68,7 +68,7 @@
'password' => 'password',
]);

$socialite = tap(new SocialiteUser(), function (SocialiteUser $user) {
$socialite = tap(new SocialiteUser, function (SocialiteUser $user) {
$user->id = '1';
$user->name = 'foo';
$user->email = '[email protected]';
Expand All @@ -92,7 +92,7 @@

$this->expectException(QueryException::class);

$socialite = tap(new SocialiteUser(), function (SocialiteUser $user) {
$socialite = tap(new SocialiteUser, function (SocialiteUser $user) {
$user->id = '1';
$user->name = 'foo';
$user->email = '[email protected]';
Expand All @@ -110,7 +110,7 @@
'password' => 'password',
]);

$socialite = tap(new SocialiteUser(), function (SocialiteUser $user) {
$socialite = tap(new SocialiteUser, function (SocialiteUser $user) {
$user->id = '123';
$user->name = 'bar';
$user->email = '[email protected]';
Expand Down

0 comments on commit 614ca03

Please sign in to comment.