update user list

This commit is contained in:
2026-04-01 13:52:12 +08:00
parent 7c83fa4664
commit 6f7e2fb599
4 changed files with 377 additions and 0 deletions
@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace HyperfTest\Cases\Unit\Command;
use App\Command\UserListCommand;
use Hyperf\Context\ApplicationContext;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
/**
* UserListCommand 单元测试
*
* @internal
* @coversNothing
*/
class UserListCommandTest extends TestCase
{
private function runInCoroutine(callable $callback): void
{
if (\Swoole\Coroutine::getCid() > 0) {
$callback();
return;
}
$exception = null;
\Swoole\Coroutine\run(static function () use ($callback, &$exception): void {
try {
$callback();
} catch (\Throwable $e) {
$exception = $e;
}
});
if ($exception !== null) {
throw $exception;
}
}
public function test_list_all_users_displays_table(): void
{
$this->runInCoroutine(function (): void {
$container = ApplicationContext::getContainer();
$command = new UserListCommand($container);
$tester = new CommandTester($command);
$tester->execute([]);
$output = $tester->getDisplay();
$this->assertStringContainsString('ID', $output);
$this->assertStringContainsString('Username', $output);
$this->assertStringContainsString('Email', $output);
$this->assertStringContainsString('Role', $output);
$this->assertStringContainsString('Status', $output);
$this->assertStringContainsString('Total:', $output);
});
}
public function test_list_users_filtered_by_role(): void
{
$this->runInCoroutine(function (): void {
$container = ApplicationContext::getContainer();
$command = new UserListCommand($container);
$tester = new CommandTester($command);
$tester->execute(['--role' => 'administrator']);
$output = $tester->getDisplay();
$this->assertTrue(
str_contains($output, 'administrator') || str_contains($output, 'No users found'),
'Should display administrator users or empty message'
);
});
}
public function test_list_users_with_nonexistent_role_shows_empty(): void
{
$this->runInCoroutine(function (): void {
$container = ApplicationContext::getContainer();
$command = new UserListCommand($container);
$tester = new CommandTester($command);
$tester->execute(['--role' => 'nonexistent_role_xyz_123']);
$output = $tester->getDisplay();
$this->assertStringContainsString('No users found', $output);
});
}
}
@@ -0,0 +1,148 @@
<?php
declare(strict_types=1);
namespace HyperfTest\Cases\Unit\Command;
use App\Command\UserResetPasswordCommand;
use App\Model\Role;
use App\Model\User;
use Hyperf\Context\ApplicationContext;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
/**
* UserResetPasswordCommand 单元测试
*
* @internal
* @coversNothing
*/
class UserResetPasswordCommandTest extends TestCase
{
private ?int $testUserId = null;
private function runInCoroutine(callable $callback): void
{
if (\Swoole\Coroutine::getCid() > 0) {
$callback();
return;
}
$exception = null;
\Swoole\Coroutine\run(static function () use ($callback, &$exception): void {
try {
$callback();
} catch (\Throwable $e) {
$exception = $e;
}
});
if ($exception !== null) {
throw $exception;
}
}
protected function tearDown(): void
{
if ($this->testUserId !== null) {
$this->runInCoroutine(function (): void {
User::query()->where('id', $this->testUserId)->delete();
});
$this->testUserId = null;
}
parent::tearDown();
}
private function createTestUser(): User
{
$role = Role::query()->first();
$unique = 'test_reset_' . uniqid();
$user = User::create([
'username' => $unique,
'email' => $unique . '@test.com',
'password' => 'old_password_123',
'status' => 1,
'role_id' => $role?->id,
'ext' => [],
]);
$this->testUserId = $user->id;
return $user;
}
public function test_reset_password_by_username(): void
{
$this->runInCoroutine(function (): void {
$test_user = $this->createTestUser();
$container = ApplicationContext::getContainer();
$command = new UserResetPasswordCommand($container);
$tester = new CommandTester($command);
$tester->execute([
'identifier' => $test_user->username,
'--password' => 'new_password_123',
]);
$this->assertSame(0, $tester->getStatusCode());
$this->assertStringContainsString('has been reset', $tester->getDisplay());
// 验证密码确实已更新
$updated_user = User::find($test_user->id);
$this->assertTrue($updated_user->verifyPassword('new_password_123'));
});
}
public function test_reset_password_by_id(): void
{
$this->runInCoroutine(function (): void {
$test_user = $this->createTestUser();
$container = ApplicationContext::getContainer();
$command = new UserResetPasswordCommand($container);
$tester = new CommandTester($command);
$tester->execute([
'identifier' => (string) $test_user->id,
'--password' => 'another_pwd_456',
]);
$this->assertSame(0, $tester->getStatusCode());
$this->assertStringContainsString('has been reset', $tester->getDisplay());
$updated_user = User::find($test_user->id);
$this->assertTrue($updated_user->verifyPassword('another_pwd_456'));
});
}
public function test_user_not_found_shows_error(): void
{
$this->runInCoroutine(function (): void {
$container = ApplicationContext::getContainer();
$command = new UserResetPasswordCommand($container);
$tester = new CommandTester($command);
$tester->execute([
'identifier' => 'nonexistent_user_xyz_999',
'--password' => 'some_password',
]);
$this->assertSame(1, $tester->getStatusCode());
$this->assertStringContainsString('not found', $tester->getDisplay());
});
}
public function test_password_too_short_shows_error(): void
{
$this->runInCoroutine(function (): void {
$test_user = $this->createTestUser();
$container = ApplicationContext::getContainer();
$command = new UserResetPasswordCommand($container);
$tester = new CommandTester($command);
$tester->execute([
'identifier' => $test_user->username,
'--password' => '12345',
]);
$this->assertSame(1, $tester->getStatusCode());
$this->assertStringContainsString('at least 6', $tester->getDisplay());
});
}
}