UserTest.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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\AccessToken;
  11. use Overtrue\Socialite\User;
  12. use PHPUnit\Framework\TestCase;
  13. class UserTest extends TestCase
  14. {
  15. public function testJsonserialize()
  16. {
  17. $this->assertSame('[]', json_encode(new User([])));
  18. $this->assertSame('{"token":"mock-token"}', json_encode(new User(['token' => new AccessToken(['access_token' => 'mock-token'])])));
  19. }
  20. public function test_it_can_get_refresh_token()
  21. {
  22. $user = new User([
  23. 'access_token' => 'mock-token',
  24. 'refresh_token' => 'fake_refresh',
  25. ]);
  26. // 能通过用 User 对象获取 refresh token
  27. $this->assertSame('fake_refresh', $user->getRefreshToken());
  28. // json 序列化只有 token 字段
  29. $this->assertSame('{"access_token":"mock-token","refresh_token":"fake_refresh"}', json_encode($user));
  30. $user = new User([]);
  31. $user->setToken(new AccessToken([
  32. 'access_token' => 'mock-token',
  33. 'refresh_token' => 'fake_refresh',
  34. ]));
  35. $this->assertSame('fake_refresh', $user->getRefreshToken());
  36. $this->assertSame('{"token":"mock-token","access_token":"mock-token","refresh_token":"fake_refresh"}', json_encode($user));
  37. }
  38. }