95 lines
2.7 KiB
PHP
95 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\User\UseCases;
|
|
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Shared\ValueObject\EmailAddress;
|
|
use App\User\CreateUserDto;
|
|
use App\User\UseCases\SearchUsers\SearchUsers;
|
|
use App\User\UseCases\SearchUsers\SearchUsersRequest;
|
|
use Tests\Fakes\FakeUserRepository;
|
|
use Tests\TestCase;
|
|
|
|
class SearchUsersTest extends TestCase
|
|
{
|
|
private FakeUserRepository $userRepo;
|
|
|
|
private SearchUsers $useCase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->userRepo = new FakeUserRepository;
|
|
$this->useCase = new SearchUsers($this->userRepo);
|
|
|
|
$this->userRepo->create(new CreateUserDto(
|
|
email: new EmailAddress('alice@example.com'),
|
|
displayName: 'alice',
|
|
passwordHash: '',
|
|
isAdmin: false,
|
|
emailConfirmedAt: null,
|
|
));
|
|
$this->userRepo->create(new CreateUserDto(
|
|
email: new EmailAddress('alex@example.com'),
|
|
displayName: 'alex',
|
|
passwordHash: '',
|
|
isAdmin: false,
|
|
emailConfirmedAt: null,
|
|
));
|
|
$this->userRepo->create(new CreateUserDto(
|
|
email: new EmailAddress('bob@other.com'),
|
|
displayName: 'bob',
|
|
passwordHash: '',
|
|
isAdmin: false,
|
|
emailConfirmedAt: null,
|
|
));
|
|
}
|
|
|
|
public function test_blank_query_throws_bad_request(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->useCase->execute(new SearchUsersRequest(query: ' '));
|
|
}
|
|
|
|
public function test_finds_users_by_display_name_prefix(): void
|
|
{
|
|
$results = $this->useCase->execute(new SearchUsersRequest(
|
|
query: 'al',
|
|
));
|
|
|
|
$this->assertCount(2, $results);
|
|
$names = array_map(
|
|
function ($user) {
|
|
return $user->getDisplayName();
|
|
},
|
|
$results,
|
|
);
|
|
sort($names);
|
|
$this->assertSame(['alex', 'alice'], $names);
|
|
}
|
|
|
|
public function test_search_is_case_insensitive(): void
|
|
{
|
|
$results = $this->useCase->execute(new SearchUsersRequest(
|
|
query: 'ALI',
|
|
));
|
|
$this->assertCount(1, $results);
|
|
$this->assertSame('alice', $results[0]->getDisplayName());
|
|
}
|
|
|
|
public function test_finds_users_by_email_prefix(): void
|
|
{
|
|
$results = $this->useCase->execute(new SearchUsersRequest(
|
|
query: 'bob@',
|
|
));
|
|
$this->assertCount(1, $results);
|
|
$this->assertSame('bob', $results[0]->getDisplayName());
|
|
}
|
|
|
|
public function test_no_results_when_no_match(): void
|
|
{
|
|
$results = $this->useCase->execute(new SearchUsersRequest(
|
|
query: 'zzz',
|
|
));
|
|
$this->assertSame([], $results);
|
|
}
|
|
}
|