SmsServiceTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Module\Sms\Tests\Unit;
  3. use App\Module\Sms\Enums\Code;
  4. use App\Module\Sms\Enums\CODE_TYPE;
  5. use App\Module\Sms\Models\SmsCode;
  6. use App\Module\Sms\Services\SmsService;
  7. use Carbon\Carbon;
  8. use Tests\TestCase;
  9. use UCore\Exception\LogicException;
  10. class SmsServiceTest extends TestCase
  11. {
  12. protected SmsService $smsService;
  13. protected string $testPhone = '13800138000';
  14. protected string $testToken = 'test_token';
  15. public function setUp(): void
  16. {
  17. parent::setUp();
  18. $this->smsService = new SmsService();
  19. }
  20. public function testSendCodeSuccessfully()
  21. {
  22. $result = $this->smsService->sendCode(CODE_TYPE::LOGIN, $this->testPhone, $this->testToken);
  23. $this->assertTrue($result);
  24. // 验证数据库中是否保存了验证码记录
  25. $smsCode = SmsCode::query()
  26. ->where('mobile', $this->testPhone)
  27. ->where('type', CODE_TYPE::LOGIN->value)
  28. ->orderByDesc('id')
  29. ->first();
  30. dump($smsCode->id);
  31. $this->assertNotNull($smsCode);
  32. $this->assertEquals($this->testPhone, $smsCode->mobile);
  33. $this->assertEquals($this->testToken, $smsCode->token);
  34. $this->assertEquals(CODE_TYPE::LOGIN->value, $smsCode->type);
  35. $this->assertMatchesRegularExpression('/^\d{6}$/', $smsCode->code_value);
  36. }
  37. public function testSendCodeWithInvalidType()
  38. {
  39. $this->expectException(\TypeError::class);
  40. $this->smsService->sendCode(999, $this->testPhone, $this->testToken);
  41. }
  42. public function testVerifyCodeWithInvalidParams()
  43. {
  44. $result = $this->smsService->verifyCode(CODE_TYPE::REGISTER->value, '', '');
  45. $this->assertFalse($result);
  46. }
  47. public function testVerifyCodeWithExpiredCode()
  48. {
  49. // 创建一个过期的验证码记录
  50. $expiredCode = new SmsCode();
  51. $expiredCode->mobile = $this->testPhone;
  52. $expiredCode->token = $this->testToken;
  53. $expiredCode->code_value = '123456';
  54. $expiredCode->type = CODE_TYPE::LOGIN->value;
  55. $expiredCode->created_at = Carbon::now()->subSeconds(SmsService::CODE_EXPIRE_TIME + 1);
  56. $expiredCode->save();
  57. $result = $this->smsService->verifyCode(CODE_TYPE::LOGIN->value, $this->testPhone, '123456');
  58. $this->assertFalse($result);
  59. // 验证过期的验证码记录是否被删除
  60. $this->assertDatabaseMissing('sms_code', ['id' => $expiredCode->id]);
  61. }
  62. public function testVerifyCodeWithInvalidCode()
  63. {
  64. // 创建一个有效的验证码记录
  65. $validCode = new SmsCode();
  66. $validCode->mobile = $this->testPhone;
  67. $validCode->token = $this->testToken;
  68. $validCode->code_value = '123456';
  69. $validCode->type = CODE_TYPE::LOGIN->value;
  70. $validCode->created_at = Carbon::now();
  71. $validCode->save();
  72. $result = $this->smsService->verifyCode(CODE_TYPE::LOGIN->value, $this->testPhone, '654321');
  73. $this->assertFalse($result);
  74. // 验证错误的验证码不会删除记录
  75. $this->assertDatabaseHas('sms_code', ['id' => $validCode->id]);
  76. }
  77. public function testVerifyCodeWithValidCode()
  78. {
  79. // 创建一个有效的验证码记录
  80. $validCode = new SmsCode();
  81. $validCode->mobile = $this->testPhone;
  82. $validCode->token = $this->testToken;
  83. $validCode->code_value = '123456';
  84. $validCode->type = CODE_TYPE::LOGIN->value;
  85. $validCode->created_at = Carbon::now();
  86. $validCode->save();
  87. $result = $this->smsService->verifyCode(CODE_TYPE::LOGIN->value, $this->testPhone, '123456');
  88. $this->assertTrue($result);
  89. // 验证成功后记录应该被删除
  90. $this->assertDatabaseMissing('sms_code', ['id' => $validCode->id]);
  91. }
  92. }