UserTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace GuoJiangClub\Component\User\Test;
  3. use GuoJiangClub\Component\User\Models\User;
  4. use GuoJiangClub\Component\User\Models\UserBind;
  5. class UserTest extends BaseTest
  6. {
  7. public function testGetUserByCredentials()
  8. {
  9. $where['mobile'] = '18684738715';
  10. $user = $this->user->getUserByCredentials($where);
  11. $this->assertEquals($user->count(), 1);
  12. $this->assertSame($user->mobile, $where['mobile']);
  13. $appId = 'wx49f648b10ef7f110b';
  14. $userBind = $this->userBind->getByUserIdAndAppId($user->id, $appId);
  15. $this->assertSame($userBind->count(), 1);
  16. }
  17. public function testGetByOpenId()
  18. {
  19. $openid = 'ovNizjmZxfWoVZ28vLdElWi8YsAw';
  20. $userBind = $this->userBind->getByOpenId($openid);
  21. $this->assertSame($userBind->count(), 1);
  22. }
  23. public function testByOpenIdAndType()
  24. {
  25. $openid = 'ovNizjmZxfWoVZ28vLdElWi8YsAw';
  26. $userBind = UserBind::ByOpenIdAndType($openid, UserBind::TYPE_WECHAT)->first();
  27. $this->assertSame($userBind->count(), 1);
  28. }
  29. public function testByUserIdAndType()
  30. {
  31. $user = User::find(1);
  32. $userBind = UserBind::ByUserIdAndType($user->id, UserBind::TYPE_WECHAT)->first();
  33. $this->assertSame($userBind->count(), 1);
  34. }
  35. public function testByAppID()
  36. {
  37. $user = User::find(1);
  38. $appId = 'wx49f648b10ef7f110b';
  39. $userBind = UserBind::ByAppID($user->id, UserBind::TYPE_WECHAT, $appId)->first();
  40. $this->assertSame($userBind->count(), 1);
  41. }
  42. public function testBindUser()
  43. {
  44. $user = User::find(1);
  45. $user->delete();
  46. $user = User::create([
  47. 'name' => 'tangqi',
  48. 'nick_name' => '承诺',
  49. 'email' => '923332947@qq.com',
  50. 'mobile' => '18684738715',
  51. 'password' => '123456',
  52. 'status' => '1',
  53. 'sex' => '男',
  54. 'avatar' => 'https://avatars1.githubusercontent.com/u/11305589?s=400&u=e05d3607f4347e3b739a995ea3b9d2374476627d&v=4',
  55. 'city' => '长沙市',
  56. ]);
  57. $openid = 'ovNizjmZxfWoVZ28vLdElWi8YsAw';
  58. $result = UserBind::bindUser($openid, UserBind::TYPE_WECHAT, $user->id);
  59. $this->assertEquals($result, 1);
  60. $result = $this->userBind->bindToUser($openid, $user->id);
  61. $this->assertEquals($result->toArray()['user_id'], $user->id);
  62. }
  63. /**
  64. * @expectedException \Exception
  65. * @expectedExceptionMessage This user bind model does not exist.
  66. */
  67. public function testException()
  68. {
  69. $user = User::find(1);
  70. $user->delete();
  71. $user = User::create([
  72. 'name' => 'tangqi',
  73. 'nick_name' => '承诺',
  74. 'email' => '923332947@qq.com',
  75. 'mobile' => '18684738715',
  76. 'password' => '123456',
  77. 'status' => '1',
  78. 'sex' => '男',
  79. 'avatar' => 'https://avatars1.githubusercontent.com/u/11305589?s=400&u=e05d3607f4347e3b739a995ea3b9d2374476627d&v=4',
  80. 'city' => '长沙市',
  81. ]);
  82. $openid = 'ovNizjmZxfWoVZ28vLdElWi8aaaa';
  83. $this->userBind->bindToUser($openid, $user->id);
  84. }
  85. }