SocialiteManagerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. use Overtrue\Socialite\Providers\Base;
  3. use Overtrue\Socialite\Providers\GitHub;
  4. use Overtrue\Socialite\SocialiteManager;
  5. use Overtrue\Socialite\User;
  6. use PHPUnit\Framework\TestCase;
  7. class SocialiteManagerTest extends TestCase
  8. {
  9. public function test_it_can_create_from_config()
  10. {
  11. $config = [
  12. 'foo' => [
  13. 'provider' => 'github',
  14. 'client_id' => 'foo-app-id',
  15. 'client_secret' => 'your-app-secret',
  16. 'redirect' => 'http://localhost/socialite/callback.php',
  17. ],
  18. 'bar' => [
  19. 'provider' => 'github',
  20. 'client_id' => 'bar-app-id',
  21. 'client_secret' => 'your-app-secret',
  22. 'redirect' => 'http://localhost/socialite/callback.php',
  23. ],
  24. ];
  25. $manager = new SocialiteManager($config);
  26. $this->assertInstanceOf(GitHub::class, $manager->create('foo'));
  27. $this->assertSame('foo-app-id', $manager->create('foo')->getClientId());
  28. $this->assertInstanceOf(GitHub::class, $manager->create('bar'));
  29. $this->assertSame('bar-app-id', $manager->create('bar')->getClientId());
  30. // from name
  31. $config = [
  32. 'github' => [
  33. 'client_id' => 'your-app-id',
  34. 'client_secret' => 'your-app-secret',
  35. 'redirect' => 'http://localhost/socialite/callback.php',
  36. ],
  37. ];
  38. $manager = new SocialiteManager($config);
  39. $this->assertInstanceOf(GitHub::class, $manager->create('github'));
  40. $this->assertSame('your-app-id', $manager->create('github')->getClientId());
  41. }
  42. public function test_it_can_create_from_custom_creator()
  43. {
  44. $config = [
  45. 'foo' => [
  46. 'provider' => 'myprovider',
  47. 'client_id' => 'your-app-id',
  48. 'client_secret' => 'your-app-secret',
  49. 'redirect' => 'http://localhost/socialite/callback.php',
  50. ],
  51. ];
  52. $manager = new SocialiteManager($config);
  53. $manager->extend('myprovider', function ($config) {
  54. return new DummyProviderForCustomProviderTest($config);
  55. });
  56. $this->assertInstanceOf(DummyProviderForCustomProviderTest::class, $manager->create('foo'));
  57. }
  58. public function test_it_can_create_from_custom_provider_class()
  59. {
  60. $config = [
  61. 'foo' => [
  62. 'provider' => DummyProviderForCustomProviderTest::class,
  63. 'client_id' => 'your-app-id',
  64. 'client_secret' => 'your-app-secret',
  65. 'redirect' => 'http://localhost/socialite/callback.php',
  66. ],
  67. ];
  68. $manager = new SocialiteManager($config);
  69. $this->assertInstanceOf(DummyProviderForCustomProviderTest::class, $manager->create('foo'));
  70. }
  71. }
  72. class DummyProviderForCustomProviderTest extends Base
  73. {
  74. protected function getAuthUrl(): string
  75. {
  76. return '';
  77. }
  78. protected function getTokenUrl(): string
  79. {
  80. return '';
  81. }
  82. protected function getUserByToken(string $token): array
  83. {
  84. return [];
  85. }
  86. protected function mapUserToObject(array $user): User
  87. {
  88. return new User([]);
  89. }
  90. }