WechatProviderTest.php 3.3 KB

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