set('database.default', 'sqlite'); config()->set('database.connections.sqlite.database', ':memory:'); DB::purge('sqlite'); Schema::create('admins', function (Blueprint $table) { $table->increments('id'); $table->string('mobile'); $table->softDeletes(); }); Schema::create('configs', function (Blueprint $table) { $table->increments('id'); $table->string('key'); $table->string('value'); $table->softDeletes(); }); DB::table('admins')->insert(['mobile' => '13800138000']); DB::table('configs')->insert(['key' => 'sms_sign', 'value' => '测试']); } public function test_provider_failure_and_unknown_mobile_have_the_same_response(): void { $challengeId = '4d2fd7fc-2630-4432-8df5-34f3edc029d0'; $service = Mockery::mock(AdminSmsChallengeService::class); $service->shouldReceive('canSend')->twice()->andReturn(true); $service->shouldReceive('issueChallenge')->once()->andReturn(null); $service->shouldReceive('issueDecoyChallenge')->twice()->andReturn($challengeId); $this->app->instance(AdminSmsChallengeService::class, $service); $registered = $this->postJson('/api/admin/auth/send-sms', ['mobile' => '13800138000']); $unknown = $this->postJson('/api/admin/auth/send-sms', ['mobile' => '13900139000']); $registered->assertOk()->assertExactJson([ 'message' => '如果手机号已登记,验证码将发送', 'challenge_id' => $challengeId, ]); $unknown->assertOk()->assertExactJson($registered->json()); } public function test_challenge_store_failure_returns_a_controlled_error(): void { $service = Mockery::mock(AdminSmsChallengeService::class); $service->shouldReceive('canSend')->once()->andReturn(true); $service->shouldReceive('issueChallenge')->once()->andReturn(null); $service->shouldReceive('issueDecoyChallenge')->once()->andThrow(new \RuntimeException('cache unavailable')); $this->app->instance(AdminSmsChallengeService::class, $service); $this->postJson('/api/admin/auth/send-sms', ['mobile' => '13800138000']) ->assertOk() ->assertExactJson([ 'errcode' => 10003, 'errmsg' => '验证码服务暂不可用,请稍后重试', ]); } }