WechatProviderTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * This file is part of the overtrue/socialite.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. use Overtrue\Socialite\Providers\WeChatProvider as RealWeChatProvider;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class WechatProviderTest extends TestCase
  14. {
  15. public function testWeChatProviderHasCorrectlyRedirectResponse()
  16. {
  17. $response = (new WeChatProvider(Request::create('foo'), [
  18. 'client_id' => 'client_id',
  19. 'client_secret' => 'client_secret',
  20. 'redirect' => 'http://localhost/socialite/callback.php',
  21. ]))->redirect();
  22. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  23. $this->assertStringStartsWith('https://open.weixin.qq.com/connect/qrconnect', $response->getTargetUrl());
  24. $this->assertRegExp('/redirect_uri=http%3A%2F%2Flocalhost%2Fsocialite%2Fcallback.php/', $response->getTargetUrl());
  25. }
  26. public function testWeChatProviderTokenUrlAndRequestFields()
  27. {
  28. $provider = new WeChatProvider(Request::create('foo'), [
  29. 'client_id' => 'client_id',
  30. 'client_secret' => 'client_secret',
  31. 'redirect' => 'http://localhost/socialite/callback.php',
  32. ]);
  33. $this->assertSame('https://api.weixin.qq.com/sns/oauth2/access_token', $provider->tokenUrl());
  34. $this->assertSame([
  35. 'appid' => 'client_id',
  36. 'secret' => 'client_secret',
  37. 'code' => 'iloveyou',
  38. 'grant_type' => 'authorization_code',
  39. ], $provider->tokenFields('iloveyou'));
  40. $this->assertSame([
  41. 'appid' => 'client_id',
  42. 'redirect_uri' => 'http://localhost/socialite/callback.php',
  43. 'response_type' => 'code',
  44. 'scope' => 'snsapi_login',
  45. 'state' => 'wechat-state',
  46. 'connect_redirect' => 1,
  47. ], $provider->codeFields('wechat-state'));
  48. }
  49. public function testOpenPlatformComponent()
  50. {
  51. $provider = new WeChatProvider(Request::create('foo'), [
  52. 'client_id' => 'client_id',
  53. 'client_secret' => null,
  54. 'redirect' => 'redirect-url',
  55. ]);
  56. $provider->component(new WeChatComponent());
  57. $this->assertSame([
  58. 'appid' => 'client_id',
  59. 'redirect_uri' => 'redirect-url',
  60. 'response_type' => 'code',
  61. 'scope' => 'snsapi_base',
  62. 'state' => 'state',
  63. 'connect_redirect' => 1,
  64. 'component_appid' => 'component-app-id',
  65. ], $provider->codeFields('state'));
  66. $this->assertSame([
  67. 'appid' => 'client_id',
  68. 'component_appid' => 'component-app-id',
  69. 'component_access_token' => 'token',
  70. 'code' => 'simcode',
  71. 'grant_type' => 'authorization_code',
  72. ], $provider->tokenFields('simcode'));
  73. $this->assertSame('https://api.weixin.qq.com/sns/oauth2/component/access_token', $provider->tokenUrl());
  74. }
  75. public function testOpenPlatformComponentWithCustomParameters()
  76. {
  77. $provider = new WeChatProvider(Request::create('foo'), [
  78. 'client_id' => 'client_id',
  79. 'client_secret' => null,
  80. 'redirect' => 'redirect-url',
  81. ]);
  82. $provider->component(new WeChatComponent());
  83. $provider->with(['foo' => 'bar']);
  84. $fields = $provider->codeFields('wechat-state');
  85. $this->assertArrayHasKey('foo', $fields);
  86. $this->assertSame('bar', $fields['foo']);
  87. }
  88. }
  89. trait ProviderTrait
  90. {
  91. public function tokenUrl()
  92. {
  93. return $this->getTokenUrl();
  94. }
  95. public function tokenFields($code)
  96. {
  97. return $this->getTokenFields($code);
  98. }
  99. public function codeFields($state = null)
  100. {
  101. return $this->getCodeFields($state);
  102. }
  103. }
  104. class WeChatProvider extends RealWeChatProvider
  105. {
  106. use ProviderTrait;
  107. }
  108. class WeChatComponent implements \Overtrue\Socialite\WeChatComponentInterface
  109. {
  110. public function getAppId()
  111. {
  112. return 'component-app-id';
  113. }
  114. public function getToken()
  115. {
  116. return 'token';
  117. }
  118. }