Repository navigation
Expand file tree
/
Copy pathFindUserByUsernameTraitTest.php
More file actions
64 lines (54 loc) · 2.41 KB
/
Copy pathFindUserByUsernameTraitTest.php
File metadata and controls
64 lines (54 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/*
* This file is part of the Silverback API Components Bundle Project
*
* (c) Daniel West <daniel@silverback.is>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silverback\ApiComponentsBundle\Tests\Repository\User;
use Doctrine\ORM\NonUniqueResultException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
use Silverback\ApiComponentsBundle\Repository\User\FindUserByUsernameTrait;
use Silverback\ApiComponentsBundle\Repository\User\UserRepositoryInterface;
use Silverback\ApiComponentsBundle\Tests\Functional\TestBundle\Entity\User;
final class FindUserByUsernameTraitTest extends TestCase
{
/**
* @return iterable<string, array{string, string, bool}>
*/
public static function usernames(): iterable
{
yield 'the username' => ['my_username', 'my_username', true];
yield 'the username in capitals with surrounding space' => ['my_username', ' MY_USERNAME ', true];
yield 'a username stored in capitals' => ['My_Username', 'my_username', true];
yield 'the email address the identifier also matches' => ['my_username', 'user@example.com', false];
}
#[DataProvider('usernames')]
public function test_only_the_users_own_username_finds_them(string $storedUsername, string $username, bool $found): void
{
$user = new User($storedUsername, 'user@example.com');
$repository = $this->createStub(UserRepositoryInterface::class);
$repository->method('loadUserByIdentifier')->willReturn($user);
self::assertSame($found ? $user : null, $this->finder()->find($repository, $username));
}
public function test_a_username_matching_several_users_finds_none(): void
{
$repository = $this->createStub(UserRepositoryInterface::class);
$repository->method('loadUserByIdentifier')->willThrowException(new NonUniqueResultException());
self::assertNull($this->finder()->find($repository, 'my_username'));
}
private function finder(): object
{
return new class {
use FindUserByUsernameTrait;
public function find(UserRepositoryInterface $repository, string $username): ?AbstractUser
{
return $this->findUserByUsernameIn($repository, $username);
}
};
}
}