first commit

This commit is contained in:
Your Name
2026-01-19 14:19:22 +08:00
commit fe2d9c1868
4777 changed files with 665503 additions and 0 deletions

View File

@@ -0,0 +1,213 @@
<?php
use Mockery as m;
use Overtrue\Socialite\Providers\Base;
use Overtrue\Socialite\User;
use PHPUnit\Framework\TestCase;
class OAuthTest extends TestCase
{
public function tearDown(): void
{
m::close();
}
public function test_it_can_get_auth_url_without_redirect()
{
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$this->assertSame('http://auth.url?client_id=fake_client_id&scope=info&response_type=code', $provider->redirect());
}
public function test_it_can_get_auth_url_with_redirect()
{
// 手动配置
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$this->assertSame('http://auth.url?client_id=fake_client_id&redirect_uri=fake_redirect&scope=info&response_type=code', $provider->redirect('fake_redirect'));
// 用配置属性配置
$config += ['redirect_url' => 'fake_redirect'];
$provider = new OAuthTestProviderStub($config);
$this->assertSame('http://auth.url?client_id=fake_client_id&redirect_uri=fake_redirect&scope=info&response_type=code', $provider->redirect('fake_redirect'));
}
public function test_it_can_get_auth_url_with_scopes()
{
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$url = $provider->scopes(['test_info', 'test_email'])->redirect();
$this->assertSame('http://auth.url?client_id=fake_client_id&scope=test_info%2Ctest_email&response_type=code', $url);
// 切换scope分割符
$url = $provider->scopes(['test_info', 'test_email'])->withScopeSeparator(' ')->redirect();
$this->assertSame('http://auth.url?client_id=fake_client_id&scope=test_info%20test_email&response_type=code', $url);
}
public function test_it_can_get_auth_url_with_state()
{
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$url = $provider->withState(123456)->redirect();
$this->assertSame('http://auth.url?client_id=fake_client_id&scope=info&response_type=code&state=123456', $url);
}
public function test_it_can_get_token()
{
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$response = m::mock(\Psr\Http\Message\ResponseInterface::class);
$response->shouldReceive('getBody')->andReturn($response);
$response->shouldReceive('__toString')->andReturn(\json_encode([
'access_token' => 'fake_access_token',
'refresh_token' => 'fake_refresh_token',
'expires_in' => 123456,
]));
$provider->getHttpClient()->shouldReceive('post')->with('http://token.url', [
'form_params' => [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
'code' => 'fake_code',
'redirect_uri' => null,
],
'headers' => [
'Accept' => 'application/json',
],
])->andReturn($response);
$this->assertSame([
'access_token' => 'fake_access_token',
'refresh_token' => 'fake_refresh_token',
'expires_in' => 123456,
], $provider->tokenFromCode('fake_code'));
}
public function test_it_can_get_user_by_token()
{
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$user = $provider->userFromToken('fake_access_token');
$this->assertSame('foo', $user->getId());
$this->assertSame(['id' => 'foo'], $user->getRaw());
$this->assertSame('fake_access_token', $user->getAccessToken());
}
public function test_it_can_get_user_by_code()
{
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$response = m::mock(\Psr\Http\Message\ResponseInterface::class);
$response->shouldReceive('getBody')->andReturn($response);
$response->shouldReceive('__toString')->andReturn(\json_encode([
'access_token' => 'fake_access_token',
'refresh_token' => 'fake_refresh_token',
'expires_in' => 123456,
]));
$provider->getHttpClient()->shouldReceive('post')->with('http://token.url', [
'form_params' => [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
'code' => 'fake_code',
'redirect_uri' => null,
],
'headers' => [
'Accept' => 'application/json',
],
])->andReturn($response);
$this->assertSame([
'access_token' => 'fake_access_token',
'refresh_token' => 'fake_refresh_token',
'expires_in' => 123456,
], $provider->tokenFromCode('fake_code'));
$user = $provider->userFromCode('fake_code');
$tokenResponse = [
'access_token' => 'fake_access_token',
'refresh_token' => 'fake_refresh_token',
'expires_in' => 123456,
];
$this->assertSame('foo', $user->getId());
$this->assertSame($tokenResponse, $user->getTokenResponse());
$this->assertSame('fake_access_token', $user->getAccessToken());
$this->assertSame('fake_refresh_token', $user->getRefreshToken());
}
}
class OAuthTestProviderStub extends Base
{
public $http;
protected array $scopes = ['info'];
protected int $encodingType = PHP_QUERY_RFC3986;
protected function getAuthUrl(): string
{
$url = 'http://auth.url';
return $this->buildAuthUrlFromBase($url);
}
protected function getTokenUrl(): string
{
return 'http://token.url';
}
protected function getUserByToken(string $token): array
{
return ['id' => 'foo'];
}
protected function mapUserToObject(array $user): User
{
return new User(['id' => $user['id']]);
}
/**
* Get a fresh instance of the Guzzle HTTP client.
*
* @return \GuzzleHttp\Client
*/
public function getHttpClient(): GuzzleHttp\Client
{
if ($this->http) {
return $this->http;
}
return $this->http = m::mock(\GuzzleHttp\Client::class);
}
}

