116 lines
4.1 KiB
PHP
116 lines
4.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 文件功能:微信机器人 Kafka 消息消费及密码重置测试
|
||
|
|
*
|
||
|
|
* 覆盖在微信上对助手机器人发送重置密码的拦截逻辑,
|
||
|
|
* 验证对未绑定账号及已绑定账号的回复行为和密码更新结果。
|
||
|
|
*
|
||
|
|
* @author ChatRoom Laravel
|
||
|
|
*
|
||
|
|
* @version 1.0.0
|
||
|
|
*/
|
||
|
|
|
||
|
|
namespace Tests\Feature;
|
||
|
|
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Services\WechatBot\KafkaConsumerService;
|
||
|
|
use App\Services\WechatBot\WechatBotApiService;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Support\Facades\Hash;
|
||
|
|
use Mockery\MockInterface;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 验证 ConsumeWechatMessages 控制台消费命令的解析与动作路由行为。
|
||
|
|
*/
|
||
|
|
class ConsumeWechatMessagesTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 当微信用户未绑定任何聊天室账户时发送“重置密码”,回复未绑定指引微信。
|
||
|
|
*/
|
||
|
|
public function test_wechat_password_reset_fails_when_not_bound(): void
|
||
|
|
{
|
||
|
|
$apiServiceMock = $this->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));
|
||
|
|
}
|
||
|
|
}
|