WechatTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use Overtrue\Socialite\Providers\WeChat;
  4. class WechatTest extends TestCase
  5. {
  6. public function testWeChatProviderHasCorrectlyRedirectResponse()
  7. {
  8. $response = (new WeChat([
  9. 'client_id' => 'client_id',
  10. 'client_secret' => 'client_secret',
  11. 'redirect_url' => 'http://localhost/socialite/callback.php',
  12. ]))->redirect();
  13. $this->assertStringStartsWith('https://open.weixin.qq.com/connect/qrconnect', $response);
  14. $this->assertRegExp('/redirect_uri=http%3A%2F%2Flocalhost%2Fsocialite%2Fcallback.php/', $response);
  15. }
  16. public function testWeChatProviderTokenUrlAndRequestFields()
  17. {
  18. $provider = new WeChat([
  19. 'client_id' => 'client_id',
  20. 'client_secret' => 'client_secret',
  21. 'redirect_url' => 'http://localhost/socialite/callback.php',
  22. ]);
  23. $getTokenUrl = new ReflectionMethod(WeChat::class, 'getTokenUrl');
  24. $getTokenUrl->setAccessible(true);
  25. $getTokenFields = new ReflectionMethod(WeChat::class, 'getTokenFields');
  26. $getTokenFields->setAccessible(true);
  27. $getCodeFields = new ReflectionMethod(WeChat::class, 'getCodeFields');
  28. $getCodeFields->setAccessible(true);
  29. $this->assertSame('https://api.weixin.qq.com/sns/oauth2/access_token', $getTokenUrl->invoke($provider));
  30. $this->assertSame([
  31. 'appid' => 'client_id',
  32. 'secret' => 'client_secret',
  33. 'code' => 'iloveyou',
  34. 'grant_type' => 'authorization_code',
  35. ], $getTokenFields->invoke($provider, 'iloveyou'));
  36. $this->assertSame([
  37. 'appid' => 'client_id',
  38. 'redirect_uri' => 'http://localhost/socialite/callback.php',
  39. 'response_type' => 'code',
  40. 'scope' => 'snsapi_login',
  41. 'state' => 'wechat-state',
  42. 'connect_redirect' => 1,
  43. ], $getCodeFields->invoke($provider->withState('wechat-state')));
  44. }
  45. public function testOpenPlatformComponent()
  46. {
  47. $provider = new WeChat([
  48. 'client_id' => 'client_id',
  49. 'client_secret' => null,
  50. 'redirect' => 'redirect-url',
  51. 'component' => [
  52. 'id' => 'component-app-id',
  53. 'token' => 'token',
  54. ],
  55. ]);
  56. $getTokenUrl = new ReflectionMethod(WeChat::class, 'getTokenUrl');
  57. $getTokenUrl->setAccessible(true);
  58. $getTokenFields = new ReflectionMethod(WeChat::class, 'getTokenFields');
  59. $getTokenFields->setAccessible(true);
  60. $getCodeFields = new ReflectionMethod(WeChat::class, 'getCodeFields');
  61. $getCodeFields->setAccessible(true);
  62. $this->assertSame([
  63. 'appid' => 'client_id',
  64. 'redirect_uri' => 'redirect-url',
  65. 'response_type' => 'code',
  66. 'scope' => 'snsapi_base',
  67. 'state' => 'state',
  68. 'connect_redirect' => 1,
  69. 'component_appid' => 'component-app-id',
  70. ], $getCodeFields->invoke($provider->withState('state')));
  71. $this->assertSame([
  72. 'appid' => 'client_id',
  73. 'component_appid' => 'component-app-id',
  74. 'component_access_token' => 'token',
  75. 'code' => 'simcode',
  76. 'grant_type' => 'authorization_code',
  77. ], $getTokenFields->invoke($provider, 'simcode'));
  78. $this->assertSame('https://api.weixin.qq.com/sns/oauth2/component/access_token', $getTokenUrl->invoke($provider));
  79. }
  80. public function testOpenPlatformComponentWithCustomParameters()
  81. {
  82. $provider = new WeChat([
  83. 'client_id' => 'client_id',
  84. 'client_secret' => null,
  85. 'redirect' => 'redirect-url',
  86. 'component' => [
  87. 'id' => 'component-app-id',
  88. 'token' => 'token',
  89. ],
  90. ]);
  91. $getCodeFields = new ReflectionMethod(WeChat::class, 'getCodeFields');
  92. $getCodeFields->setAccessible(true);
  93. $provider->with(['foo' => 'bar']);
  94. $fields = $getCodeFields->invoke($provider->withState('wechat-state'));
  95. $this->assertArrayHasKey('foo', $fields);
  96. $this->assertSame('bar', $fields['foo']);
  97. }
  98. }