WechatProviderTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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'), '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 testWeChatProviderTokenUrlAndRequestFields()
  23. {
  24. $provider = new WeChatProvider(Request::create('foo'), 'client_id', 'client_secret', 'http://localhost/socialite/callback.php');
  25. $this->assertSame('https://api.weixin.qq.com/sns/oauth2/access_token', $provider->tokenUrl());
  26. $this->assertSame([
  27. 'appid' => 'client_id',
  28. 'secret' => 'client_secret',
  29. 'code' => 'iloveyou',
  30. 'grant_type' => 'authorization_code',
  31. ], $provider->tokenFields('iloveyou'));
  32. $this->assertSame([
  33. 'appid' => 'client_id',
  34. 'redirect_uri' => 'http://localhost/socialite/callback.php',
  35. 'response_type' => 'code',
  36. 'scope' => 'snsapi_login',
  37. 'state' => 'wechat-state',
  38. 'connect_redirect' => 1,
  39. ], $provider->codeFields('wechat-state'));
  40. }
  41. public function testOpenPlatformComponent()
  42. {
  43. $provider = new WeChatProvider(Request::create('foo'), 'client_id', null, 'redirect-url');
  44. $provider->component(new WeChatComponent());
  45. $this->assertSame([
  46. 'appid' => 'client_id',
  47. 'redirect_uri' => 'redirect-url',
  48. 'response_type' => 'code',
  49. 'scope' => 'snsapi_base',
  50. 'state' => 'state',
  51. 'connect_redirect' => 1,
  52. 'component_appid' => 'component-app-id',
  53. ], $provider->codeFields('state'));
  54. $this->assertSame([
  55. 'appid' => 'client_id',
  56. 'component_appid' => 'component-app-id',
  57. 'component_access_token' => 'token',
  58. 'code' => 'simcode',
  59. 'grant_type' => 'authorization_code',
  60. ], $provider->tokenFields('simcode'));
  61. $this->assertSame('https://api.weixin.qq.com/sns/oauth2/component/access_token', $provider->tokenUrl());
  62. }
  63. public function testOpenPlatformComponentWithCustomParameters()
  64. {
  65. $provider = new WeChatProvider(Request::create('foo'), 'client_id', null, 'redirect-url');
  66. $provider->component(new WeChatComponent());
  67. $provider->with(['foo' => 'bar']);
  68. $fields = $provider->codeFields('wechat-state');
  69. $this->assertArrayHasKey('foo', $fields);
  70. $this->assertSame('bar', $fields['foo']);
  71. }
  72. }
  73. trait ProviderTrait
  74. {
  75. public function tokenUrl()
  76. {
  77. return $this->getTokenUrl();
  78. }
  79. public function tokenFields($code)
  80. {
  81. return $this->getTokenFields($code);
  82. }
  83. public function codeFields($state = null)
  84. {
  85. return $this->getCodeFields($state);
  86. }
  87. }
  88. class WeChatProvider extends RealWeChatProvider
  89. {
  90. use ProviderTrait;
  91. }
  92. class WeChatComponent implements \Overtrue\Socialite\WeChatComponentInterface
  93. {
  94. public function getAppId()
  95. {
  96. return 'component-app-id';
  97. }
  98. public function getToken()
  99. {
  100. return 'token';
  101. }
  102. }