smsService = new SmsService(); } public function testSendCodeSuccessfully() { $result = $this->smsService->sendCode(CODE_TYPE::LOGIN, $this->testPhone, $this->testToken); $this->assertTrue($result); // 验证数据库中是否保存了验证码记录 $smsCode = SmsCode::query() ->where('mobile', $this->testPhone) ->where('type', CODE_TYPE::LOGIN->value) ->orderByDesc('id') ->first(); dump($smsCode->id); $this->assertNotNull($smsCode); $this->assertEquals($this->testPhone, $smsCode->mobile); $this->assertEquals($this->testToken, $smsCode->token); $this->assertEquals(CODE_TYPE::LOGIN->value, $smsCode->type); $this->assertMatchesRegularExpression('/^\d{6}$/', $smsCode->code_value); } public function testSendCodeWithInvalidType() { $this->expectException(\TypeError::class); $this->smsService->sendCode(999, $this->testPhone, $this->testToken); } public function testVerifyCodeWithInvalidParams() { $result = $this->smsService->verifyCode(CODE_TYPE::REGISTER->value, '', ''); $this->assertFalse($result); } public function testVerifyCodeWithExpiredCode() { // 创建一个过期的验证码记录 $expiredCode = new SmsCode(); $expiredCode->mobile = $this->testPhone; $expiredCode->token = $this->testToken; $expiredCode->code_value = '123456'; $expiredCode->type = CODE_TYPE::LOGIN->value; $expiredCode->created_at = Carbon::now()->subSeconds(SmsService::CODE_EXPIRE_TIME + 1); $expiredCode->save(); $result = $this->smsService->verifyCode(CODE_TYPE::LOGIN->value, $this->testPhone, '123456'); $this->assertFalse($result); // 验证过期的验证码记录是否被删除 $this->assertDatabaseMissing('sms_code', ['id' => $expiredCode->id]); } public function testVerifyCodeWithInvalidCode() { // 创建一个有效的验证码记录 $validCode = new SmsCode(); $validCode->mobile = $this->testPhone; $validCode->token = $this->testToken; $validCode->code_value = '123456'; $validCode->type = CODE_TYPE::LOGIN->value; $validCode->created_at = Carbon::now(); $validCode->save(); $result = $this->smsService->verifyCode(CODE_TYPE::LOGIN->value, $this->testPhone, '654321'); $this->assertFalse($result); // 验证错误的验证码不会删除记录 $this->assertDatabaseHas('sms_code', ['id' => $validCode->id]); } public function testVerifyCodeWithValidCode() { // 创建一个有效的验证码记录 $validCode = new SmsCode(); $validCode->mobile = $this->testPhone; $validCode->token = $this->testToken; $validCode->code_value = '123456'; $validCode->type = CODE_TYPE::LOGIN->value; $validCode->created_at = Carbon::now(); $validCode->save(); $result = $this->smsService->verifyCode(CODE_TYPE::LOGIN->value, $this->testPhone, '123456'); $this->assertTrue($result); // 验证成功后记录应该被删除 $this->assertDatabaseMissing('sms_code', ['id' => $validCode->id]); } }