mock(WechatBotApiService::class, function (MockInterface $mock) { $mock->shouldReceive('sendTextMessage') ->once() ->with('wxid_test_user_123', \Mockery::on(function ($arg) { return str_contains($arg, '密码重置失败') && str_contains($arg, '尚未绑定'); })) ->andReturn([]); }); $msg = [ 'msg_type' => 1, 'content' => '重置密码', 'from_user' => 'wxid_test_user_123', 'is_chatroom' => false, ]; $command = new \App\Console\Commands\ConsumeWechatMessages( $this->createMock(KafkaConsumerService::class) ); $command->setLaravel($this->app); // 绑定 NullOutput 避免 $this->info 报 null 指针错误 $output = new \Symfony\Component\Console\Output\NullOutput; $reflectorOutput = new \ReflectionProperty($command, 'output'); $reflectorOutput->setValue($command, new \Illuminate\Console\OutputStyle( new \Symfony\Component\Console\Input\ArrayInput([]), $output )); $reflector = new \ReflectionMethod($command, 'processMessage'); $reflector->invoke($command, $msg, $apiServiceMock); } /** * 当已绑定微信的用户在微信私聊中发送“重置密码”时,自动随机生成 8 位密码并完成更新,通过微信私信回发。 */ public function test_wechat_password_reset_succeeds_when_bound(): void { $user = User::factory()->create([ 'username' => 'test_bind_user', 'wxid' => 'wxid_test_user_123', 'password' => Hash::make('old_password_123'), ]); $apiServiceMock = $this->mock(WechatBotApiService::class, function (MockInterface $mock) use ($user) { $mock->shouldReceive('sendTextMessage') ->once() ->with('wxid_test_user_123', \Mockery::on(function ($arg) use ($user) { return str_contains($arg, '密码重置成功') && str_contains($arg, $user->username); })) ->andReturn([]); }); $msg = [ 'msg_type' => 1, 'content' => ' 想要重置密码呢 ', 'from_user' => 'wxid_test_user_123', 'is_chatroom' => false, ]; $command = new \App\Console\Commands\ConsumeWechatMessages( $this->createMock(KafkaConsumerService::class) ); $command->setLaravel($this->app); // 绑定 NullOutput $output = new \Symfony\Component\Console\Output\NullOutput; $reflectorOutput = new \ReflectionProperty($command, 'output'); $reflectorOutput->setValue($command, new \Illuminate\Console\OutputStyle( new \Symfony\Component\Console\Input\ArrayInput([]), $output )); $reflector = new \ReflectionMethod($command, 'processMessage'); $reflector->invoke($command, $msg, $apiServiceMock); $user->refresh(); $this->assertFalse(Hash::check('old_password_123', $user->password)); } }