From 124b539df1ee7189ba26a2c7acd5e864f6b8c1cd Mon Sep 17 00:00:00 2001 From: Alessio Date: Sun, 9 Feb 2025 23:20:34 +0100 Subject: [PATCH 01/11] Initial changes to support Laravel 11 --- .github/workflows/ci.yml | 6 ++--- composer.json | 12 ++++----- .../ScaffoldAuthenticationControllers.php | 2 +- tests/TestCase.php | 1 + tests/Unit/ConnectControllerTest.php | 2 ++ tests/Unit/IdentityLinkTest.php | 1 - .../Unit/IdentityModelsConfigurationTest.php | 1 - tests/Unit/IdentityServiceProviderTest.php | 5 +++- tests/Unit/IdentityTraitTest.php | 2 ++ tests/Unit/InteractWithPreviousUrlTest.php | 12 ++++----- ...ditionalAttributesDefinedViaMethodTest.php | 26 ++++++++----------- ...tionalAttributesDefinedViaPropertyTest.php | 16 +++++------- .../InteractsWithAdditionalAttributesTest.php | 17 ++++++------ tests/Unit/LoginControllerTest.php | 2 ++ tests/Unit/NamespaceConfigurationTest.php | 6 +++-- tests/Unit/RegistrationControllerTest.php | 2 ++ .../ScaffoldAuthenticationControllersTest.php | 1 - 17 files changed, 60 insertions(+), 54 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 733f9ee..9744f21 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,11 +21,11 @@ jobs: matrix: os: [ubuntu-latest, windows-latest] php: [8.3, 8.4] - laravel: ["10.*"] + laravel: ["11.*"] stability: [prefer-lowest, prefer-stable] include: - - laravel: 10.* - testbench: 8.* + - laravel: 11.* + testbench: 9.* carbon: ^2.63 steps: diff --git a/composer.json b/composer.json index 0550b26..2d17f3a 100644 --- a/composer.json +++ b/composer.json @@ -18,11 +18,11 @@ ], "require": { "php": "^8.2", - "illuminate/auth": "^10.0", - "illuminate/console": "^10.0", - "illuminate/encryption": "^10.0", - "illuminate/support": "^10.0", - "illuminate/view": "^10.0", + "illuminate/auth": "^11.0", + "illuminate/console": "^11.0", + "illuminate/encryption": "^11.0", + "illuminate/support": "^11.0", + "illuminate/view": "^11.0", "laravel/socialite": "^5.0", "socialiteproviders/gitlab": "^4.0", "socialiteproviders/dropbox": "^4.1" @@ -33,7 +33,7 @@ "larastan/larastan": "^2.9", "mockery/mockery": "^1.4.4", "phpunit/phpunit": "^10.5", - "orchestra/testbench": "^8.22" + "orchestra/testbench": "^9.9" }, "autoload": { "psr-4": { diff --git a/src/Console/Commands/ScaffoldAuthenticationControllers.php b/src/Console/Commands/ScaffoldAuthenticationControllers.php index 7f40fca..bb00dcd 100644 --- a/src/Console/Commands/ScaffoldAuthenticationControllers.php +++ b/src/Console/Commands/ScaffoldAuthenticationControllers.php @@ -97,7 +97,7 @@ protected function identifyApplicationNamespace() $this->comment("Using [$this->namespace] as application namespace."); } - $this->modelNamespace = is_dir(app_path('Models')) ? $this->namespace.'\\Models' : $this->namespace; + $this->modelNamespace = is_dir(app_path('Models')) ? $this->namespace.'\Models' : $this->namespace; } catch (RuntimeException $ex) { $this->warn("Unable to identity the application namespace, assuming [$this->namespace]."); } diff --git a/tests/TestCase.php b/tests/TestCase.php index ea3bf13..34f03ef 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -59,6 +59,7 @@ protected function defineEnvironment($app) 'database' => ':memory:', 'prefix' => '', ]); + config()->set('session.driver', 'array'); config()->set('services.gitlab', [ 'client_id' => 'aaa', 'client_secret' => 'bbb', diff --git a/tests/Unit/ConnectControllerTest.php b/tests/Unit/ConnectControllerTest.php index 4f99a73..8687d75 100644 --- a/tests/Unit/ConnectControllerTest.php +++ b/tests/Unit/ConnectControllerTest.php @@ -9,12 +9,14 @@ use Mockery; use Oneofftech\Identities\Facades\Identity as IdentityFacade; use Oneofftech\Identities\Facades\IdentityCrypt; +use Orchestra\Testbench\Attributes\WithMigration; use SocialiteProviders\GitLab\Provider; use SocialiteProviders\Manager\OAuth2\User as OauthUser; use Tests\Fixtures\Concern\UseTestFixtures; use Tests\Fixtures\User; use Tests\TestCase; +#[WithMigration] class ConnectControllerTest extends TestCase { use RefreshDatabase, UseTestFixtures; diff --git a/tests/Unit/IdentityLinkTest.php b/tests/Unit/IdentityLinkTest.php index 17bda0c..e87e219 100644 --- a/tests/Unit/IdentityLinkTest.php +++ b/tests/Unit/IdentityLinkTest.php @@ -11,7 +11,6 @@ class IdentityLinkTest extends TestCase { - use DatabaseMigrations; protected function setUp(): void { diff --git a/tests/Unit/IdentityModelsConfigurationTest.php b/tests/Unit/IdentityModelsConfigurationTest.php index 472bac5..d74d96e 100644 --- a/tests/Unit/IdentityModelsConfigurationTest.php +++ b/tests/Unit/IdentityModelsConfigurationTest.php @@ -10,7 +10,6 @@ class IdentityModelsConfigurationTest extends TestCase { - use DatabaseMigrations; public function test_default_models_uses_app_namespace() { diff --git a/tests/Unit/IdentityServiceProviderTest.php b/tests/Unit/IdentityServiceProviderTest.php index 612ac00..4c8452f 100644 --- a/tests/Unit/IdentityServiceProviderTest.php +++ b/tests/Unit/IdentityServiceProviderTest.php @@ -3,20 +3,23 @@ namespace Tests\Unit; use Illuminate\Foundation\Testing\DatabaseMigrations; +use Illuminate\Foundation\Testing\RefreshDatabase; use InvalidArgumentException; use Laravel\Socialite\Two\FacebookProvider; use Oneofftech\Identities\Facades\Identity; use Oneofftech\Identities\IdentitiesManager; use Oneofftech\Identities\Providers\IdentitiesServiceProvider; +use Orchestra\Testbench\Attributes\WithMigration; use SocialiteProviders\Dropbox\Provider as DropboxDriver; use SocialiteProviders\GitLab\GitLabExtendSocialite; use SocialiteProviders\GitLab\Provider as GitlabSocialiteProvider; use SocialiteProviders\Manager\SocialiteWasCalled; use Tests\TestCase; +#[WithMigration] class IdentityServiceProviderTest extends TestCase { - use DatabaseMigrations; + use RefreshDatabase; public function test_default_driver_cannot_not_configured() { diff --git a/tests/Unit/IdentityTraitTest.php b/tests/Unit/IdentityTraitTest.php index 3b76bd3..184f2e9 100644 --- a/tests/Unit/IdentityTraitTest.php +++ b/tests/Unit/IdentityTraitTest.php @@ -8,10 +8,12 @@ use Oneofftech\Identities\Facades\Identity as IdentityFacade; use Oneofftech\Identities\Facades\IdentityCrypt; use Oneofftech\Identities\Support\FindIdentity; +use Orchestra\Testbench\Attributes\WithMigration; use Tests\Fixtures\Identity; use Tests\Fixtures\User; use Tests\TestCase; +#[WithMigration] class IdentityTraitTest extends TestCase { use FindIdentity, RefreshDatabase; diff --git a/tests/Unit/InteractWithPreviousUrlTest.php b/tests/Unit/InteractWithPreviousUrlTest.php index 2bcf3e6..a5c8c7c 100644 --- a/tests/Unit/InteractWithPreviousUrlTest.php +++ b/tests/Unit/InteractWithPreviousUrlTest.php @@ -12,19 +12,19 @@ class InteractWithPreviousUrlTest extends TestCase public function test_previous_url_is_stored() { - Session::shouldReceive('previousUrl')->andReturn('http://localhost/previous'); - - Session::shouldReceive('put')->once()->with('_oot.identities.previous_url', 'http://localhost/previous'); + Session::put('_previous.url', 'http://localhost/previous'); $this->savePreviousUrl(); + + $this->assertEquals('http://localhost/previous', Session::get('_oot.identities.previous_url')); } public function test_previous_url_can_be_retrieved() { - Session::shouldReceive('previousUrl')->andReturnNull(); - - Session::shouldReceive('pull')->once()->with('_oot.identities.previous_url', 'http://localhost')->andReturn('http://localhost/previous'); + Session::put('_previous.url', null); + Session::put('_oot.identities.previous_url', 'http://localhost/previous'); + $url = $this->getPreviousUrl(); $this->assertEquals('http://localhost/previous', $url); diff --git a/tests/Unit/InteractsWithAdditionalAttributesDefinedViaMethodTest.php b/tests/Unit/InteractsWithAdditionalAttributesDefinedViaMethodTest.php index 22d21d2..81e7aab 100644 --- a/tests/Unit/InteractsWithAdditionalAttributesDefinedViaMethodTest.php +++ b/tests/Unit/InteractsWithAdditionalAttributesDefinedViaMethodTest.php @@ -25,45 +25,41 @@ public function test_attributes_can_be_defined() public function test_attributes_are_saved() { - Session::shouldReceive('put')->once()->with('_oot.identities.attributes', '{"attribute":"value"}'); - $request = Request::create('http://localhost', 'GET', [ 'attribute' => 'value', ]); - $request->setLaravelSession(Session::getFacadeRoot()); + $request->setLaravelSession(Session::driver()); $this->pushAttributes($request); + + $this->assertEquals('{"attribute":"value"}', Session::get('_oot.identities.attributes')); } public function test_attributes_are_retrieved() { - Session::shouldReceive('previousUrl')->andReturnNull(); - - Session::shouldReceive('pull')->once() - ->with('_oot.identities.attributes') - ->andReturn('{"attribute":"http://localhost/previous"}'); + Session::put('_oot.identities.attributes', '{"attribute":"http://localhost/previous"}'); $request = Request::create('http://localhost/callback'); - $request->setLaravelSession(Session::getFacadeRoot()); + $request->setLaravelSession(Session::driver()); $data = $this->pullAttributes($request); $this->assertEquals(['attribute' => 'http://localhost/previous'], $data); + + $this->assertNull(Session::previousUrl()); } public function test_attributes_not_retrieved_if_nothing_saved() { - Session::shouldReceive('previousUrl')->andReturnNull(); - - Session::shouldReceive('pull')->once() - ->with('_oot.identities.attributes') - ->andReturn(null); + Session::put('_oot.identities.attributes', null); $request = Request::create('http://localhost/callback'); - $request->setLaravelSession(Session::getFacadeRoot()); + $request->setLaravelSession(Session::driver()); $data = $this->pullAttributes($request); $this->assertEquals([], $data); + + $this->assertNull(Session::previousUrl()); } } diff --git a/tests/Unit/InteractsWithAdditionalAttributesDefinedViaPropertyTest.php b/tests/Unit/InteractsWithAdditionalAttributesDefinedViaPropertyTest.php index 601d095..d1cc224 100644 --- a/tests/Unit/InteractsWithAdditionalAttributesDefinedViaPropertyTest.php +++ b/tests/Unit/InteractsWithAdditionalAttributesDefinedViaPropertyTest.php @@ -22,29 +22,27 @@ public function test_attributes_can_be_defined() public function test_attributes_are_saved() { - Session::shouldReceive('put')->once()->with('_oot.identities.attributes', '{"attribute":"value"}'); - $request = Request::create('http://localhost', 'GET', [ 'attribute' => 'value', ]); - $request->setLaravelSession(Session::getFacadeRoot()); + $request->setLaravelSession(Session::driver()); $this->pushAttributes($request); + + $this->assertEquals('{"attribute":"value"}', Session::get('_oot.identities.attributes')); + $this->assertNull(Session::previousUrl()); } public function test_attributes_are_retrieved() { - Session::shouldReceive('previousUrl')->andReturnNull(); - - Session::shouldReceive('pull')->once() - ->with('_oot.identities.attributes') - ->andReturn('{"attribute":"http://localhost/previous"}'); + Session::put('_oot.identities.attributes','{"attribute":"http://localhost/previous"}'); $request = Request::create('http://localhost/callback'); - $request->setLaravelSession(Session::getFacadeRoot()); + $request->setLaravelSession(Session::driver()); $data = $this->pullAttributes($request); $this->assertEquals(['attribute' => 'http://localhost/previous'], $data); + $this->assertNull(Session::previousUrl()); } } diff --git a/tests/Unit/InteractsWithAdditionalAttributesTest.php b/tests/Unit/InteractsWithAdditionalAttributesTest.php index 024fb96..8fe92ac 100644 --- a/tests/Unit/InteractsWithAdditionalAttributesTest.php +++ b/tests/Unit/InteractsWithAdditionalAttributesTest.php @@ -3,6 +3,7 @@ namespace Tests\Unit; use Illuminate\Http\Request; +use Illuminate\Session\SessionManager; use Illuminate\Support\Facades\Session; use Oneofftech\Identities\Support\InteractsWithAdditionalAttributes; use Tests\TestCase; @@ -20,27 +21,27 @@ public function test_attributes_not_defined() public function test_nothing_is_saved() { - Session::shouldReceive('put')->never(); - $request = Request::create('http://localhost', 'GET', [ 'attribute' => 'value', ]); - $request->setLaravelSession(Session::getFacadeRoot()); + + $request->setLaravelSession(Session::driver()); $this->pushAttributes($request); + + $this->assertTrue(Session::missing('_oot.identities.attributes')); } public function test_nothing_is_retrieved() { - Session::shouldReceive('previousUrl')->andReturnNull(); - - Session::shouldReceive('pull')->never(); - $request = Request::create('http://localhost/callback'); - $request->setLaravelSession(Session::getFacadeRoot()); + + $request->setLaravelSession(Session::driver()); $data = $this->pullAttributes($request); $this->assertEquals([], $data); + + $this->assertTrue(Session::missing('_oot.identities.attributes')); } } diff --git a/tests/Unit/LoginControllerTest.php b/tests/Unit/LoginControllerTest.php index afa2581..9787dd4 100644 --- a/tests/Unit/LoginControllerTest.php +++ b/tests/Unit/LoginControllerTest.php @@ -10,8 +10,10 @@ use SocialiteProviders\GitLab\Provider; use SocialiteProviders\Manager\OAuth2\User as OauthUser; use Tests\Fixtures\Concern\UseTestFixtures; +use Orchestra\Testbench\Attributes\WithMigration; use Tests\TestCase; +#[WithMigration] class LoginControllerTest extends TestCase { use RefreshDatabase, UseTestFixtures; diff --git a/tests/Unit/NamespaceConfigurationTest.php b/tests/Unit/NamespaceConfigurationTest.php index 0a908b8..a89fc56 100644 --- a/tests/Unit/NamespaceConfigurationTest.php +++ b/tests/Unit/NamespaceConfigurationTest.php @@ -2,13 +2,15 @@ namespace Tests\Unit; -use Illuminate\Foundation\Testing\DatabaseMigrations; +use Illuminate\Foundation\Testing\RefreshDatabase; use Oneofftech\Identities\Facades\Identity as IdentityFacade; +use Orchestra\Testbench\Attributes\WithMigration; use Tests\TestCase; +#[WithMigration] class NamespaceConfigurationTest extends TestCase { - use DatabaseMigrations; + use RefreshDatabase; public function test_default_app_namespace() { diff --git a/tests/Unit/RegistrationControllerTest.php b/tests/Unit/RegistrationControllerTest.php index 9722aed..1f2f6b3 100644 --- a/tests/Unit/RegistrationControllerTest.php +++ b/tests/Unit/RegistrationControllerTest.php @@ -7,12 +7,14 @@ use Mockery; use Oneofftech\Identities\Facades\Identity as IdentityFacade; use Oneofftech\Identities\Facades\IdentityCrypt; +use Orchestra\Testbench\Attributes\WithMigration; use SocialiteProviders\GitLab\Provider; use SocialiteProviders\Manager\OAuth2\User as OauthUser; use Tests\Fixtures\Concern\UseTestFixtures; use Tests\Fixtures\User; use Tests\TestCase; +#[WithMigration] class RegistrationControllerTest extends TestCase { use RefreshDatabase, UseTestFixtures; diff --git a/tests/Unit/ScaffoldAuthenticationControllersTest.php b/tests/Unit/ScaffoldAuthenticationControllersTest.php index a719318..2b46361 100644 --- a/tests/Unit/ScaffoldAuthenticationControllersTest.php +++ b/tests/Unit/ScaffoldAuthenticationControllersTest.php @@ -9,7 +9,6 @@ class ScaffoldAuthenticationControllersTest extends TestCase { - use DatabaseMigrations; public function test_scaffold() { From 740afc71aaa44690adbe826f6763fa5efdd877e6 Mon Sep 17 00:00:00 2001 From: avvertix <5672748+avvertix@users.noreply.github.com> Date: Sun, 9 Feb 2025 22:21:01 +0000 Subject: [PATCH 02/11] Fix styling --- tests/Unit/IdentityLinkTest.php | 2 -- tests/Unit/IdentityModelsConfigurationTest.php | 2 -- tests/Unit/IdentityServiceProviderTest.php | 1 - tests/Unit/InteractWithPreviousUrlTest.php | 2 +- ...InteractsWithAdditionalAttributesDefinedViaPropertyTest.php | 2 +- tests/Unit/InteractsWithAdditionalAttributesTest.php | 3 +-- tests/Unit/LoginControllerTest.php | 2 +- tests/Unit/ScaffoldAuthenticationControllersTest.php | 2 -- 8 files changed, 4 insertions(+), 12 deletions(-) diff --git a/tests/Unit/IdentityLinkTest.php b/tests/Unit/IdentityLinkTest.php index e87e219..9620fce 100644 --- a/tests/Unit/IdentityLinkTest.php +++ b/tests/Unit/IdentityLinkTest.php @@ -2,7 +2,6 @@ namespace Tests\Unit; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\View\Component; use InvalidArgumentException; use Oneofftech\Identities\Facades\Identity; @@ -11,7 +10,6 @@ class IdentityLinkTest extends TestCase { - protected function setUp(): void { parent::setUp(); diff --git a/tests/Unit/IdentityModelsConfigurationTest.php b/tests/Unit/IdentityModelsConfigurationTest.php index d74d96e..1ebda68 100644 --- a/tests/Unit/IdentityModelsConfigurationTest.php +++ b/tests/Unit/IdentityModelsConfigurationTest.php @@ -2,7 +2,6 @@ namespace Tests\Unit; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Oneofftech\Identities\Facades\Identity as IdentityFacade; use Tests\Fixtures\Identity; use Tests\Fixtures\User; @@ -10,7 +9,6 @@ class IdentityModelsConfigurationTest extends TestCase { - public function test_default_models_uses_app_namespace() { $this->assertEquals('App\\User', IdentityFacade::userModel()); diff --git a/tests/Unit/IdentityServiceProviderTest.php b/tests/Unit/IdentityServiceProviderTest.php index 4c8452f..053782f 100644 --- a/tests/Unit/IdentityServiceProviderTest.php +++ b/tests/Unit/IdentityServiceProviderTest.php @@ -2,7 +2,6 @@ namespace Tests\Unit; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\RefreshDatabase; use InvalidArgumentException; use Laravel\Socialite\Two\FacebookProvider; diff --git a/tests/Unit/InteractWithPreviousUrlTest.php b/tests/Unit/InteractWithPreviousUrlTest.php index a5c8c7c..fe453a1 100644 --- a/tests/Unit/InteractWithPreviousUrlTest.php +++ b/tests/Unit/InteractWithPreviousUrlTest.php @@ -24,7 +24,7 @@ public function test_previous_url_can_be_retrieved() Session::put('_previous.url', null); Session::put('_oot.identities.previous_url', 'http://localhost/previous'); - + $url = $this->getPreviousUrl(); $this->assertEquals('http://localhost/previous', $url); diff --git a/tests/Unit/InteractsWithAdditionalAttributesDefinedViaPropertyTest.php b/tests/Unit/InteractsWithAdditionalAttributesDefinedViaPropertyTest.php index d1cc224..49994ba 100644 --- a/tests/Unit/InteractsWithAdditionalAttributesDefinedViaPropertyTest.php +++ b/tests/Unit/InteractsWithAdditionalAttributesDefinedViaPropertyTest.php @@ -35,7 +35,7 @@ public function test_attributes_are_saved() public function test_attributes_are_retrieved() { - Session::put('_oot.identities.attributes','{"attribute":"http://localhost/previous"}'); + Session::put('_oot.identities.attributes', '{"attribute":"http://localhost/previous"}'); $request = Request::create('http://localhost/callback'); $request->setLaravelSession(Session::driver()); diff --git a/tests/Unit/InteractsWithAdditionalAttributesTest.php b/tests/Unit/InteractsWithAdditionalAttributesTest.php index 8fe92ac..3f1f7e8 100644 --- a/tests/Unit/InteractsWithAdditionalAttributesTest.php +++ b/tests/Unit/InteractsWithAdditionalAttributesTest.php @@ -3,7 +3,6 @@ namespace Tests\Unit; use Illuminate\Http\Request; -use Illuminate\Session\SessionManager; use Illuminate\Support\Facades\Session; use Oneofftech\Identities\Support\InteractsWithAdditionalAttributes; use Tests\TestCase; @@ -35,7 +34,7 @@ public function test_nothing_is_saved() public function test_nothing_is_retrieved() { $request = Request::create('http://localhost/callback'); - + $request->setLaravelSession(Session::driver()); $data = $this->pullAttributes($request); diff --git a/tests/Unit/LoginControllerTest.php b/tests/Unit/LoginControllerTest.php index 9787dd4..13b7c4c 100644 --- a/tests/Unit/LoginControllerTest.php +++ b/tests/Unit/LoginControllerTest.php @@ -7,10 +7,10 @@ use Mockery; use Oneofftech\Identities\Facades\Identity as IdentityFacade; use Oneofftech\Identities\Facades\IdentityCrypt; +use Orchestra\Testbench\Attributes\WithMigration; use SocialiteProviders\GitLab\Provider; use SocialiteProviders\Manager\OAuth2\User as OauthUser; use Tests\Fixtures\Concern\UseTestFixtures; -use Orchestra\Testbench\Attributes\WithMigration; use Tests\TestCase; #[WithMigration] diff --git a/tests/Unit/ScaffoldAuthenticationControllersTest.php b/tests/Unit/ScaffoldAuthenticationControllersTest.php index 2b46361..0c42e5d 100644 --- a/tests/Unit/ScaffoldAuthenticationControllersTest.php +++ b/tests/Unit/ScaffoldAuthenticationControllersTest.php @@ -2,14 +2,12 @@ namespace Tests\Unit; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\File; use Tests\TestCase; class ScaffoldAuthenticationControllersTest extends TestCase { - public function test_scaffold() { $webRoutesFile = base_path('routes/web.php'); From fe793a39dd216aa49eb4055a14464fbf9d4ce270 Mon Sep 17 00:00:00 2001 From: Alessio Date: Sun, 9 Feb 2025 23:24:23 +0100 Subject: [PATCH 03/11] Pin testbench version --- .github/workflows/ci.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9744f21..70a6305 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,7 +25,6 @@ jobs: stability: [prefer-lowest, prefer-stable] include: - laravel: 11.* - testbench: 9.* carbon: ^2.63 steps: @@ -53,7 +52,7 @@ jobs: - name: Install dependencies run: | - composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.os == 'windows-latest' && '^^^' || '' }}${{ matrix.carbon }}" --no-interaction --no-update + composer require "laravel/framework:${{ matrix.laravel }}" "nesbot/carbon:${{ matrix.os == 'windows-latest' && '^^^' || '' }}${{ matrix.carbon }}" --no-interaction --no-update composer update --${{ matrix.stability }} --prefer-dist --no-interaction - name: List Installed Dependencies From ca68634f45ce3d9a3cc91d020061b97873c5cc38 Mon Sep 17 00:00:00 2001 From: Alessio Date: Thu, 13 Feb 2025 21:00:41 +0100 Subject: [PATCH 04/11] Use redirectTo method and change default redirect to the welcome page --- src/Auth/RedirectsUsers.php | 6 ++---- stubs/Identities/Auth/ConnectController.stub | 14 ++------------ stubs/Identities/Auth/LoginController.stub | 14 ++------------ stubs/Identities/Auth/RegisterController.stub | 14 ++------------ tests/Unit/ConnectControllerTest.php | 4 ++-- tests/Unit/LoginControllerTest.php | 2 +- tests/Unit/RegistrationControllerTest.php | 2 +- 7 files changed, 12 insertions(+), 44 deletions(-) diff --git a/src/Auth/RedirectsUsers.php b/src/Auth/RedirectsUsers.php index dcf4684..661ffd7 100644 --- a/src/Auth/RedirectsUsers.php +++ b/src/Auth/RedirectsUsers.php @@ -10,15 +10,13 @@ trait RedirectsUsers { /** * Get the post register / login redirect path. - * - * @return string */ - public function redirectPath() + public function redirectPath(): string { if (method_exists($this, 'redirectTo')) { return $this->redirectTo(); } - return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home'; + return property_exists($this, 'redirectTo') ? $this->redirectTo : '/'; } } diff --git a/stubs/Identities/Auth/ConnectController.stub b/stubs/Identities/Auth/ConnectController.stub index 96e8055..e8572f9 100644 --- a/stubs/Identities/Auth/ConnectController.stub +++ b/stubs/Identities/Auth/ConnectController.stub @@ -4,7 +4,6 @@ namespace App\Http\Controllers\Identities\Auth; use App\User; use App\Http\Controllers\Controller; -use App\Providers\RouteServiceProvider; use Oneofftech\Identities\Auth\ConnectUserIdentity; class ConnectController extends Controller @@ -25,18 +24,9 @@ class ConnectController extends Controller /** * Where to redirect users after connection. - * - * @var string */ - protected $redirectTo = RouteServiceProvider::HOME; - - /** - * Create a new controller instance. - * - * @return void - */ - public function __construct() + protected function redirectTo(): string { - $this->middleware('auth'); + return '/'; } } diff --git a/stubs/Identities/Auth/LoginController.stub b/stubs/Identities/Auth/LoginController.stub index d800486..1766c7b 100644 --- a/stubs/Identities/Auth/LoginController.stub +++ b/stubs/Identities/Auth/LoginController.stub @@ -3,7 +3,6 @@ namespace App\Http\Controllers\Identities\Auth; use App\Http\Controllers\Controller; -use App\Providers\RouteServiceProvider; use Oneofftech\Identities\Auth\AuthenticatesUsersWithIdentity; class LoginController extends Controller @@ -24,18 +23,9 @@ class LoginController extends Controller /** * Where to redirect users after login. - * - * @var string */ - protected $redirectTo = RouteServiceProvider::HOME; - - /** - * Create a new controller instance. - * - * @return void - */ - public function __construct() + protected function redirectTo(): string { - $this->middleware('guest'); + return '/'; } } diff --git a/stubs/Identities/Auth/RegisterController.stub b/stubs/Identities/Auth/RegisterController.stub index 863bfe3..4787759 100644 --- a/stubs/Identities/Auth/RegisterController.stub +++ b/stubs/Identities/Auth/RegisterController.stub @@ -6,7 +6,6 @@ use App\User; use Illuminate\Support\Str; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Hash; -use App\Providers\RouteServiceProvider; use Illuminate\Support\Facades\Validator; use Oneofftech\Identities\Auth\RegistersUsersWithIdentity; @@ -27,19 +26,10 @@ class RegisterController extends Controller /** * Where to redirect users after registration. - * - * @var string - */ - protected $redirectTo = RouteServiceProvider::HOME; - - /** - * Create a new controller instance. - * - * @return void */ - public function __construct() + protected function redirectTo(): string { - $this->middleware('guest'); + return '/'; } /** diff --git a/tests/Unit/ConnectControllerTest.php b/tests/Unit/ConnectControllerTest.php index 8687d75..1a3b331 100644 --- a/tests/Unit/ConnectControllerTest.php +++ b/tests/Unit/ConnectControllerTest.php @@ -93,7 +93,7 @@ public function test_connect_creates_identity() $response = $this->actingAs($user) ->get(route('oneofftech::connect.callback', ['provider' => 'gitlab'])); - $response->assertRedirect('http://localhost/home'); + $response->assertRedirect('http://localhost'); $updatedIdentity = $user->identities->first(); @@ -144,7 +144,7 @@ public function test_connect_updates_existing_identity() $response = $this->actingAs($user) ->get(route('oneofftech::connect.callback', ['provider' => 'gitlab'])); - $response->assertRedirect('http://localhost/home'); + $response->assertRedirect('http://localhost'); $updatedIdentity = $user->identities->first(); diff --git a/tests/Unit/LoginControllerTest.php b/tests/Unit/LoginControllerTest.php index 13b7c4c..b701cde 100644 --- a/tests/Unit/LoginControllerTest.php +++ b/tests/Unit/LoginControllerTest.php @@ -67,7 +67,7 @@ public function test_user_login() $response = $this->get(route('oneofftech::login.callback', ['provider' => 'gitlab'])); - $response->assertRedirect('http://localhost/home'); + $response->assertRedirect('http://localhost'); $this->assertAuthenticatedAs($user); } diff --git a/tests/Unit/RegistrationControllerTest.php b/tests/Unit/RegistrationControllerTest.php index 1f2f6b3..95a009b 100644 --- a/tests/Unit/RegistrationControllerTest.php +++ b/tests/Unit/RegistrationControllerTest.php @@ -56,7 +56,7 @@ public function test_user_can_be_registered() $response = $this->get(route('oneofftech::register.callback', ['provider' => 'gitlab'])); - $response->assertRedirect('http://localhost/home'); + $response->assertRedirect('http://localhost'); $user = User::first(); From 47000f01053f6ec644aa326052cd7420ce721fdc Mon Sep 17 00:00:00 2001 From: Alessio Date: Thu, 13 Feb 2025 21:02:28 +0100 Subject: [PATCH 05/11] Defaults to Models directory --- .../ScaffoldAuthenticationControllers.php | 2 +- src/Facades/Identity.php | 40 ++++++++++++------- stubs/Identities/Auth/RegisterController.stub | 4 +- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/Console/Commands/ScaffoldAuthenticationControllers.php b/src/Console/Commands/ScaffoldAuthenticationControllers.php index bb00dcd..7e98a2a 100644 --- a/src/Console/Commands/ScaffoldAuthenticationControllers.php +++ b/src/Console/Commands/ScaffoldAuthenticationControllers.php @@ -97,7 +97,7 @@ protected function identifyApplicationNamespace() $this->comment("Using [$this->namespace] as application namespace."); } - $this->modelNamespace = is_dir(app_path('Models')) ? $this->namespace.'\Models' : $this->namespace; + $this->modelNamespace = is_dir(app_path('Models')) ? $this->namespace.'Models' : $this->namespace; } catch (RuntimeException $ex) { $this->warn("Unable to identity the application namespace, assuming [$this->namespace]."); } diff --git a/src/Facades/Identity.php b/src/Facades/Identity.php index c4a9613..ecf2161 100644 --- a/src/Facades/Identity.php +++ b/src/Facades/Identity.php @@ -26,14 +26,14 @@ class Identity extends Facade * * @var string */ - public static $userModel = 'App\\User'; + public static $userModel = 'App\\Models\\User'; /** * The identity model that should be used. * * @var string */ - public static $identityModel = 'App\\Identity'; + public static $identityModel = 'App\\Models\\Identity'; /** * Get the registered name of the component. @@ -53,24 +53,34 @@ protected static function getFacadeAccessor() */ public static function routes() { + /** + * @var \Illuminate\Routing\Router + */ $router = static::$app->make('router'); $namespace = '\\'.rtrim(self::$appNamespace, '\\'); - $router->match(['get', 'post'], 'login-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\LoginController@redirect") - ->name('oneofftech::login.provider'); - $router->get('login-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\LoginController@login") - ->name('oneofftech::login.callback'); + $router + ->middleware('web') + ->group(function($groupRouter) use ($namespace) { + $groupRouter->match(['get', 'post'], 'login-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\LoginController@redirect") + ->name('oneofftech::login.provider'); + $groupRouter->get('login-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\LoginController@login") + ->name('oneofftech::login.callback'); + + $groupRouter->match(['get', 'post'], 'register-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\RegisterController@redirect") + ->name('oneofftech::register.provider'); + $groupRouter->get('register-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\RegisterController@register") + ->name('oneofftech::register.callback'); + + $groupRouter->match(['get', 'post'], 'connect-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\ConnectController@redirect") + ->middleware('auth') + ->name('oneofftech::connect.provider'); + $groupRouter->get('connect-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\ConnectController@connect") + ->middleware('auth') + ->name('oneofftech::connect.callback'); + }); - $router->match(['get', 'post'], 'register-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\RegisterController@redirect") - ->name('oneofftech::register.provider'); - $router->get('register-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\RegisterController@register") - ->name('oneofftech::register.callback'); - - $router->match(['get', 'post'], 'connect-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\ConnectController@redirect") - ->name('oneofftech::connect.provider'); - $router->get('connect-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\ConnectController@connect") - ->name('oneofftech::connect.callback'); } /** diff --git a/stubs/Identities/Auth/RegisterController.stub b/stubs/Identities/Auth/RegisterController.stub index 4787759..29cc190 100644 --- a/stubs/Identities/Auth/RegisterController.stub +++ b/stubs/Identities/Auth/RegisterController.stub @@ -2,7 +2,7 @@ namespace App\Http\Controllers\Identities\Auth; -use App\User; +use App\Models\User; use Illuminate\Support\Str; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Hash; @@ -58,7 +58,7 @@ class RegisterController extends Controller return User::create([ 'name' => $data['name'], 'email' => $data['email'], - 'password' => Hash::make($data['password'] ?? Str::random(20)), + 'password' => Hash::make($data['password'] ?? Str::random(30)), ]); } } From be08ed12c8c39e3fdc9a5af4ab0eaecaeefd84a3 Mon Sep 17 00:00:00 2001 From: Alessio Date: Thu, 13 Feb 2025 21:02:52 +0100 Subject: [PATCH 06/11] Switch to casts() method --- stubs/Identities/Models/Identity.stub | 28 +++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/stubs/Identities/Models/Identity.stub b/stubs/Identities/Models/Identity.stub index 4cc85b4..c9ebdd1 100644 --- a/stubs/Identities/Models/Identity.stub +++ b/stubs/Identities/Models/Identity.stub @@ -3,20 +3,11 @@ namespace App; use Illuminate\Database\Eloquent\Model; - +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Oneofftech\Identities\Facades\Identity as IdentityFacade; class Identity extends Model { - - /** - * @var array - */ - protected $casts = [ - 'expires_at' => 'datetime', - 'registration' => 'bool', - ]; - /** * @var array */ @@ -39,11 +30,24 @@ class Identity extends Model 'refresh_token', 'expires_at', ]; - + /** + * Get the attributes that should be cast. * + * @return array + */ + protected function casts(): array + { + return [ + 'expires_at' => 'datetime', + 'registration' => 'bool', + ]; + } + + /** + * The user to whom this identity belongs. */ - public function user() + public function user(): BelongsTo { return $this->belongsTo(IdentityFacade::userModel()); } From 6ecd1065351a0254fca1f8996f46de2c0d31690d Mon Sep 17 00:00:00 2001 From: Alessio Date: Thu, 13 Feb 2025 21:04:02 +0100 Subject: [PATCH 07/11] Make the default controller empty and do not use middlewares in controllers --- tests/Fixtures/Http/Controllers/Controller.php | 9 ++------- .../Controllers/Identities/Auth/ConnectController.php | 10 ---------- .../Controllers/Identities/Auth/LoginController.php | 9 --------- .../Controllers/Identities/Auth/RegisterController.php | 10 ---------- 4 files changed, 2 insertions(+), 36 deletions(-) diff --git a/tests/Fixtures/Http/Controllers/Controller.php b/tests/Fixtures/Http/Controllers/Controller.php index 487a593..72cecd6 100644 --- a/tests/Fixtures/Http/Controllers/Controller.php +++ b/tests/Fixtures/Http/Controllers/Controller.php @@ -2,12 +2,7 @@ namespace Tests\Fixtures\Http\Controllers; -use Illuminate\Foundation\Auth\Access\AuthorizesRequests; -use Illuminate\Foundation\Bus\DispatchesJobs; -use Illuminate\Foundation\Validation\ValidatesRequests; -use Illuminate\Routing\Controller as BaseController; - -class Controller extends BaseController +abstract class Controller { - use AuthorizesRequests, DispatchesJobs, ValidatesRequests; + } diff --git a/tests/Fixtures/Http/Controllers/Identities/Auth/ConnectController.php b/tests/Fixtures/Http/Controllers/Identities/Auth/ConnectController.php index 2394746..cf7a649 100644 --- a/tests/Fixtures/Http/Controllers/Identities/Auth/ConnectController.php +++ b/tests/Fixtures/Http/Controllers/Identities/Auth/ConnectController.php @@ -23,16 +23,6 @@ class ConnectController extends Controller use ConnectUserIdentity; - /** - * Create a new controller instance. - * - * @return void - */ - public function __construct() - { - $this->middleware('auth'); - } - /** * Get a validator for an incoming connection request. * diff --git a/tests/Fixtures/Http/Controllers/Identities/Auth/LoginController.php b/tests/Fixtures/Http/Controllers/Identities/Auth/LoginController.php index 1eb4ff7..43d7611 100644 --- a/tests/Fixtures/Http/Controllers/Identities/Auth/LoginController.php +++ b/tests/Fixtures/Http/Controllers/Identities/Auth/LoginController.php @@ -21,13 +21,4 @@ class LoginController extends Controller use AuthenticatesUsersWithIdentity; - /** - * Create a new controller instance. - * - * @return void - */ - public function __construct() - { - $this->middleware('guest'); - } } diff --git a/tests/Fixtures/Http/Controllers/Identities/Auth/RegisterController.php b/tests/Fixtures/Http/Controllers/Identities/Auth/RegisterController.php index 72c70f3..d3eea2f 100644 --- a/tests/Fixtures/Http/Controllers/Identities/Auth/RegisterController.php +++ b/tests/Fixtures/Http/Controllers/Identities/Auth/RegisterController.php @@ -24,16 +24,6 @@ class RegisterController extends Controller use RegistersUsersWithIdentity; - /** - * Create a new controller instance. - * - * @return void - */ - public function __construct() - { - $this->middleware('guest'); - } - /** * Get a validator for an incoming registration request. * From b4d56d3da6e579e8ab9e0eb0644a564d12e72074 Mon Sep 17 00:00:00 2001 From: Alessio Date: Thu, 13 Feb 2025 21:04:19 +0100 Subject: [PATCH 08/11] Update fixtures to use new conventions --- tests/Fixtures/Identity.php | 24 +++++++++++++++--------- tests/Fixtures/User.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/tests/Fixtures/Identity.php b/tests/Fixtures/Identity.php index aaf48df..7bce28c 100644 --- a/tests/Fixtures/Identity.php +++ b/tests/Fixtures/Identity.php @@ -3,18 +3,11 @@ namespace Tests\Fixtures; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Oneofftech\Identities\Facades\Identity as IdentityFacade; class Identity extends Model { - /** - * @var array - */ - protected $casts = [ - 'expires_at' => 'datetime', - 'registration' => 'bool', - ]; - /** * @var array */ @@ -38,7 +31,20 @@ class Identity extends Model 'expires_at', ]; - public function user() + /** + * Get the attributes that should be cast. + * + * @return array + */ + protected function casts(): array + { + return [ + 'expires_at' => 'datetime', + 'registration' => 'bool', + ]; + } + + public function user(): BelongsTo { return $this->belongsTo(IdentityFacade::userModel()); } diff --git a/tests/Fixtures/User.php b/tests/Fixtures/User.php index 8cede5f..de6879e 100644 --- a/tests/Fixtures/User.php +++ b/tests/Fixtures/User.php @@ -9,9 +9,37 @@ class User extends Authenticatable { use WithIdentities; + /** + * The attributes that are mass assignable. + * + * @var list + */ protected $fillable = [ 'name', 'email', 'password', ]; + + /** + * The attributes that should be hidden for serialization. + * + * @var list + */ + protected $hidden = [ + 'password', + 'remember_token', + ]; + + /** + * Get the attributes that should be cast. + * + * @return array + */ + protected function casts(): array + { + return [ + 'email_verified_at' => 'datetime', + 'password' => 'hashed', + ]; + } } From af923c199f5c2c9b5fc6159a9042d0c81dc7c61a Mon Sep 17 00:00:00 2001 From: Alessio Date: Thu, 13 Feb 2025 21:04:33 +0100 Subject: [PATCH 09/11] Use anonymous classes for migrations --- .../migrations/2020_08_09_115707_create_identities_table.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 2e1a30d..5620e01 100644 --- a/stubs/migrations/2020_08_09_115707_create_identities_table.php +++ b/stubs/migrations/2020_08_09_115707_create_identities_table.php @@ -4,7 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateIdentitiesTable extends Migration +return new class extends Migration { /** * Run the migrations. @@ -35,4 +35,4 @@ public function down() { Schema::dropIfExists('identities'); } -} +}; From 2801dc5f6100aa9d4840343699dfb6c761dd094d Mon Sep 17 00:00:00 2001 From: avvertix <5672748+avvertix@users.noreply.github.com> Date: Thu, 13 Feb 2025 20:05:16 +0000 Subject: [PATCH 10/11] Fix styling --- src/Facades/Identity.php | 2 +- tests/Fixtures/Http/Controllers/Controller.php | 5 +---- .../Http/Controllers/Identities/Auth/LoginController.php | 1 - 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Facades/Identity.php b/src/Facades/Identity.php index ecf2161..ebaf354 100644 --- a/src/Facades/Identity.php +++ b/src/Facades/Identity.php @@ -62,7 +62,7 @@ public static function routes() $router ->middleware('web') - ->group(function($groupRouter) use ($namespace) { + ->group(function ($groupRouter) use ($namespace) { $groupRouter->match(['get', 'post'], 'login-via/{provider}', "$namespace\Http\Controllers\Identities\Auth\LoginController@redirect") ->name('oneofftech::login.provider'); $groupRouter->get('login-via/{provider}/callback', "$namespace\Http\Controllers\Identities\Auth\LoginController@login") diff --git a/tests/Fixtures/Http/Controllers/Controller.php b/tests/Fixtures/Http/Controllers/Controller.php index 72cecd6..e3c1a8b 100644 --- a/tests/Fixtures/Http/Controllers/Controller.php +++ b/tests/Fixtures/Http/Controllers/Controller.php @@ -2,7 +2,4 @@ namespace Tests\Fixtures\Http\Controllers; -abstract class Controller -{ - -} +abstract class Controller {} diff --git a/tests/Fixtures/Http/Controllers/Identities/Auth/LoginController.php b/tests/Fixtures/Http/Controllers/Identities/Auth/LoginController.php index 43d7611..aecc55d 100644 --- a/tests/Fixtures/Http/Controllers/Identities/Auth/LoginController.php +++ b/tests/Fixtures/Http/Controllers/Identities/Auth/LoginController.php @@ -20,5 +20,4 @@ class LoginController extends Controller */ use AuthenticatesUsersWithIdentity; - } From 20f6c59d1e48c337b12a9144a055030f2162e38c Mon Sep 17 00:00:00 2001 From: Alessio Date: Sat, 22 Feb 2025 20:24:59 +0100 Subject: [PATCH 11/11] Update installation documentation --- README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5b1184c..6d1d890 100644 --- a/README.md +++ b/README.md @@ -91,20 +91,23 @@ configure the required options inside the `services` configuration file. 'client_secret' => env('GITLAB_CLIENT_SECRET'), 'redirect' => null, // set in the controller no need to specify 'instance_uri' => env('GITLAB_INSTANCE_URI', 'https://gitlab.com/') + // 'host' => env('GITLAB_INSTANCE_URI', 'https://gitlab.com/') // if using the default Socialite Gitlab driver ], ``` +> [!TIP] +> We do require also Socialite Providers Gitlab driver. So you need to include the `Identity::events()` in your app provider. If you want to use the Gitlab driver included in Laravel Socialite you can omitt `Identity::events()`. Remember to use `host` instead of `instance_uri` to configure the url of your Gitlab instance. + If you are using one of the community maintained [Socialite Providers](https://socialiteproviders.com/) -remember to register their events in your `EventsServiceProvider`. +remember to register their events in your `AppServiceProvider`. If you are not using those providers this step is optional. `oneofftech/laravel-connect-identity` provides out-of-the-box support for the `gitlab` and `dropbox` driver. If you are using those two you might add the following call to -your `EventsServiceProvider`. +your `AppServiceProvider`. ```php - public function boot() { parent::boot();