diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5a4f408..733f9ee 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,35 +1,40 @@ name: CI -on: +on: push: - branches: - - "master" + paths: + - '**.php' + - '.github/workflows/run-tests.yml' + - 'phpunit.xml.dist' + - 'composer.json' + - 'composer.lock' pull_request: types: [opened, synchronize, reopened] jobs: phpunit: - name: Tests PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - runs-on: ubuntu-latest + name: Tests PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} + runs-on: ${{ matrix.os }} strategy: fail-fast: true matrix: - php: [7.3, 7.4, 8.0, 8.1] - laravel: ["8.*", "9.*"] - exclude: - - php: 7.3 - laravel: "9.*" - - php: 7.4 - laravel: "9.*" + os: [ubuntu-latest, windows-latest] + php: [8.3, 8.4] + laravel: ["10.*"] + stability: [prefer-lowest, prefer-stable] + include: + - laravel: 10.* + testbench: 8.* + carbon: ^2.63 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: fetch-depth: 1 - name: Cache dependencies - uses: actions/cache@v1 + uses: actions/cache@v4 with: path: ~/.composer/cache/files key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} @@ -38,12 +43,21 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} - extensions: dom, curl, libxml, mbstring, zip + extensions: dom, curl, libxml, mbstring, zip, fileinfo, sqlite, pdo_sqlite coverage: none + + - name: Setup problem matchers + run: | + echo "::add-matcher::${{ runner.tool_cache }}/php.json" + echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - name: Install dependencies run: | - composer require "illuminate/support:${{ matrix.laravel }}" --prefer-dist --no-interaction + composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.os == 'windows-latest' && '^^^' || '' }}${{ matrix.carbon }}" --no-interaction --no-update + composer update --${{ matrix.stability }} --prefer-dist --no-interaction + + - name: List Installed Dependencies + run: composer show -D - name: Run Testsuite run: ./vendor/bin/phpunit diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml index 425495a..d6fc7e9 100644 --- a/.github/workflows/code-style.yml +++ b/.github/workflows/code-style.yml @@ -1,44 +1,28 @@ name: Code-Style -on: +on: push: - branches: - - "master" - pull_request: - types: [opened, synchronize, reopened] + paths: + - '**.php' + +permissions: + contents: write jobs: - php-code-style: - name: Check PHP code style + php-code-styling: runs-on: ubuntu-latest + timeout-minutes: 5 steps: - - uses: actions/checkout@v2 - with: - fetch-depth: 1 - - - name: Cache dependencies - uses: actions/cache@v1 - with: - path: ~/.composer/cache/files - key: dependencies-php-code-style-composer-${{ hashFiles('composer.json') }} + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: 8.0 - tools: composer:v2 - extensions: dom, curl, libxml, mbstring, zip - coverage: none - - - name: Install dependencies - run: | - composer update --prefer-stable --prefer-dist --no-interaction --no-progress + - name: Fix PHP code style issues + uses: aglipanci/laravel-pint-action@2.4 - - name: Run CS Fixer - run: ./vendor/bin/php-cs-fixer fix - - - name: Commit changes - uses: stefanzweifel/git-auto-commit-action@v4 - with: - commit_message: Fix styling + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: Fix styling \ No newline at end of file diff --git a/.gitignore b/.gitignore index 1aa8eef..d428510 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ vendor/ coverage/ .phpunit.result.cache +.phpunit.cache composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json index c7f4a6f..0550b26 100644 --- a/composer.json +++ b/composer.json @@ -17,22 +17,23 @@ } ], "require": { - "php": "^7.3|^8.0.2", - "illuminate/auth": "^8.0|^9.0", - "illuminate/console": "^8.0|^9.0", - "illuminate/encryption": "^8.0|^9.0", - "illuminate/support": "^8.0|^9.0", - "illuminate/view": "^8.0|^9.0", + "php": "^8.2", + "illuminate/auth": "^10.0", + "illuminate/console": "^10.0", + "illuminate/encryption": "^10.0", + "illuminate/support": "^10.0", + "illuminate/view": "^10.0", "laravel/socialite": "^5.0", - "socialiteproviders/gitlab": "^3.1|^4.0", + "socialiteproviders/gitlab": "^4.0", "socialiteproviders/dropbox": "^4.1" }, "require-dev": { + "laravel/pint": "^1.14", "fakerphp/faker": "^1.9.1", + "larastan/larastan": "^2.9", "mockery/mockery": "^1.4.4", - "phpunit/phpunit": "^9.5.10", - "orchestra/testbench": "^6.15|^7.4", - "friendsofphp/php-cs-fixer": "^2.16|^3.8" + "phpunit/phpunit": "^10.5", + "orchestra/testbench": "^8.22" }, "autoload": { "psr-4": { @@ -44,6 +45,20 @@ "Tests\\": "tests/" } }, + "scripts": { + "post-autoload-dump": "@composer run prepare", + "prepare": "@php vendor/bin/testbench package:discover --ansi", + "analyse": "vendor/bin/phpstan analyse", + "test": "vendor/bin/phpunit", + "format": "vendor/bin/pint" + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "phpstan/extension-installer": true + } + }, "extra": { "laravel": { "providers": [ diff --git a/config/identities.php b/config/identities.php index c763b13..c4c6064 100644 --- a/config/identities.php +++ b/config/identities.php @@ -7,7 +7,7 @@ | Encryption Key |-------------------------------------------------------------------------- | - | The key is used by the encrypter service and should be set to a + | The key is used by the encrypter service and should be set to a | random, 32 character string, otherwise encrypted strings will | not be safe. By default the current APP_KEY is used. | diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6f575aa..851a5b8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,22 +1,14 @@ - - - - ./tests/ - - - - - - - + + + + ./tests/Unit + + + + + + + + diff --git a/src/Auth/AuthenticatesUsersWithIdentity.php b/src/Auth/AuthenticatesUsersWithIdentity.php index f6deeae..6a712c7 100644 --- a/src/Auth/AuthenticatesUsersWithIdentity.php +++ b/src/Auth/AuthenticatesUsersWithIdentity.php @@ -12,7 +12,7 @@ trait AuthenticatesUsersWithIdentity { - use RedirectsUsers, InteractsWithPreviousUrl, FindIdentity; + use FindIdentity, InteractsWithPreviousUrl, RedirectsUsers; /** * Redirect the user to the provider authentication page. @@ -54,7 +54,7 @@ public function login(Request $request, $provider) // GuzzleHttp\Exception\ClientException // Client error: `POST https://gitlab.com/oauth/token` resulted in a `401 Unauthorized` response: {"error":"invalid_grant","error_description":"The provided authorization grant is invalid, expired, revoked, does not ma (truncated...) // $this->sendFailedLoginResponse - + // If we find a registered user with the // same identity we attempt to login @@ -63,16 +63,15 @@ public function login(Request $request, $provider) if (! $user) { $this->sendFailedLoginResponse($request, $provider); } - - $this->guard()->login($user /*, $remember = false*/); - + + $this->guard()->login($user /* , $remember = false */); + return $this->sendLoginResponse($request); } /** * Get the failed login response instance. * - * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response * * @throws \Illuminate\Validation\ValidationException @@ -87,7 +86,6 @@ protected function sendFailedLoginResponse(Request $request, $provider) /** * Send the response after the user was authenticated. * - * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ protected function sendLoginResponse(Request $request) @@ -111,7 +109,7 @@ protected function authenticated(Request $request, $user) { // } - + /** * Get the guard to be used during authentication. * diff --git a/src/Auth/ConnectUserIdentity.php b/src/Auth/ConnectUserIdentity.php index 08108a5..c500315 100644 --- a/src/Auth/ConnectUserIdentity.php +++ b/src/Auth/ConnectUserIdentity.php @@ -6,15 +6,15 @@ use Illuminate\Http\Response; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; -use Oneofftech\Identities\Facades\IdentityCrypt; use Oneofftech\Identities\Facades\Identity; +use Oneofftech\Identities\Facades\IdentityCrypt; use Oneofftech\Identities\Support\FindIdentity; -use Oneofftech\Identities\Support\InteractsWithPreviousUrl; use Oneofftech\Identities\Support\InteractsWithAdditionalAttributes; +use Oneofftech\Identities\Support\InteractsWithPreviousUrl; trait ConnectUserIdentity { - use RedirectsUsers, InteractsWithPreviousUrl, InteractsWithAdditionalAttributes, FindIdentity; + use FindIdentity, InteractsWithAdditionalAttributes, InteractsWithPreviousUrl, RedirectsUsers; /** * Redirect the user to the Authentication provider authentication page. @@ -68,14 +68,14 @@ public function connect(Request $request, $provider) // create or update the user's identity - list($user, $identity) = DB::transaction(function () use ($user, $provider, $oauthUser) { + [$user, $identity] = DB::transaction(function () use ($user, $provider, $oauthUser) { $identity = $this->createIdentity($user, $provider, $oauthUser); return [$user, $identity]; }); // todo: event(new Connected($user, $identity)); - + return $this->sendConnectionResponse($request, $identity); } @@ -83,14 +83,14 @@ protected function createIdentity($user, $provider, $oauthUser) { return $user->identities()->updateOrCreate( [ - 'provider'=> $provider, - 'provider_id'=> IdentityCrypt::hash($oauthUser->getId()) + 'provider' => $provider, + 'provider_id' => IdentityCrypt::hash($oauthUser->getId()), ], [ - 'token'=> IdentityCrypt::encryptString($oauthUser->token), - 'refresh_token'=> IdentityCrypt::encryptString($oauthUser->refreshToken), - 'expires_at'=> $oauthUser->expiresIn ? now()->addSeconds($oauthUser->expiresIn) : null, - 'registration' => false, + 'token' => IdentityCrypt::encryptString($oauthUser->token), + 'refresh_token' => IdentityCrypt::encryptString($oauthUser->refreshToken), + 'expires_at' => $oauthUser->expiresIn ? now()->addSeconds($oauthUser->expiresIn) : null, + 'registration' => false, ] ); } @@ -111,8 +111,6 @@ protected function sendConnectionResponse(Request $request, $identity) * * @param mixed $user * @param mixed $identity - * @param array $attributes - * @param \Illuminate\Http\Request $request * @return mixed */ protected function connected($user, $identity, array $attributes, Request $request) diff --git a/src/Auth/RegistersUsersWithIdentity.php b/src/Auth/RegistersUsersWithIdentity.php index 40bcf78..22a35ea 100644 --- a/src/Auth/RegistersUsersWithIdentity.php +++ b/src/Auth/RegistersUsersWithIdentity.php @@ -5,19 +5,19 @@ use Illuminate\Auth\Events\Registered; use Illuminate\Http\Request; use Illuminate\Http\Response; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; -use Oneofftech\Identities\Facades\IdentityCrypt; use Illuminate\Validation\ValidationException; use Laravel\Socialite\AbstractUser as SocialiteUser; use Oneofftech\Identities\Facades\Identity; -use Oneofftech\Identities\Support\InteractsWithPreviousUrl; +use Oneofftech\Identities\Facades\IdentityCrypt; use Oneofftech\Identities\Support\InteractsWithAdditionalAttributes; +use Oneofftech\Identities\Support\InteractsWithPreviousUrl; trait RegistersUsersWithIdentity { - use RedirectsUsers, InteractsWithPreviousUrl, InteractsWithAdditionalAttributes; + use InteractsWithAdditionalAttributes, InteractsWithPreviousUrl, RedirectsUsers; /** * Redirect the user to the Authentication provider authentication page. @@ -86,23 +86,22 @@ public function register(Request $request, $provider) $user = DB::transaction(function () use ($data, $provider, $oauthUser) { $user = $this->create($data); - + $this->createIdentity($user, $provider, $oauthUser); return $user; }); event(new Registered($user)); - - $this->guard()->login($user /*, $remember = false*/); - + + $this->guard()->login($user /* , $remember = false */); + return $this->sendRegistrationResponse($request, $provider); } /** * Maps the socialite user to attributes * - * @param SocialiteUser $oauthUser * @return array */ protected function map(Request $request, SocialiteUser $oauthUser) @@ -120,13 +119,13 @@ protected function createIdentity($user, $provider, $oauthUser) { return $user->identities()->updateOrCreate( [ - 'provider'=> $provider, - 'provider_id'=> IdentityCrypt::hash($oauthUser->getId()) + 'provider' => $provider, + 'provider_id' => IdentityCrypt::hash($oauthUser->getId()), ], [ - 'token'=> IdentityCrypt::encryptString($oauthUser->token), - 'refresh_token'=> IdentityCrypt::encryptString($oauthUser->refreshToken), - 'expires_at'=> $oauthUser->expiresIn ? now()->addSeconds($oauthUser->expiresIn) : null, + 'token' => IdentityCrypt::encryptString($oauthUser->token), + 'refresh_token' => IdentityCrypt::encryptString($oauthUser->refreshToken), + 'expires_at' => $oauthUser->expiresIn ? now()->addSeconds($oauthUser->expiresIn) : null, 'registration' => true, ] ); @@ -153,7 +152,7 @@ protected function registered(Request $request, $user, $provider) { // } - + /** * Get the guard to be used during authentication. * diff --git a/src/Console/Commands/ScaffoldAuthenticationControllers.php b/src/Console/Commands/ScaffoldAuthenticationControllers.php index 96e1ab6..7f40fca 100644 --- a/src/Console/Commands/ScaffoldAuthenticationControllers.php +++ b/src/Console/Commands/ScaffoldAuthenticationControllers.php @@ -10,13 +10,12 @@ class ScaffoldAuthenticationControllers extends Command { - /** * Default application namespace * * @var string */ - const DEFAULT_NAMESPACE = "App\\"; + const DEFAULT_NAMESPACE = 'App\\'; /** * The name and signature of the console command. @@ -61,7 +60,7 @@ public function __construct() parent::__construct(); $this->namespace = self::DEFAULT_NAMESPACE; - + $this->modelNamespace = self::DEFAULT_NAMESPACE; $this->filesystem = new Filesystem; @@ -93,7 +92,7 @@ protected function identifyApplicationNamespace() { try { $this->namespace = $this->laravel->getNamespace(); - + if ($this->namespace !== self::DEFAULT_NAMESPACE) { $this->comment("Using [$this->namespace] as application namespace."); } @@ -198,9 +197,9 @@ protected function compileControllerStub($stub) */ protected function compileModelStub($stub) { - $originalNamespaceDeclaration = str_replace('\\;', ';', "namespace ".self::DEFAULT_NAMESPACE.';'); + $originalNamespaceDeclaration = str_replace('\\;', ';', 'namespace '.self::DEFAULT_NAMESPACE.';'); $newNamespaceDeclaration = str_replace('\\;', ';', "namespace $this->modelNamespace;"); - + return str_replace( $originalNamespaceDeclaration, $newNamespaceDeclaration, diff --git a/src/Encryption/Encrypter.php b/src/Encryption/Encrypter.php index 7563fc2..49a8b74 100644 --- a/src/Encryption/Encrypter.php +++ b/src/Encryption/Encrypter.php @@ -2,8 +2,8 @@ namespace Oneofftech\Identities\Encryption; -use Illuminate\Support\Str; use Illuminate\Encryption\Encrypter as BaseEncrypter; +use Illuminate\Support\Str; class Encrypter extends BaseEncrypter { diff --git a/src/Facades/Identity.php b/src/Facades/Identity.php index f95255d..c4a9613 100644 --- a/src/Facades/Identity.php +++ b/src/Facades/Identity.php @@ -6,8 +6,8 @@ use Illuminate\Support\Facades\Facade; use Oneofftech\Identities\IdentitiesManager; use SocialiteProviders\Dropbox\DropboxExtendSocialite; -use SocialiteProviders\Manager\SocialiteWasCalled; use SocialiteProviders\GitLab\GitLabExtendSocialite; +use SocialiteProviders\Manager\SocialiteWasCalled; /** * @see \Oneofftech\Identities\IdentitiesManager @@ -58,19 +58,19 @@ public static function routes() $namespace = '\\'.rtrim(self::$appNamespace, '\\'); $router->match(['get', 'post'], 'login-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\LoginController@redirect") - ->name("oneofftech::login.provider"); + ->name('oneofftech::login.provider'); $router->get('login-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\LoginController@login") - ->name("oneofftech::login.callback"); - + ->name('oneofftech::login.callback'); + $router->match(['get', 'post'], 'register-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\RegisterController@redirect") - ->name("oneofftech::register.provider"); + ->name('oneofftech::register.provider'); $router->get('register-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\RegisterController@register") - ->name("oneofftech::register.callback"); - + ->name('oneofftech::register.callback'); + $router->match(['get', 'post'], 'connect-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\ConnectController@redirect") - ->name("oneofftech::connect.provider"); + ->name('oneofftech::connect.provider'); $router->get('connect-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\ConnectController@connect") - ->name("oneofftech::connect.callback"); + ->name('oneofftech::connect.callback'); } /** @@ -88,20 +88,17 @@ public static function events() /** * Find a user instance by the given ID. - * - * @param string $id */ public static function findUserByIdOrFail(string $id) { $model = static::newUserModel(); + return $model->where($model->getKeyName(), $id)->firstOrFail(); } /** * Find a user instance by the given provider or fail. * - * @param string $provider - * @param string $id * @return mixed */ public static function findUserByIdentity(string $provider, string $id) @@ -139,7 +136,6 @@ public static function newUserModel() /** * Specify the user model that should be used. * - * @param string $model * @return static */ public static function useUserModel(string $model) @@ -174,7 +170,6 @@ public static function newIdentityModel() /** * Specify the identity model that should be used. * - * @param string $model * @return static */ public static function useIdentityModel(string $model) @@ -196,7 +191,7 @@ public static function useNamespace(string $namespace) return new static; } - + /** * Get the configured namespace * diff --git a/src/Facades/IdentityCrypt.php b/src/Facades/IdentityCrypt.php index f536809..67c3f83 100644 --- a/src/Facades/IdentityCrypt.php +++ b/src/Facades/IdentityCrypt.php @@ -32,7 +32,7 @@ protected static function getFacadeAccessor() /** * Hash a string for pseudoanonymization * - * @param string $value + * @param string $value * @return string */ public static function hash($value) diff --git a/src/IdentitiesManager.php b/src/IdentitiesManager.php index f56e76b..e06826b 100644 --- a/src/IdentitiesManager.php +++ b/src/IdentitiesManager.php @@ -2,14 +2,14 @@ namespace Oneofftech\Identities; -use InvalidArgumentException; use Illuminate\Support\Manager; +use InvalidArgumentException; use Laravel\Socialite\Facades\Socialite; class IdentitiesManager extends Manager { /** - * @inheritdoc + * {@inheritdoc} */ protected function createDriver($driver) { @@ -22,7 +22,7 @@ protected function createDriver($driver) return Socialite::driver($driver); } } - + /** * Create an instance of the gitlab driver. * @@ -53,7 +53,7 @@ protected function createDropboxDriver() return Socialite::driver('dropbox'); } - + /** * Get the default driver name. * diff --git a/src/Providers/IdentitiesServiceProvider.php b/src/Providers/IdentitiesServiceProvider.php index b725ab3..145f0ac 100644 --- a/src/Providers/IdentitiesServiceProvider.php +++ b/src/Providers/IdentitiesServiceProvider.php @@ -2,12 +2,12 @@ namespace Oneofftech\Identities\Providers; -use Illuminate\Support\Str; use Illuminate\Support\ServiceProvider; -use Oneofftech\Identities\IdentitiesManager; +use Illuminate\Support\Str; +use Oneofftech\Identities\Console\Commands\ScaffoldAuthenticationControllers; use Oneofftech\Identities\Encryption\Encrypter; +use Oneofftech\Identities\IdentitiesManager; use Oneofftech\Identities\View\Components\IdentityLink; -use Oneofftech\Identities\Console\Commands\ScaffoldAuthenticationControllers; class IdentitiesServiceProvider extends ServiceProvider { @@ -46,7 +46,7 @@ public function register() return new Encrypter($key, $app_config['cipher']); }); - + $this->app->singleton(IdentitiesManager::class, function ($app) { return new IdentitiesManager($app); }); diff --git a/src/Support/FindIdentity.php b/src/Support/FindIdentity.php index e77ba9d..6c8758a 100644 --- a/src/Support/FindIdentity.php +++ b/src/Support/FindIdentity.php @@ -2,17 +2,17 @@ namespace Oneofftech\Identities\Support; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Oneofftech\Identities\Facades\Identity; use Oneofftech\Identities\Facades\IdentityCrypt; -use Illuminate\Database\Eloquent\ModelNotFoundException; trait FindIdentity { /** * Search a user given its identity in the third party provider * - * @param \Laravel\Socialite\Contracts\User $identity Third party identity provided by Laravel Socialite - * @param string $provider The identity provider name + * @param \Laravel\Socialite\Contracts\User $identity Third party identity provided by Laravel Socialite + * @param string $provider The identity provider name * @return \Illuminate\Contracts\Auth\Authenticatable|null */ protected function findUserFromIdentity($identity, $provider) diff --git a/src/Support/InteractsWithAdditionalAttributes.php b/src/Support/InteractsWithAdditionalAttributes.php index c5e5c1e..d6014d2 100644 --- a/src/Support/InteractsWithAdditionalAttributes.php +++ b/src/Support/InteractsWithAdditionalAttributes.php @@ -4,7 +4,6 @@ trait InteractsWithAdditionalAttributes { - /** * The attributes that should be retrieved from * the request to append to the redirect @@ -27,7 +26,7 @@ protected function pushAttributes($request) if (empty($attributes)) { return; } - + $request->session()->put('_oot.identities.attributes', json_encode($request->only($attributes))); } diff --git a/src/View/Components/IdentityLink.php b/src/View/Components/IdentityLink.php index 8935c2c..703c70b 100644 --- a/src/View/Components/IdentityLink.php +++ b/src/View/Components/IdentityLink.php @@ -9,7 +9,7 @@ class IdentityLink extends Component { private static $availableActions = ['register', 'login', 'connect']; - + private static $actionLabels = [ 'register' => 'Register via :Provider', 'login' => 'Log in via :Provider', @@ -29,7 +29,7 @@ class IdentityLink extends Component * @var string */ public $provider; - + /** * The label for the link * @@ -47,11 +47,11 @@ class IdentityLink extends Component /** * Create a new component instance. * - * @param string $provider The identity provider - * @param string $action The action the button should perform. Available: login, register. Default login. - * @param string $label The label for the link. It will be used as string to localize. - * Default null a default label in the form "$action via $provider" will be used. - * @param array|null $parameter Additional parameters to append on the redirect request + * @param string $provider The identity provider + * @param string $action The action the button should perform. Available: login, register. Default login. + * @param string $label The label for the link. It will be used as string to localize. + * Default null a default label in the form "$action via $provider" will be used. + * @param array|null $parameter Additional parameters to append on the redirect request * @return self */ public function __construct($provider, $action = 'login', $label = null, $parameters = null) diff --git a/src/WithIdentities.php b/src/WithIdentities.php index df73a03..6da2299 100644 --- a/src/WithIdentities.php +++ b/src/WithIdentities.php @@ -6,7 +6,6 @@ trait WithIdentities { - /** * Get the associated identities * diff --git a/stubs/migrations/2020_08_09_115707_create_identities_table.php b/stubs/migrations/2020_08_09_115707_create_identities_table.php index 860806c..2e1a30d 100644 --- a/stubs/migrations/2020_08_09_115707_create_identities_table.php +++ b/stubs/migrations/2020_08_09_115707_create_identities_table.php @@ -1,8 +1,8 @@ belongsTo(IdentityFacade::userModel()); diff --git a/tests/Fixtures/User.php b/tests/Fixtures/User.php index 2c062da..8cede5f 100644 --- a/tests/Fixtures/User.php +++ b/tests/Fixtures/User.php @@ -2,8 +2,8 @@ namespace Tests\Fixtures; -use Oneofftech\Identities\WithIdentities; use Illuminate\Foundation\Auth\User as Authenticatable; +use Oneofftech\Identities\WithIdentities; class User extends Authenticatable { diff --git a/tests/TestCase.php b/tests/TestCase.php index 74a866b..ea3bf13 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,13 +2,13 @@ namespace Tests; -use ReflectionFunction; -use Illuminate\Support\Str; use Illuminate\Events\Dispatcher; +use Illuminate\Support\Str; use Laravel\Socialite\SocialiteServiceProvider; -use Orchestra\Testbench\TestCase as BaseTestCase; use Oneofftech\Identities\Facades\Identity as IdentityFacade; use Oneofftech\Identities\Providers\IdentitiesServiceProvider; +use Orchestra\Testbench\TestCase as BaseTestCase; +use ReflectionFunction; use SocialiteProviders\Dropbox\DropboxExtendSocialite; use SocialiteProviders\GitLab\GitLabExtendSocialite; use SocialiteProviders\Manager\SocialiteWasCalled; @@ -17,33 +17,30 @@ abstract class TestCase extends BaseTestCase { - /** * Setup the test environment. */ - public function setUp(): void + protected function setUp(): void { parent::setUp(); - $this->setUpDatabase(); - $this->setUpSession($this->app); $this->activateSocialiteExtensions(); } - protected function setUpTraits() - { - parent::setUpTraits(); + // protected function setUpTraits() + // { + // parent::setUpTraits(); - $uses = \array_flip(\class_uses_recursive(static::class)); + // $uses = \array_flip(\class_uses_recursive(static::class)); - if (isset($uses[UseTestFixtures::class])) { - $this->useTestFixtures(); - } + // if (isset($uses[UseTestFixtures::class])) { + // $this->useTestFixtures(); + // } - return $uses; - } + // return $uses; + // } /** * Define environment setup. @@ -53,33 +50,37 @@ protected function setUpTraits() * @param \Illuminate\Foundation\Application $app * @return void */ - protected function getEnvironmentSetUp($app) + protected function defineEnvironment($app) { // Setup default database to use sqlite :memory: - $app['config']->set('database.default', 'testing'); - $app['config']->set('database.connections.testing', [ - 'driver' => 'sqlite', + config()->set('database.default', 'testing'); + config()->set('database.connections.testing', [ + 'driver' => 'sqlite', 'database' => ':memory:', - 'prefix' => '', + 'prefix' => '', ]); - $app['config']->set('services.gitlab', [ + config()->set('services.gitlab', [ 'client_id' => 'aaa', 'client_secret' => 'bbb', 'redirect' => null, - 'instance_uri' => 'https://gitlab.com/' + 'instance_uri' => 'https://gitlab.com/', ]); - $app['config']->set('services.dropbox', [ + config()->set('services.dropbox', [ 'client_id' => 'aaa', 'client_secret' => 'bbb', 'redirect' => null, ]); $key = Str::random(32); - $app['config']->set('app.key', 'base64:'.base64_encode($key)); - $app['config']->set('app.cipher', 'AES-256-CBC'); - $app['config']->set('identities.key', 'base64:'.base64_encode($key)); + config()->set('app.key', 'base64:'.base64_encode($key)); + config()->set('app.cipher', 'AES-256-CBC'); + config()->set('identities.key', 'base64:'.base64_encode($key)); + + IdentityFacade::useNamespace('App'); + IdentityFacade::useIdentityModel('App\\Identity'); + IdentityFacade::useUserModel('App\\User'); } - + /** * Loads the service provider during the tests */ @@ -88,19 +89,17 @@ protected function getPackageProviders($app) return [ SocialiteServiceProvider::class, \SocialiteProviders\Manager\ServiceProvider::class, - IdentitiesServiceProvider::class + IdentitiesServiceProvider::class, ]; } - protected function setUpDatabase() + protected function defineDatabaseMigrations() { $this->loadLaravelMigrations(); - $this->loadMigrationsFrom(__DIR__.'/../stubs/migrations'); - - IdentityFacade::useNamespace("App"); - IdentityFacade::useIdentityModel("App\\Identity"); - IdentityFacade::useUserModel("App\\User"); + $this->loadMigrationsFrom( + __DIR__.'/../stubs/migrations' + ); } protected function setUpSession() diff --git a/tests/Unit/ConnectControllerTest.php b/tests/Unit/ConnectControllerTest.php index 358f033..4f99a73 100644 --- a/tests/Unit/ConnectControllerTest.php +++ b/tests/Unit/ConnectControllerTest.php @@ -2,18 +2,18 @@ namespace Tests\Unit; -use Mockery; use Carbon\Carbon; -use Tests\TestCase; -use Tests\Fixtures\User; -use Illuminate\Support\Str; -use SocialiteProviders\GitLab\Provider; -use Tests\Fixtures\Concern\UseTestFixtures; use Illuminate\Auth\AuthenticationException; -use Oneofftech\Identities\Facades\IdentityCrypt; use Illuminate\Foundation\Testing\RefreshDatabase; -use SocialiteProviders\Manager\OAuth2\User as OauthUser; +use Illuminate\Support\Str; +use Mockery; use Oneofftech\Identities\Facades\Identity as IdentityFacade; +use Oneofftech\Identities\Facades\IdentityCrypt; +use SocialiteProviders\GitLab\Provider; +use SocialiteProviders\Manager\OAuth2\User as OauthUser; +use Tests\Fixtures\Concern\UseTestFixtures; +use Tests\Fixtures\User; +use Tests\TestCase; class ConnectControllerTest extends TestCase { @@ -26,8 +26,7 @@ public function test_redirect_to_provider_require_authentication() $router = $this->app->make('router'); - $router->get('login', function () { - })->name('login'); + $router->get('login', function () {})->name('login'); $this->withoutExceptionHandling(); @@ -42,7 +41,7 @@ public function test_redirect_to_provider() IdentityFacade::useNamespace('Tests\\Fixtures'); IdentityFacade::routes(); - $user = tap((new User()), function ($u) { + $user = tap((new User), function ($u) { $u->forceFill([ 'email' => 'user@local.local', 'name' => 'User', @@ -55,7 +54,7 @@ public function test_redirect_to_provider() ->get(route('oneofftech::connect.provider', ['provider' => 'gitlab'])); $response->assertRedirect(); - + $location = urldecode($response->headers->get('Location')); $this->assertStringContainsString('gitlab.com', $location); @@ -64,8 +63,6 @@ public function test_redirect_to_provider() public function test_connect_creates_identity() { - $this->useTestFixtures(); - $user = $this->createUser(); $this->withoutExceptionHandling(); @@ -74,17 +71,17 @@ public function test_connect_creates_identity() Carbon::setTestNow(Carbon::create(2020, 11, 12, 10, 20)); - $oauthFakeUser = (new OauthUser())->map([ - 'id' => 'U1', + $oauthFakeUser = (new OauthUser)->map([ + 'id' => 'U1', 'nickname' => 'User', - 'name' => 'User', - 'email' => 'user@local.com', - 'avatar' => 'https://gitlab.com', - 'token' => 'T2', + 'name' => 'User', + 'email' => 'user@local.com', + 'avatar' => 'https://gitlab.com', + 'token' => 'T2', 'refreshToken' => 'RT2', 'expiresIn' => Carbon::SECONDS_PER_MINUTE, ]); - + $driverMock->shouldReceive('user')->andReturn($oauthFakeUser); $driverMock->shouldReceive('redirectUrl')->andReturn($driverMock); @@ -105,19 +102,17 @@ public function test_connect_creates_identity() $this->assertEquals('RT2', IdentityCrypt::decryptString($updatedIdentity->refresh_token)); $this->assertFalse($updatedIdentity->registration); } - + public function test_connect_updates_existing_identity() { - $this->useTestFixtures(); - $user = $this->createUser(); $identity = $user->identities()->create([ - 'provider'=> 'gitlab', - 'provider_id'=> IdentityCrypt::hash('U1'), - 'token'=> 'T1', - 'refresh_token'=> null, - 'expires_at'=> null, + 'provider' => 'gitlab', + 'provider_id' => IdentityCrypt::hash('U1'), + 'token' => 'T1', + 'refresh_token' => null, + 'expires_at' => null, 'registration' => true, ]); @@ -127,17 +122,17 @@ public function test_connect_updates_existing_identity() Carbon::setTestNow(Carbon::create(2020, 11, 12, 10, 20)); - $oauthFakeUser = (new OauthUser())->map([ - 'id' => 'U1', + $oauthFakeUser = (new OauthUser)->map([ + 'id' => 'U1', 'nickname' => 'User', - 'name' => 'User', - 'email' => 'user@local.com', - 'avatar' => 'https://gitlab.com', - 'token' => 'T2', + 'name' => 'User', + 'email' => 'user@local.com', + 'avatar' => 'https://gitlab.com', + 'token' => 'T2', 'refreshToken' => 'RT2', 'expiresIn' => Carbon::SECONDS_PER_MINUTE, ]); - + $driverMock->shouldReceive('user')->andReturn($oauthFakeUser); $driverMock->shouldReceive('redirectUrl')->andReturn($driverMock); diff --git a/tests/Unit/EncrypterTest.php b/tests/Unit/EncrypterTest.php index a3841d1..ca81fa3 100644 --- a/tests/Unit/EncrypterTest.php +++ b/tests/Unit/EncrypterTest.php @@ -2,10 +2,10 @@ namespace Tests\Unit; -use Tests\TestCase; use Illuminate\Support\Str; use Oneofftech\Identities\Encryption\Encrypter; use Oneofftech\Identities\Facades\IdentityCrypt; +use Tests\TestCase; class EncrypterTest extends TestCase { diff --git a/tests/Unit/IdentityCryptTest.php b/tests/Unit/IdentityCryptTest.php index 27c478d..0a26fba 100644 --- a/tests/Unit/IdentityCryptTest.php +++ b/tests/Unit/IdentityCryptTest.php @@ -2,9 +2,9 @@ namespace Tests\Unit; -use Tests\TestCase; use Oneofftech\Identities\Encryption\Encrypter; use Oneofftech\Identities\Facades\IdentityCrypt; +use Tests\TestCase; class IdentityCryptTest extends TestCase { @@ -17,7 +17,7 @@ public function test_value_can_be_hashed() $this->assertEquals($expected, $result); } - + public function test_facade_return_encrypter_instance() { $this->assertInstanceOf(Encrypter::class, IdentityCrypt::getFacadeRoot()); diff --git a/tests/Unit/IdentityLinkTest.php b/tests/Unit/IdentityLinkTest.php index e249c95..17bda0c 100644 --- a/tests/Unit/IdentityLinkTest.php +++ b/tests/Unit/IdentityLinkTest.php @@ -2,18 +2,18 @@ namespace Tests\Unit; -use Tests\TestCase; -use Oneofftech\Identities\Facades\Identity; use Illuminate\Foundation\Testing\DatabaseMigrations; -use InvalidArgumentException; use Illuminate\View\Component; +use InvalidArgumentException; +use Oneofftech\Identities\Facades\Identity; use Oneofftech\Identities\View\Components\IdentityLink; +use Tests\TestCase; class IdentityLinkTest extends TestCase { use DatabaseMigrations; - public function setUp(): void + protected function setUp(): void { parent::setUp(); @@ -23,7 +23,7 @@ public function setUp(): void public function test_unsupported_action_throws() { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("Specified action [dance] is not supported."); + $this->expectExceptionMessage('Specified action [dance] is not supported.'); new IdentityLink('gitlab', 'dance'); } diff --git a/tests/Unit/IdentityModelsConfigurationTest.php b/tests/Unit/IdentityModelsConfigurationTest.php index a7b41d7..472bac5 100644 --- a/tests/Unit/IdentityModelsConfigurationTest.php +++ b/tests/Unit/IdentityModelsConfigurationTest.php @@ -2,11 +2,11 @@ namespace Tests\Unit; -use Tests\TestCase; -use Oneofftech\Identities\Facades\Identity as IdentityFacade; use Illuminate\Foundation\Testing\DatabaseMigrations; +use Oneofftech\Identities\Facades\Identity as IdentityFacade; use Tests\Fixtures\Identity; use Tests\Fixtures\User; +use Tests\TestCase; class IdentityModelsConfigurationTest extends TestCase { @@ -21,18 +21,18 @@ public function test_default_models_uses_app_namespace() public function test_user_model_can_be_customized() { IdentityFacade::useUserModel(User::class); - + $this->assertEquals(User::class, IdentityFacade::userModel()); - + $this->assertInstanceOf(User::class, IdentityFacade::newUserModel()); } public function test_identity_model_can_be_customized() { IdentityFacade::useIdentityModel(Identity::class); - + $this->assertEquals(Identity::class, IdentityFacade::identityModel()); - + $this->assertInstanceOf(Identity::class, IdentityFacade::newIdentityModel()); } } diff --git a/tests/Unit/IdentityServiceProviderTest.php b/tests/Unit/IdentityServiceProviderTest.php index ab6ffaf..612ac00 100644 --- a/tests/Unit/IdentityServiceProviderTest.php +++ b/tests/Unit/IdentityServiceProviderTest.php @@ -2,17 +2,17 @@ namespace Tests\Unit; -use Tests\TestCase; -use SocialiteProviders\Dropbox\Provider as DropboxDriver; -use Oneofftech\Identities\Facades\Identity; -use Oneofftech\Identities\IdentitiesManager; -use SocialiteProviders\Manager\SocialiteWasCalled; -use SocialiteProviders\GitLab\GitLabExtendSocialite; use Illuminate\Foundation\Testing\DatabaseMigrations; use InvalidArgumentException; use Laravel\Socialite\Two\FacebookProvider; +use Oneofftech\Identities\Facades\Identity; +use Oneofftech\Identities\IdentitiesManager; use Oneofftech\Identities\Providers\IdentitiesServiceProvider; +use SocialiteProviders\Dropbox\Provider as DropboxDriver; +use SocialiteProviders\GitLab\GitLabExtendSocialite; use SocialiteProviders\GitLab\Provider as GitlabSocialiteProvider; +use SocialiteProviders\Manager\SocialiteWasCalled; +use Tests\TestCase; class IdentityServiceProviderTest extends TestCase { @@ -32,7 +32,7 @@ public function test_it_can_instantiate_the_gitlab_driver() $factory = $this->app->make(IdentitiesManager::class); $provider = $factory->driver('gitlab'); - + $this->assertInstanceOf(GitlabSocialiteProvider::class, $provider); } diff --git a/tests/Unit/IdentityTraitTest.php b/tests/Unit/IdentityTraitTest.php index 4c0c8a4..3b76bd3 100644 --- a/tests/Unit/IdentityTraitTest.php +++ b/tests/Unit/IdentityTraitTest.php @@ -2,26 +2,26 @@ namespace Tests\Unit; +use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Str; -use Tests\TestCase; +use Laravel\Socialite\Contracts\User as SocialiteUser; use Oneofftech\Identities\Facades\Identity as IdentityFacade; -use Illuminate\Foundation\Testing\RefreshDatabase; use Oneofftech\Identities\Facades\IdentityCrypt; use Oneofftech\Identities\Support\FindIdentity; use Tests\Fixtures\Identity; use Tests\Fixtures\User; -use Laravel\Socialite\Contracts\User as SocialiteUser; +use Tests\TestCase; class IdentityTraitTest extends TestCase { - use RefreshDatabase, FindIdentity; + use FindIdentity, RefreshDatabase; public function test_with_identity_trait() { - IdentityFacade::useNamespace("Tests\\Fixtures\\"); - IdentityFacade::useIdentityModel("Tests\\Fixtures\\Identity"); - IdentityFacade::useUserModel("Tests\\Fixtures\\User"); - + IdentityFacade::useNamespace('Tests\\Fixtures\\'); + IdentityFacade::useIdentityModel('Tests\\Fixtures\\Identity'); + IdentityFacade::useUserModel('Tests\\Fixtures\\User'); + $user = tap((new User), function ($u) { $u->forceFill([ 'email' => 'user@local.local', @@ -32,11 +32,11 @@ public function test_with_identity_trait() }); $identity = $user->identities()->create([ - 'provider'=> 'social', - 'provider_id'=> 'aaaa', - 'token'=> 'tttt', - 'refresh_token'=> null, - 'expires_at'=> null, + 'provider' => 'social', + 'provider_id' => 'aaaa', + 'token' => 'tttt', + 'refresh_token' => null, + 'expires_at' => null, 'registration' => true, ]); @@ -53,10 +53,10 @@ public function test_with_identity_trait() public function test_find_identity_trait() { - IdentityFacade::useNamespace("Tests\\Fixtures\\"); - IdentityFacade::useIdentityModel("Tests\\Fixtures\\Identity"); - IdentityFacade::useUserModel("Tests\\Fixtures\\User"); - + IdentityFacade::useNamespace('Tests\\Fixtures\\'); + IdentityFacade::useIdentityModel('Tests\\Fixtures\\Identity'); + IdentityFacade::useUserModel('Tests\\Fixtures\\User'); + $expectedUser = tap((new User), function ($u) { $u->forceFill([ 'email' => 'user@local.local', @@ -66,32 +66,37 @@ public function test_find_identity_trait() ])->save(); $u->identities()->create([ - 'provider'=> 'social', - 'provider_id'=> IdentityCrypt::hash('P1'), - 'token'=> 'tttt', - 'refresh_token'=> null, - 'expires_at'=> null, + 'provider' => 'social', + 'provider_id' => IdentityCrypt::hash('P1'), + 'token' => 'tttt', + 'refresh_token' => null, + 'expires_at' => null, 'registration' => true, ]); }); - $user = $this->findUserFromIdentity(new class implements SocialiteUser { + $user = $this->findUserFromIdentity(new class implements SocialiteUser + { public function getId() { return 'P1'; } + public function getNickname() { return null; } + public function getName() { return null; } + public function getEmail() { return null; } + public function getAvatar() { return null; @@ -103,27 +108,32 @@ public function getAvatar() public function test_find_identity_trait_return_null_when_not_found() { - IdentityFacade::useNamespace("Tests\\Fixtures\\"); - IdentityFacade::useIdentityModel("Tests\\Fixtures\\Identity"); - IdentityFacade::useUserModel("Tests\\Fixtures\\User"); - - $user = $this->findUserFromIdentity(new class implements SocialiteUser { + IdentityFacade::useNamespace('Tests\\Fixtures\\'); + IdentityFacade::useIdentityModel('Tests\\Fixtures\\Identity'); + IdentityFacade::useUserModel('Tests\\Fixtures\\User'); + + $user = $this->findUserFromIdentity(new class implements SocialiteUser + { public function getId() { return 'P1'; } + public function getNickname() { return null; } + public function getName() { return null; } + public function getEmail() { return null; } + public function getAvatar() { return null; @@ -135,10 +145,10 @@ public function getAvatar() public function test_find_user() { - IdentityFacade::useNamespace("Tests\\Fixtures\\"); - IdentityFacade::useIdentityModel("Tests\\Fixtures\\Identity"); - IdentityFacade::useUserModel("Tests\\Fixtures\\User"); - + IdentityFacade::useNamespace('Tests\\Fixtures\\'); + IdentityFacade::useIdentityModel('Tests\\Fixtures\\Identity'); + IdentityFacade::useUserModel('Tests\\Fixtures\\User'); + $expectedUser = tap((new User), function ($u) { $u->forceFill([ 'email' => 'user@local.local', diff --git a/tests/Unit/InteractWithPreviousUrlTest.php b/tests/Unit/InteractWithPreviousUrlTest.php index 81cafe5..2bcf3e6 100644 --- a/tests/Unit/InteractWithPreviousUrlTest.php +++ b/tests/Unit/InteractWithPreviousUrlTest.php @@ -2,9 +2,9 @@ namespace Tests\Unit; -use Tests\TestCase; use Illuminate\Support\Facades\Session; use Oneofftech\Identities\Support\InteractsWithPreviousUrl; +use Tests\TestCase; class InteractWithPreviousUrlTest extends TestCase { @@ -18,7 +18,7 @@ public function test_previous_url_is_stored() $this->savePreviousUrl(); } - + public function test_previous_url_can_be_retrieved() { Session::shouldReceive('previousUrl')->andReturnNull(); diff --git a/tests/Unit/InteractsWithAdditionalAttributesDefinedViaMethodTest.php b/tests/Unit/InteractsWithAdditionalAttributesDefinedViaMethodTest.php index af39aa5..22d21d2 100644 --- a/tests/Unit/InteractsWithAdditionalAttributesDefinedViaMethodTest.php +++ b/tests/Unit/InteractsWithAdditionalAttributesDefinedViaMethodTest.php @@ -3,9 +3,9 @@ namespace Tests\Unit; use Illuminate\Http\Request; -use Tests\TestCase; use Illuminate\Support\Facades\Session; use Oneofftech\Identities\Support\InteractsWithAdditionalAttributes; +use Tests\TestCase; class InteractsWithAdditionalAttributesDefinedViaMethodTest extends TestCase { @@ -22,19 +22,19 @@ public function test_attributes_can_be_defined() $this->assertEquals(['attribute'], $attributes); } - + public function test_attributes_are_saved() { Session::shouldReceive('put')->once()->with('_oot.identities.attributes', '{"attribute":"value"}'); $request = Request::create('http://localhost', 'GET', [ - 'attribute' => 'value' + 'attribute' => 'value', ]); $request->setLaravelSession(Session::getFacadeRoot()); $this->pushAttributes($request); } - + public function test_attributes_are_retrieved() { Session::shouldReceive('previousUrl')->andReturnNull(); diff --git a/tests/Unit/InteractsWithAdditionalAttributesDefinedViaPropertyTest.php b/tests/Unit/InteractsWithAdditionalAttributesDefinedViaPropertyTest.php index 35d6c5f..601d095 100644 --- a/tests/Unit/InteractsWithAdditionalAttributesDefinedViaPropertyTest.php +++ b/tests/Unit/InteractsWithAdditionalAttributesDefinedViaPropertyTest.php @@ -3,9 +3,9 @@ namespace Tests\Unit; use Illuminate\Http\Request; -use Tests\TestCase; use Illuminate\Support\Facades\Session; use Oneofftech\Identities\Support\InteractsWithAdditionalAttributes; +use Tests\TestCase; class InteractsWithAdditionalAttributesDefinedViaPropertyTest extends TestCase { @@ -19,19 +19,19 @@ public function test_attributes_can_be_defined() $this->assertEquals(['attribute'], $attributes); } - + public function test_attributes_are_saved() { Session::shouldReceive('put')->once()->with('_oot.identities.attributes', '{"attribute":"value"}'); $request = Request::create('http://localhost', 'GET', [ - 'attribute' => 'value' + 'attribute' => 'value', ]); $request->setLaravelSession(Session::getFacadeRoot()); $this->pushAttributes($request); } - + public function test_attributes_are_retrieved() { Session::shouldReceive('previousUrl')->andReturnNull(); diff --git a/tests/Unit/InteractsWithAdditionalAttributesTest.php b/tests/Unit/InteractsWithAdditionalAttributesTest.php index 1b70a86..024fb96 100644 --- a/tests/Unit/InteractsWithAdditionalAttributesTest.php +++ b/tests/Unit/InteractsWithAdditionalAttributesTest.php @@ -3,9 +3,9 @@ namespace Tests\Unit; use Illuminate\Http\Request; -use Tests\TestCase; use Illuminate\Support\Facades\Session; use Oneofftech\Identities\Support\InteractsWithAdditionalAttributes; +use Tests\TestCase; class InteractsWithAdditionalAttributesTest extends TestCase { @@ -17,19 +17,19 @@ public function test_attributes_not_defined() $this->assertEquals([], $attributes); } - + public function test_nothing_is_saved() { Session::shouldReceive('put')->never(); $request = Request::create('http://localhost', 'GET', [ - 'attribute' => 'value' + 'attribute' => 'value', ]); $request->setLaravelSession(Session::getFacadeRoot()); $this->pushAttributes($request); } - + public function test_nothing_is_retrieved() { Session::shouldReceive('previousUrl')->andReturnNull(); diff --git a/tests/Unit/LoginControllerTest.php b/tests/Unit/LoginControllerTest.php index 75d91a7..afa2581 100644 --- a/tests/Unit/LoginControllerTest.php +++ b/tests/Unit/LoginControllerTest.php @@ -2,15 +2,15 @@ namespace Tests\Unit; -use Mockery; -use Tests\TestCase; -use SocialiteProviders\GitLab\Provider; -use Tests\Fixtures\Concern\UseTestFixtures; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Validation\ValidationException; -use SocialiteProviders\Manager\OAuth2\User as OauthUser; +use Mockery; use Oneofftech\Identities\Facades\Identity as IdentityFacade; use Oneofftech\Identities\Facades\IdentityCrypt; +use SocialiteProviders\GitLab\Provider; +use SocialiteProviders\Manager\OAuth2\User as OauthUser; +use Tests\Fixtures\Concern\UseTestFixtures; +use Tests\TestCase; class LoginControllerTest extends TestCase { @@ -24,7 +24,7 @@ public function test_redirect_to_provider() $response = $this->get(route('oneofftech::login.provider', ['provider' => 'gitlab'])); $response->assertRedirect(); - + $location = urldecode($response->headers->get('Location')); $this->assertStringContainsString('gitlab.com', $location); @@ -33,16 +33,14 @@ public function test_redirect_to_provider() public function test_user_login() { - $this->useTestFixtures(); - $user = $this->createUser(); $identity = $user->identities()->create([ - 'provider'=> 'gitlab', - 'provider_id'=> IdentityCrypt::hash('U1'), - 'token'=> 'T1', - 'refresh_token'=> null, - 'expires_at'=> null, + 'provider' => 'gitlab', + 'provider_id' => IdentityCrypt::hash('U1'), + 'token' => 'T1', + 'refresh_token' => null, + 'expires_at' => null, 'registration' => true, ]); @@ -50,15 +48,15 @@ public function test_user_login() $driverMock = Mockery::mock(Provider::class)->makePartial(); - $oauthFakeUser = (new OauthUser())->map([ - 'id' => 'U1', + $oauthFakeUser = (new OauthUser)->map([ + 'id' => 'U1', 'nickname' => 'User', - 'name' => 'User', - 'email' => 'user@local.com', - 'avatar' => 'https://gitlab.com', - 'token' => 'T1', + 'name' => 'User', + 'email' => 'user@local.com', + 'avatar' => 'https://gitlab.com', + 'token' => 'T1', ]); - + $driverMock->shouldReceive('user')->andReturn($oauthFakeUser); $driverMock->shouldReceive('redirectUrl')->andReturn($driverMock); @@ -71,19 +69,17 @@ public function test_user_login() $this->assertAuthenticatedAs($user); } - + public function test_user_login_denied_if_identity_cannot_be_found() { - $this->useTestFixtures(); - $user = $this->createUser(); $identity = $user->identities()->create([ - 'provider'=> 'facebook', - 'provider_id'=> IdentityCrypt::hash('U2'), - 'token'=> 'T1', - 'refresh_token'=> null, - 'expires_at'=> null, + 'provider' => 'facebook', + 'provider_id' => IdentityCrypt::hash('U2'), + 'token' => 'T1', + 'refresh_token' => null, + 'expires_at' => null, 'registration' => true, ]); @@ -91,15 +87,15 @@ public function test_user_login_denied_if_identity_cannot_be_found() $driverMock = Mockery::mock(Provider::class)->makePartial(); - $oauthFakeUser = (new OauthUser())->map([ - 'id' => 'U1', + $oauthFakeUser = (new OauthUser)->map([ + 'id' => 'U1', 'nickname' => 'User', - 'name' => 'User', - 'email' => 'user@local.com', - 'avatar' => 'https://gitlab.com', - 'token' => 'T1', + 'name' => 'User', + 'email' => 'user@local.com', + 'avatar' => 'https://gitlab.com', + 'token' => 'T1', ]); - + $driverMock->shouldReceive('user')->andReturn($oauthFakeUser); $driverMock->shouldReceive('redirectUrl')->andReturn($driverMock); diff --git a/tests/Unit/NamespaceConfigurationTest.php b/tests/Unit/NamespaceConfigurationTest.php index 0beb10d..0a908b8 100644 --- a/tests/Unit/NamespaceConfigurationTest.php +++ b/tests/Unit/NamespaceConfigurationTest.php @@ -2,9 +2,9 @@ namespace Tests\Unit; -use Tests\TestCase; -use Oneofftech\Identities\Facades\Identity as IdentityFacade; use Illuminate\Foundation\Testing\DatabaseMigrations; +use Oneofftech\Identities\Facades\Identity as IdentityFacade; +use Tests\TestCase; class NamespaceConfigurationTest extends TestCase { @@ -18,7 +18,7 @@ public function test_default_app_namespace() public function test_namespace_can_be_customized() { IdentityFacade::useNamespace('Test\\Fixtures'); - + $this->assertEquals('Test\\Fixtures', IdentityFacade::namespace()); } @@ -27,7 +27,7 @@ public function test_routes_registered_when_using_custom_namespace() IdentityFacade::useNamespace('Test\\Fixtures'); IdentityFacade::routes(); - + /** * @var \Illuminate\Routing\Router */ diff --git a/tests/Unit/RegistrationControllerTest.php b/tests/Unit/RegistrationControllerTest.php index 94137c9..9722aed 100644 --- a/tests/Unit/RegistrationControllerTest.php +++ b/tests/Unit/RegistrationControllerTest.php @@ -2,16 +2,16 @@ namespace Tests\Unit; -use Mockery; -use Tests\TestCase; -use Tests\Fixtures\User; -use SocialiteProviders\GitLab\Provider; -use Tests\Fixtures\Concern\UseTestFixtures; -use Illuminate\Validation\ValidationException; -use Oneofftech\Identities\Facades\IdentityCrypt; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Validation\ValidationException; +use Mockery; use Oneofftech\Identities\Facades\Identity as IdentityFacade; +use Oneofftech\Identities\Facades\IdentityCrypt; +use SocialiteProviders\GitLab\Provider; use SocialiteProviders\Manager\OAuth2\User as OauthUser; +use Tests\Fixtures\Concern\UseTestFixtures; +use Tests\Fixtures\User; +use Tests\TestCase; class RegistrationControllerTest extends TestCase { @@ -22,7 +22,7 @@ public function test_redirect_to_provider() $response = $this->get(route('oneofftech::register.provider', ['provider' => 'gitlab'])); $response->assertRedirect(); - + $location = urldecode($response->headers->get('Location')); $this->assertStringContainsString('gitlab.com', $location); @@ -37,15 +37,15 @@ public function test_user_can_be_registered() $driverMock = Mockery::mock(Provider::class)->makePartial(); - $oauthFakeUser = (new OauthUser())->map([ - 'id' => 'U1', + $oauthFakeUser = (new OauthUser)->map([ + 'id' => 'U1', 'nickname' => 'User', - 'name' => 'User', - 'email' => 'user@local.com', - 'avatar' => 'https://gitlab.com', - 'token' => 'T1', + 'name' => 'User', + 'email' => 'user@local.com', + 'avatar' => 'https://gitlab.com', + 'token' => 'T1', ]); - + $driverMock->shouldReceive('user')->andReturn($oauthFakeUser); $driverMock->shouldReceive('redirectUrl')->andReturn($driverMock); @@ -55,7 +55,7 @@ public function test_user_can_be_registered() $response = $this->get(route('oneofftech::register.callback', ['provider' => 'gitlab'])); $response->assertRedirect('http://localhost/home'); - + $user = User::first(); $this->assertNotNull($user); @@ -83,15 +83,15 @@ public function test_user_cannot_register_twice() $driverMock = Mockery::mock(Provider::class)->makePartial(); - $oauthFakeUser = (new OauthUser())->map([ - 'id' => 'U1', + $oauthFakeUser = (new OauthUser)->map([ + 'id' => 'U1', 'nickname' => 'User', - 'name' => 'User', - 'email' => 'user@local.com', - 'avatar' => 'https://gitlab.com', - 'token' => 'T1', + 'name' => 'User', + 'email' => 'user@local.com', + 'avatar' => 'https://gitlab.com', + 'token' => 'T1', ]); - + $driverMock->shouldReceive('user')->andReturn($oauthFakeUser); $driverMock->shouldReceive('redirectUrl')->andReturn($driverMock); diff --git a/tests/Unit/ScaffoldAuthenticationControllersTest.php b/tests/Unit/ScaffoldAuthenticationControllersTest.php index 3ec0818..a719318 100644 --- a/tests/Unit/ScaffoldAuthenticationControllersTest.php +++ b/tests/Unit/ScaffoldAuthenticationControllersTest.php @@ -2,10 +2,10 @@ namespace Tests\Unit; -use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\File; +use Tests\TestCase; class ScaffoldAuthenticationControllersTest extends TestCase {