-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserRepositoryTest.php
More file actions
91 lines (74 loc) · 3.15 KB
/
Copy pathUserRepositoryTest.php
File metadata and controls
91 lines (74 loc) · 3.15 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
namespace App\Tests\Integration\Repository;
use App\Entity\User;
use App\Enum\UserStatus;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
/**
* Cover the `PasswordUpgraderInterface` hook on {@see UserRepository}.
*
* Symfony Security calls `upgradePassword()` automatically during
* authentication when it detects a hash that needs rehashing (e.g.
* the configured cost has increased). The functional login test does
* not exercise that path because the fixtures already hash with the
* current algorithm, so we cover the upgrade method directly here.
*
* Uses baseline alice from `UserFixtures`, loaded by
* `tests/bootstrap_integration.php`.
*/
final class UserRepositoryTest extends KernelTestCase
{
private UserRepository $repository;
protected function setUp(): void
{
self::bootKernel();
$container = self::getContainer();
$this->repository = $container->get(UserRepository::class);
}
// Tests that upgradePassword() writes the new hash on a fixture user and the change persists across reloads.
public function testUpgradePasswordWritesTheNewHash(): void
{
$alice = $this->repository->findOneBy(['email' => 'alice@example.test']);
self::assertNotNull($alice);
$oldHash = $alice->getPassword();
$this->repository->upgradePassword($alice, 'a-new-hash');
self::assertSame('a-new-hash', $alice->getPassword());
$reloaded = $this->repository->find($alice->getId());
self::assertNotNull($reloaded);
self::assertSame('a-new-hash', $reloaded->getPassword());
self::assertNotSame($oldHash, $reloaded->getPassword());
}
// Ensures upgradePassword() throws UnsupportedUserException when handed a user not of the App\Entity\User class.
public function testUpgradePasswordRejectsForeignUserType(): void
{
$foreignUser = new class () implements PasswordAuthenticatedUserInterface {
public function getPassword(): ?string
{
return null;
}
};
$this->expectException(UnsupportedUserException::class);
$this->repository->upgradePassword($foreignUser, 'irrelevant');
}
// Verifies the UserStatus enum mapping round-trips: persisted then reloaded keeps the same enum case.
public function testStatusEnumRoundTripsThroughThePersistedRow(): void
{
$em = self::getContainer()->get(EntityManagerInterface::class);
$user = (new User())
->setEmail('eve@example.test')
->setName('Eve')
->setPassword('hash')
->setStatus(UserStatus::Blocked);
$em->persist($user);
$em->flush();
$em->clear();
$reloaded = $this->repository->find($user->getId());
self::assertNotNull($reloaded);
self::assertSame('Eve', $reloaded->getName());
self::assertSame(UserStatus::Blocked, $reloaded->getStatus());
}
}