From 8e164bda36e4c4d2a4263c72940c14f8cbe23620 Mon Sep 17 00:00:00 2001 From: Falconiere Barbosa Date: Wed, 10 Jun 2026 14:56:44 -0300 Subject: [PATCH] refactor(linkedin): dedupe connect-scope test setup into shared closure --- .../Feature/Social/LinkedInControllerTest.php | 46 +++++++------------ 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/tests/Feature/Social/LinkedInControllerTest.php b/tests/Feature/Social/LinkedInControllerTest.php index 768a9f01..ef05246d 100644 --- a/tests/Feature/Social/LinkedInControllerTest.php +++ b/tests/Feature/Social/LinkedInControllerTest.php @@ -39,9 +39,13 @@ expect(session('social_connect_workspace'))->toBe($this->workspace->id); }); -test('linkedin connect requests the default scope set when LINKEDIN_EXTRA_SCOPES is unset', function () { - config(['trypost.platforms.linkedin.extra_scopes' => null]); - +/** + * Mock the LinkedIn Socialite driver, hit /connect, and return the scopes + * the controller requested. + * + * @return array + */ +$captureConnectScopes = function (object $test): array { $captured = []; $driverMock = Mockery::mock(); @@ -60,43 +64,27 @@ ->with('linkedin') ->andReturn($driverMock); - $this->actingAs($this->user) + $test->actingAs($test->user) ->withHeader('X-Inertia', 'true') ->get(route('app.social.linkedin.connect')); - expect($captured)->toEqualCanonicalizing([ + return $captured; +}; + +test('linkedin connect requests the default scope set when LINKEDIN_EXTRA_SCOPES is unset', function () use ($captureConnectScopes) { + config(['trypost.platforms.linkedin.extra_scopes' => null]); + + expect($captureConnectScopes($this))->toEqualCanonicalizing([ 'openid', 'profile', 'email', 'w_member_social', ]); }); -test('linkedin connect appends LINKEDIN_EXTRA_SCOPES to the default scope set', function () { +test('linkedin connect appends LINKEDIN_EXTRA_SCOPES to the default scope set', function () use ($captureConnectScopes) { // Backward-compatibility: ops who have legacy products approved on // their LinkedIn app (e.g. r_basicprofile) opt back in via env. config(['trypost.platforms.linkedin.extra_scopes' => 'r_basicprofile, r_emailaddress']); - $captured = []; - - $driverMock = Mockery::mock(); - $driverMock->shouldReceive('scopes') - ->withArgs(function (array $scopes) use (&$captured) { - $captured = $scopes; - - return true; - }) - ->andReturnSelf(); - $driverMock->shouldReceive('redirect')->andReturn(Mockery::mock([ - 'getTargetUrl' => 'https://www.linkedin.com/oauth/v2/authorization?test=1', - ])); - - Socialite::shouldReceive('driver') - ->with('linkedin') - ->andReturn($driverMock); - - $this->actingAs($this->user) - ->withHeader('X-Inertia', 'true') - ->get(route('app.social.linkedin.connect')); - - expect($captured)->toEqualCanonicalizing([ + expect($captureConnectScopes($this))->toEqualCanonicalizing([ 'openid', 'profile', 'email', 'w_member_social', 'r_basicprofile', 'r_emailaddress', ]);