View File

@@ -0,0 +1,246 @@
<?php
namespace Providers;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Overtrue\Socialite\Exceptions\FeiShu\InvalidTicketException;
use Overtrue\Socialite\Exceptions\InvalidTokenException;
use Overtrue\Socialite\Providers\FeiShu;
use PHPUnit\Framework\TestCase;
class FeiShuTest extends TestCase
{
public function testProviderCanCreateCorrect()
{
// one way
$config = [
'app_id' => 'xxxxx',
'app_secret' => 'yyyyy',
'app_mode' => 'internal',
];
$f = new FeiShu($config);
$rf = new \ReflectionObject($f);
$this->assertEquals('xxxxx', $f->getClientId());
$this->assertEquals('yyyyy', $f->getClientSecret());
$rfProperty = $rf->getProperty('isInternalApp');
$rfProperty->setAccessible(true);
$this->assertEquals(true, $rfProperty->getValue($f));
// diff filed way
$config = [
'client_id' => 'xxxxx',
'client_secret' => 'yyyyy',
'mode' => 'internal',
];
$f = new FeiShu($config);
$rf = new \ReflectionObject($f);
$this->assertEquals('xxxxx', $f->getClientId());
$this->assertEquals('yyyyy', $f->getClientSecret());
$rfProperty = $rf->getProperty('isInternalApp');
$rfProperty->setAccessible(true);
$this->assertEquals(true, $rfProperty->getValue($f));
// no mode config way
$config = [
'client_id' => 'xxxxx',
'client_secret' => 'yyyyy',
];
$f = new FeiShu($config);
$rf = new \ReflectionObject($f);
$this->assertEquals('xxxxx', $f->getClientId());
$this->assertEquals('yyyyy', $f->getClientSecret());
$rfProperty = $rf->getProperty('isInternalApp');
$rfProperty->setAccessible(true);
$this->assertEquals(false, $rfProperty->getValue($f));
}
public function testProviderWithInternalAppModeWork()
{
$config = [
'client_id' => 'xxxxx',
'client_secret' => 'yyyyy',
];
$f = new FeiShu($config);
$rf = new \ReflectionObject($f);
$rfProperty = $rf->getProperty('isInternalApp');
$rfProperty->setAccessible(true);
$f->withInternalAppMode();
$this->assertEquals(true, $rfProperty->getValue($f));
$f->withDefaultMode();
$this->assertEquals(false, $rfProperty->getValue($f));
}
public function testProviderWithAppTicketWork()
{
$config = [
'client_id' => 'xxxxx',
'client_secret' => 'yyyyy',
];
$f = new FeiShu($config);
$f->withAppTicket('app_ticket');
$this->assertEquals('app_ticket', $f->getConfig()->get('app_ticket'));
}
public function testConfigAppAccessTokenWithDefaultModeNoAppTicketWork()
{
$config = [
'client_id' => 'xxxxx',
'client_secret' => 'yyyyy',
];
$f = new FeiShu($config);
$fr = new \ReflectionObject($f);
$frClient = $fr->getProperty('httpClient');
$frClient->setAccessible(true);
$ff = new \ReflectionMethod(FeiShu::class, 'configAppAccessToken');
$mock = new MockHandler([
new Response(403, []),
new Response(200, [], \json_encode([
'app_access_token' => 'app_access_token',
])),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$frClient->setValue($f, $client);
$ff->setAccessible(true);
// 默认模式下没有 app_ticket
$this->expectException(InvalidTicketException::class);
$ff->invoke($f);
$ff->invoke($f);
$f->withAppTicket('app_ticket');
$this->assertEquals('app_access_token', $f->getConfig()->get('app_access_token'));
$this->expectException(InvalidTokenException::class);
$ff->invoke($f);
}
public function testConfigAppAccessTokenWithDefaultModeAndAppTicketWorkInBadResponse()
{
$config = [
'client_id' => 'xxxxx',
'client_secret' => 'yyyyy',
];
$f = new FeiShu($config);
$fr = new \ReflectionObject($f);
$frClient = $fr->getProperty('httpClient');
$frClient->setAccessible(true);
$ff = new \ReflectionMethod(FeiShu::class, 'configAppAccessToken');
$mock = new MockHandler([
new Response(200, [], '{}'),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$frClient->setValue($f, $client);
$ff->setAccessible(true);
$this->expectException(InvalidTokenException::class);
$ff->invoke($f->withAppTicket('app_ticket'));
}
public function testConfigAppAccessTokenWithDefaultModeAndAppTicketWorkInGoodResponse()
{
$config = [
'client_id' => 'xxxxx',
'client_secret' => 'yyyyy',
];
$f = new FeiShu($config);
$fr = new \ReflectionObject($f);
$frClient = $fr->getProperty('httpClient');
$frClient->setAccessible(true);
$ff = new \ReflectionMethod(FeiShu::class, 'configAppAccessToken');
$mock = new MockHandler([
new Response(200, [], \json_encode([
'app_access_token' => 'app_access_token',
])),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$frClient->setValue($f, $client);
$ff->setAccessible(true);
$this->assertEquals(null, $f->getConfig()->get('app_access_token'));
$ff->invoke($f->withAppTicket('app_ticket'));
$this->assertEquals('app_access_token', $f->getConfig()->get('app_access_token'));
}
public function testConfigAppAccessTokenWithInternalInBadResponse()
{
$config = [
'client_id' => 'xxxxx',
'client_secret' => 'yyyyy',
'mode' => 'internal',
];
$f = new FeiShu($config);
$fr = new \ReflectionObject($f);
$frClient = $fr->getProperty('httpClient');
$frClient->setAccessible(true);
$ff = new \ReflectionMethod(FeiShu::class, 'configAppAccessToken');
$mock = new MockHandler([
new Response(200, [], '{}'),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$frClient->setValue($f, $client);
$ff->setAccessible(true);
$this->expectException(InvalidTokenException::class);
$ff->invoke($f);
}
public function testConfigAppAccessTokenWithInternalInGoodResponse()
{
$config = [
'client_id' => 'xxxxx',
'client_secret' => 'yyyyy',
'mode' => 'internal',
];
$f = new FeiShu($config);
$fr = new \ReflectionObject($f);
$frClient = $fr->getProperty('httpClient');
$frClient->setAccessible(true);
$ff = new \ReflectionMethod(FeiShu::class, 'configAppAccessToken');
$mock = new MockHandler([
new Response(200, [], \json_encode([
'app_access_token' => 'app_access_token',
])),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$frClient->setValue($f, $client);
$ff->setAccessible(true);
$this->assertEquals(null, $f->getConfig()->get('app_access_token'));
$ff->invoke($f);
$this->assertEquals('app_access_token', $f->getConfig()->get('app_access_token'));
}
}

View File

@@ -0,0 +1,20 @@
<?php
use Overtrue\Socialite\Providers\WeWork;
use PHPUnit\Framework\TestCase;
class WeWorkTest extends TestCase
{
public function testOAuthUrl()
{
$response = (new WeWork([
'client_id' => 'CORPID',
'client_secret' => 'client_secret',
'redirect' => 'REDIRECT_URI',
]))
->scopes(['snsapi_base'])
->redirect();
$this->assertSame('https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base#wechat_redirect', $response);
}
}

View File

@@ -0,0 +1,120 @@
<?php
use Overtrue\Socialite\Providers\WeChat;
use PHPUnit\Framework\TestCase;
// here we need loaded the symbols first.
\class_exists(\Overtrue\Socialite\Contracts\FactoryInterface::class);
class WechatTest extends TestCase
{
public function testWeChatProviderHasCorrectlyRedirectResponse()
{
$response = (new WeChat([
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect_url' => 'http://localhost/socialite/callback.php',
]))->redirect();
$this->assertStringStartsWith('https://open.weixin.qq.com/connect/qrconnect', $response);
$this->assertMatchesRegularExpression('/redirect_uri=http%3A%2F%2Flocalhost%2Fsocialite%2Fcallback.php/', $response);
}
public function testWeChatProviderTokenUrlAndRequestFields()
{
$provider = new WeChat([
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect_url' => 'http://localhost/socialite/callback.php',
]);
$getTokenUrl = new ReflectionMethod(WeChat::class, 'getTokenUrl');
$getTokenUrl->setAccessible(true);
$getTokenFields = new ReflectionMethod(WeChat::class, 'getTokenFields');
$getTokenFields->setAccessible(true);
$getCodeFields = new ReflectionMethod(WeChat::class, 'getCodeFields');
$getCodeFields->setAccessible(true);
$this->assertSame('https://api.weixin.qq.com/sns/oauth2/access_token', $getTokenUrl->invoke($provider));
$this->assertSame([
'appid' => 'client_id',
'secret' => 'client_secret',
'code' => 'iloveyou',
'grant_type' => 'authorization_code',
], $getTokenFields->invoke($provider, 'iloveyou'));
$this->assertSame([
'appid' => 'client_id',
'redirect_uri' => 'http://localhost/socialite/callback.php',
'response_type' => 'code',
'scope' => 'snsapi_login',
'state' => 'wechat-state',
'connect_redirect' => 1,
], $getCodeFields->invoke($provider->withState('wechat-state')));
}
public function testOpenPlatformComponent()
{
$provider = new WeChat([
'client_id' => 'client_id',
'client_secret' => null,
'redirect' => 'redirect-url',
'component' => [
'id' => 'component-app-id',
'token' => 'token',
],
]);
$getTokenUrl = new ReflectionMethod(WeChat::class, 'getTokenUrl');
$getTokenUrl->setAccessible(true);
$getTokenFields = new ReflectionMethod(WeChat::class, 'getTokenFields');
$getTokenFields->setAccessible(true);
$getCodeFields = new ReflectionMethod(WeChat::class, 'getCodeFields');
$getCodeFields->setAccessible(true);
$this->assertSame([
'appid' => 'client_id',
'redirect_uri' => 'redirect-url',
'response_type' => 'code',
'scope' => 'snsapi_base',
'state' => 'state',
'connect_redirect' => 1,
'component_appid' => 'component-app-id',
], $getCodeFields->invoke($provider->withState('state')));
$this->assertSame([
'appid' => 'client_id',
'component_appid' => 'component-app-id',
'component_access_token' => 'token',
'code' => 'simcode',
'grant_type' => 'authorization_code',
], $getTokenFields->invoke($provider, 'simcode'));
$this->assertSame('https://api.weixin.qq.com/sns/oauth2/component/access_token', $getTokenUrl->invoke($provider));
}
public function testOpenPlatformComponentWithCustomParameters()
{
$provider = new WeChat([
'client_id' => 'client_id',
'client_secret' => null,
'redirect' => 'redirect-url',
'component' => [
'id' => 'component-app-id',
'token' => 'token',
],
]);
$getCodeFields = new ReflectionMethod(WeChat::class, 'getCodeFields');
$getCodeFields->setAccessible(true);
$provider->with(['foo' => 'bar']);
$fields = $getCodeFields->invoke($provider->withState('wechat-state'));
$this->assertArrayHasKey('foo', $fields);
$this->assertSame('bar', $fields['foo']);
}
}

View File

@@ -0,0 +1,109 @@
<?php
use Overtrue\Socialite\Providers\Base;
use Overtrue\Socialite\Providers\GitHub;
use Overtrue\Socialite\SocialiteManager;
use Overtrue\Socialite\User;
use PHPUnit\Framework\TestCase;
class SocialiteManagerTest extends TestCase
{
public function test_it_can_create_from_config()
{
$config = [
'foo' => [
'provider' => 'github',
'client_id' => 'foo-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
'bar' => [
'provider' => 'github',
'client_id' => 'bar-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
$manager = new SocialiteManager($config);
$this->assertInstanceOf(GitHub::class, $manager->create('foo'));
$this->assertSame('foo-app-id', $manager->create('foo')->getClientId());
$this->assertInstanceOf(GitHub::class, $manager->create('bar'));
$this->assertSame('bar-app-id', $manager->create('bar')->getClientId());
// from name
$config = [
'github' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
$manager = new SocialiteManager($config);
$this->assertInstanceOf(GitHub::class, $manager->create('github'));
$this->assertSame('your-app-id', $manager->create('github')->getClientId());
}
public function test_it_can_create_from_custom_creator()
{
$config = [
'foo' => [
'provider' => 'myprovider',
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
$manager = new SocialiteManager($config);
$manager->extend('myprovider', function ($config) {
return new DummyProviderForCustomProviderTest($config);
});
$this->assertInstanceOf(DummyProviderForCustomProviderTest::class, $manager->create('foo'));
}
public function test_it_can_create_from_custom_provider_class()
{
$config = [
'foo' => [
'provider' => DummyProviderForCustomProviderTest::class,
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
$manager = new SocialiteManager($config);
$this->assertInstanceOf(DummyProviderForCustomProviderTest::class, $manager->create('foo'));
}
}
class DummyProviderForCustomProviderTest extends Base
{
protected function getAuthUrl(): string
{
return '';
}
protected function getTokenUrl(): string
{
return '';
}
protected function getUserByToken(string $token): array
{
return [];
}
protected function mapUserToObject(array $user): User
{
return new User([]);
}
}

View File

@@ -0,0 +1,51 @@
<?php
use Overtrue\Socialite\User;
use PHPUnit\Framework\TestCase;
class UserTest extends TestCase
{
public function testJsonserialize()
{
$this->assertSame('[]', json_encode(new User([])));
$this->assertSame('{"access_token":"mock-token"}', json_encode(new User(['access_token' => 'mock-token'])));
}
public function test_it_can_get_refresh_token()
{
$user = new User([
'name' => 'fake_name',
'access_token' => 'mock-token',
'refresh_token' => 'fake_refresh',
]);
// 能通过用 User 对象获取 refresh token
$this->assertSame('fake_refresh', $user->getRefreshToken());
$this->assertSame('{"name":"fake_name","access_token":"mock-token","refresh_token":"fake_refresh"}', json_encode($user));
// 无 refresh_token 属性返回 null
$user = new User([]);
$this->assertSame(null, $user->getRefreshToken());
// 能通过 setRefreshToken() 设置
$user->setRefreshToken('fake_refreshToken');
$this->assertSame('fake_refreshToken', $user->getRefreshToken());
$this->assertSame('{"refresh_token":"fake_refreshToken"}', json_encode($user));
}
public function test_it_can_set_raw_data()
{
$user = new User([]);
$data = ['data' => 'raw'];
$user->setRaw($data);
$this->assertSame($data, $user->getRaw());
$this->assertSame(json_encode(['raw' => ['data' => 'raw']]), json_encode($user));
}
public function test_ie_can_get_attribute_by_magic_method()
{
$user = new User(['xx' => 'data']);
$this->assertSame('data', $user->xx);
}
}