Skip to content

Commit f4fbe88

Browse files
Copilotkawax
andauthored
refactor: simplify tests using Socialite::fake() (#7)
* chore: plan simplification of tests using Socialite::fake() Agent-Logs-Url: https://github.com/invokable/socialite-wordpress/sessions/7135750a-9216-4f1c-8409-6a976a78cf49 Co-authored-by: kawax <1502086+kawax@users.noreply.github.com> * refactor: simplify tests using Socialite::fake() instead of complex Mockery/Guzzle mocks Agent-Logs-Url: https://github.com/invokable/socialite-wordpress/sessions/7135750a-9216-4f1c-8409-6a976a78cf49 Co-authored-by: kawax <1502086+kawax@users.noreply.github.com> * chore: remove duplicate auth routes from workbench Agent-Logs-Url: https://github.com/invokable/socialite-wordpress/sessions/7135750a-9216-4f1c-8409-6a976a78cf49 Co-authored-by: kawax <1502086+kawax@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: kawax <1502086+kawax@users.noreply.github.com>
1 parent cf978ec commit f4fbe88

5 files changed

Lines changed: 73 additions & 295 deletions

File tree

.phpunit.result.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":2,"defects":{"Tests\\SocialiteTest::test_redirect":7,"Tests\\SocialiteTest::test_callback_user":7},"times":{"Tests\\SocialiteTest::test_instance":0.037,"Tests\\SocialiteTest::test_redirect":0.019,"Tests\\WordPressProviderTest::test_redirect_generates_correct_url":0.003,"Tests\\WordPressProviderTest::test_get_auth_url_method":0.005,"Tests\\WordPressProviderTest::test_get_token_url_method":0.003,"Tests\\WordPressProviderTest::test_user_retrieval_with_mocked_http_client":0.008,"Tests\\WordPressProviderTest::test_user_retrieval_with_missing_optional_fields":0.004,"Tests\\WordPressProviderTest::test_user_retrieval_with_partial_data":0.003,"Tests\\WordPressProviderTest::test_user_profile_request_uses_bearer_token":0.003,"Tests\\WordPressProviderTest::test_map_user_to_object_with_all_fields":0.005,"Tests\\WordPressProviderTest::test_map_user_to_object_with_fallback_fields":0.003,"Tests\\WordPressProviderTest::test_get_user_by_token_method":0.003,"Tests\\WordPressProviderTest::test_map_user_to_object_with_minimal_data":0.003,"Tests\\SocialiteTest::test_callback_user":0.009}}

build/logs/clover.xml

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<coverage generated="1776163308">
3-
<project timestamp="1776163308" name="Clover Coverage">
2+
<coverage generated="1776164891">
3+
<project timestamp="1776164891" name="Clover Coverage">
44
<package name="Revolution\Socialite\WordPress">
5-
<file name="/Users/logic/Sites/socialite-wordpress/src/WordPressProvider.php">
5+
<file name="/home/runner/work/socialite-wordpress/socialite-wordpress/src/WordPressProvider.php">
66
<class name="Revolution\Socialite\WordPress\WordPressProvider" namespace="Revolution\Socialite\WordPress">
7-
<metrics complexity="4" methods="4" coveredmethods="4" conditionals="0" coveredconditionals="0" statements="17" coveredstatements="17" elements="21" coveredelements="21"/>
7+
<metrics complexity="4" methods="4" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="17" coveredstatements="7" elements="21" coveredelements="8"/>
88
</class>
9-
<line num="30" type="method" name="getAuthUrl" visibility="protected" complexity="1" crap="1" count="3"/>
10-
<line num="32" type="stmt" count="3"/>
11-
<line num="34" type="stmt" count="3"/>
12-
<line num="40" type="method" name="getTokenUrl" visibility="protected" complexity="1" crap="1" count="5"/>
13-
<line num="42" type="stmt" count="5"/>
14-
<line num="48" type="method" name="getUserByToken" visibility="protected" complexity="1" crap="1" count="5"/>
15-
<line num="50" type="stmt" count="5"/>
16-
<line num="51" type="stmt" count="5"/>
17-
<line num="52" type="stmt" count="5"/>
18-
<line num="53" type="stmt" count="5"/>
19-
<line num="54" type="stmt" count="5"/>
20-
<line num="55" type="stmt" count="5"/>
21-
<line num="57" type="stmt" count="5"/>
22-
<line num="63" type="method" name="mapUserToObject" visibility="protected" complexity="1" crap="1" count="7"/>
23-
<line num="65" type="stmt" count="7"/>
24-
<line num="66" type="stmt" count="7"/>
25-
<line num="67" type="stmt" count="7"/>
26-
<line num="68" type="stmt" count="7"/>
27-
<line num="69" type="stmt" count="7"/>
28-
<line num="70" type="stmt" count="7"/>
29-
<line num="71" type="stmt" count="7"/>
30-
<metrics loc="74" ncloc="52" classes="1" methods="4" coveredmethods="4" conditionals="0" coveredconditionals="0" statements="17" coveredstatements="17" elements="21" coveredelements="21"/>
9+
<line num="30" type="method" name="getAuthUrl" visibility="protected" complexity="1" crap="2" count="0"/>
10+
<line num="32" type="stmt" count="0"/>
11+
<line num="34" type="stmt" count="0"/>
12+
<line num="40" type="method" name="getTokenUrl" visibility="protected" complexity="1" crap="2" count="0"/>
13+
<line num="42" type="stmt" count="0"/>
14+
<line num="48" type="method" name="getUserByToken" visibility="protected" complexity="1" crap="2" count="0"/>
15+
<line num="50" type="stmt" count="0"/>
16+
<line num="51" type="stmt" count="0"/>
17+
<line num="52" type="stmt" count="0"/>
18+
<line num="53" type="stmt" count="0"/>
19+
<line num="54" type="stmt" count="0"/>
20+
<line num="55" type="stmt" count="0"/>
21+
<line num="57" type="stmt" count="0"/>
22+
<line num="63" type="method" name="mapUserToObject" visibility="protected" complexity="1" crap="1" count="3"/>
23+
<line num="65" type="stmt" count="3"/>
24+
<line num="66" type="stmt" count="3"/>
25+
<line num="67" type="stmt" count="3"/>
26+
<line num="68" type="stmt" count="3"/>
27+
<line num="69" type="stmt" count="3"/>
28+
<line num="70" type="stmt" count="3"/>
29+
<line num="71" type="stmt" count="3"/>
30+
<metrics loc="74" ncloc="52" classes="1" methods="4" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="17" coveredstatements="7" elements="21" coveredelements="8"/>
3131
</file>
32-
<file name="/Users/logic/Sites/socialite-wordpress/src/WordPressServiceProvider.php">
32+
<file name="/home/runner/work/socialite-wordpress/socialite-wordpress/src/WordPressServiceProvider.php">
3333
<class name="Revolution\Socialite\WordPress\WordPressServiceProvider" namespace="Revolution\Socialite\WordPress">
3434
<metrics complexity="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="4" coveredstatements="4" elements="5" coveredelements="5"/>
3535
</class>
36-
<line num="15" type="method" name="boot" visibility="public" complexity="1" crap="1" count="13"/>
37-
<line num="17" type="stmt" count="13"/>
36+
<line num="15" type="method" name="boot" visibility="public" complexity="1" crap="1" count="6"/>
37+
<line num="17" type="stmt" count="6"/>
3838
<line num="18" type="stmt" count="1"/>
3939
<line num="20" type="stmt" count="1"/>
40-
<line num="21" type="stmt" count="13"/>
40+
<line num="21" type="stmt" count="6"/>
4141
<metrics loc="24" ncloc="19" classes="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="4" coveredstatements="4" elements="5" coveredelements="5"/>
4242
</file>
4343
</package>
44-
<metrics files="2" loc="98" ncloc="71" classes="2" methods="5" coveredmethods="5" conditionals="0" coveredconditionals="0" statements="21" coveredstatements="21" elements="26" coveredelements="26"/>
44+
<metrics files="2" loc="98" ncloc="71" classes="2" methods="5" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="21" coveredstatements="11" elements="26" coveredelements="13"/>
4545
</project>
4646
</coverage>

tests/SocialiteTest.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,43 @@
22

33
namespace Tests;
44

5-
use Illuminate\Http\Request;
65
use Laravel\Socialite\Facades\Socialite;
7-
use Mockery as m;
6+
use Laravel\Socialite\Two\User;
87
use Revolution\Socialite\WordPress\WordPressProvider;
98

109
class SocialiteTest extends TestCase
1110
{
12-
protected function tearDown(): void
13-
{
14-
m::close();
15-
16-
parent::tearDown();
17-
}
18-
19-
public function test_instance()
11+
public function test_instance(): void
2012
{
2113
$provider = Socialite::driver('wordpress');
2214

2315
$this->assertInstanceOf(WordPressProvider::class, $provider);
2416
}
2517

26-
public function test_redirect()
18+
public function test_redirect(): void
2719
{
28-
$request = Request::create('foo');
29-
$request->setLaravelSession($session = m::mock('Illuminate\Contracts\Session\Session'));
30-
$session->shouldReceive('put')->once();
20+
Socialite::fake('wordpress');
3121

32-
$provider = new WordPressProvider($request, 'client_id', 'client_secret', 'redirect');
33-
$response = $provider->redirect();
22+
$response = $this->get('/auth/redirect');
3423

35-
$this->assertStringStartsWith('http://localhost', $response->getTargetUrl());
24+
$response->assertRedirect();
25+
}
26+
27+
public function test_callback_user(): void
28+
{
29+
Socialite::fake('wordpress', (new User)->map([
30+
'id' => 12345,
31+
'name' => 'johndoe',
32+
'nickname' => 'John Doe',
33+
'email' => 'john@example.com',
34+
'avatar' => 'https://example.com/avatar.jpg',
35+
]));
36+
37+
$response = $this->get('/auth/callback');
38+
39+
$response->assertOk();
40+
$response->assertJsonPath('id', 12345);
41+
$response->assertJsonPath('name', 'johndoe');
42+
$response->assertJsonPath('email', 'john@example.com');
3643
}
3744
}

tests/TestCase.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests;
44

55
use Illuminate\Foundation\Application;
6+
use Laravel\Socialite\Facades\Socialite;
67
use Laravel\Socialite\SocialiteServiceProvider;
78
use Revolution\Socialite\WordPress\WordPressServiceProvider;
89

@@ -48,4 +49,17 @@ protected function getEnvironmentSetUp($app): void
4849
'prefix' => '',
4950
]);
5051
}
52+
53+
protected function defineWebRoutes($router): void
54+
{
55+
$router->get('/auth/redirect', function () {
56+
return Socialite::driver('wordpress')->redirect();
57+
});
58+
59+
$router->get('/auth/callback', function () {
60+
$user = Socialite::driver('wordpress')->user();
61+
62+
return response()->json($user);
63+
});
64+
}
5165
}

0 commit comments

Comments
 (0)