WechatProviderTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\WeChatOpenPlatformProvider as RealWeChatOpenPlatformProvider;
  11. use Overtrue\Socialite\Providers\WeChatProvider as RealWeChatProvider;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class WechatProviderTest extends PHPUnit_Framework_TestCase
  14. {
  15. public function testWeChatProviderHasCorrectlyRedirectResponse()
  16. {
  17. $response = (new WeChatProvider(Request::create('foo'), 'client_id', 'client_secret', 'http://localhost/socialite/callback.php'))->redirect();
  18. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  19. $this->assertStringStartsWith('https://open.weixin.qq.com/connect/qrconnect', $response->getTargetUrl());
  20. $this->assertRegExp('/redirect_uri=http%3A%2F%2Flocalhost%2Fsocialite%2Fcallback.php/', $response->getTargetUrl());
  21. }
  22. public function testWeChatOpenPlatformProviderHasCorrectlyRedirectResponse()
  23. {
  24. $response = (new WeChatOpenPlatformProvider(Request::create('foo'), 'client_id', ['component-app-id', 'component-access-token'], 'http://localhost/callback.php'))->redirect();
  25. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  26. $this->assertStringStartsWith('https://open.weixin.qq.com/connect/oauth2/authorize', $response->getTargetUrl());
  27. $this->assertRegExp('/redirect_uri=http%3A%2F%2Flocalhost%2Fcallback.php/', $response->getTargetUrl());
  28. }
  29. public function testWeChatProviderTokenUrlAndRequestFields()
  30. {
  31. $provider = new WeChatProvider(Request::create('foo'), 'client_id', 'client_secret', 'http://localhost/socialite/callback.php');
  32. $this->assertSame('https://api.weixin.qq.com/sns/oauth2/access_token', $provider->tokenUrl());
  33. $this->assertSame([
  34. 'appid' => 'client_id',
  35. 'secret' => 'client_secret',
  36. 'code' => 'iloveyou',
  37. 'grant_type' => 'authorization_code',
  38. ], $provider->tokenFields('iloveyou'));
  39. $this->assertSame([
  40. 'appid' => 'client_id',
  41. 'redirect_uri' => 'http://localhost/socialite/callback.php',
  42. 'response_type' => 'code',
  43. 'scope' => 'snsapi_login',
  44. 'state' => 'wechat-state',
  45. ], $provider->codeFields('wechat-state'));
  46. }
  47. public function testWeChatOpenPlatformProviderTokenUrlAndRequestFields()
  48. {
  49. $provider = new WeChatOpenPlatformProvider(Request::create('foo'), 'client_id', ['component-app-id', 'component-access-token'], 'redirect-url');
  50. $this->assertSame('https://api.weixin.qq.com/sns/oauth2/component/access_token', $provider->tokenUrl());
  51. $this->assertSame([
  52. 'appid' => 'client_id',
  53. 'component_appid' => 'component-app-id',
  54. 'component_access_token' => 'component-access-token',
  55. 'code' => 'code',
  56. 'grant_type' => 'authorization_code',
  57. ], $provider->tokenFields('code'));
  58. $this->assertSame([
  59. 'appid' => 'client_id',
  60. 'redirect_uri' => 'redirect-url',
  61. 'response_type' => 'code',
  62. 'scope' => 'snsapi_base',
  63. 'state' => 'state',
  64. 'component_appid' => 'component-app-id',
  65. ], $provider->codeFields('state'));
  66. }
  67. public function testOpenPlatformComponent()
  68. {
  69. $provider = new WeChatProvider(Request::create('foo'), 'client_id', null, 'redirect-url');
  70. $provider->component(new WeChatComponent());
  71. $this->assertSame([
  72. 'appid' => 'client_id',
  73. 'redirect_uri' => 'redirect-url',
  74. 'response_type' => 'code',
  75. 'scope' => 'snsapi_base',
  76. 'state' => 'state',
  77. 'component_appid' => 'component-app-id',
  78. ], $provider->codeFields('state'));
  79. $this->assertSame([
  80. 'appid' => 'client_id',
  81. 'component_appid' => 'component-app-id',
  82. 'component_access_token' => 'token',
  83. 'code' => 'simcode',
  84. 'grant_type' => 'authorization_code',
  85. ], $provider->tokenFields('simcode'));
  86. $this->assertSame('https://api.weixin.qq.com/sns/oauth2/component/access_token', $provider->tokenUrl());
  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 WeChatOpenPlatformProvider extends RealWeChatOpenPlatformProvider
  109. {
  110. use ProviderTrait;
  111. }
  112. class WeChatComponent implements \Overtrue\Socialite\WeChatComponentInterface
  113. {
  114. public function getAppId()
  115. {
  116. return 'component-app-id';
  117. }
  118. public function getToken()
  119. {
  120. return 'token';
  121. }
  122. }