Passed
Push — master ( bf47d9...8e5e80 )
by Adrien
07:36
created

UserRepositoryTest::assertNoStamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Repository;
6
7
use Application\Model\User;
8
use Application\Repository\UserRepository;
9
use ApplicationTest\Traits\LimitedAccessSubQuery;
10
11
class UserRepositoryTest extends AbstractRepositoryTest
12
{
13
    use LimitedAccessSubQuery;
14
15
    private UserRepository $repository;
16
17
    protected function setUp(): void
18
    {
19
        parent::setUp();
20
        $this->repository = $this->getEntityManager()->getRepository(User::class);
21
    }
22
23
    public function providerGetAccessibleSubQuery(): array
24
    {
25
        $all = range(1000, 1015);
26
27
        return [
28
            ['anonymous', []],
29
            ['accounting_verificator', []],
30
            ['bookingonly', $all],
31
            ['individual', $all],
32
            ['member', $all],
33
            ['trainer', $all],
34
            ['formationresponsible', $all],
35
            ['responsible', $all],
36
            ['administrator', $all],
37
        ];
38
    }
39
40
    public function testGetOneByLoginPassword(): void
41
    {
42
        self::assertNull($this->repository->getOneByLoginPassword('foo', 'bar'), 'wrong user');
43
        self::assertNull($this->repository->getOneByLoginPassword('administrator', 'bar'), 'wrong password');
44
45
        $user = $this->repository->getOneByLoginPassword('administrator', 'administrator');
46
        self::assertNotNull($user);
47
        self::assertSame(1000, $user->getId());
48
49
        $hash = $this->getEntityManager()->getConnection()->executeQuery('SELECT password FROM `user` WHERE id = 1000')->fetchOne();
50
        self::assertStringStartsWith('$', $hash, 'password should have been re-hashed automatically');
0 ignored issues
show
Bug introduced by
It seems like $hash can also be of type false; however, parameter $string of PHPUnit\Framework\Assert::assertStringStartsWith() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        self::assertStringStartsWith('$', /** @scrutinizer ignore-type */ $hash, 'password should have been re-hashed automatically');
Loading history...
51
        self::assertNotSame(md5('administrator'), $hash, 'password should have been re-hashed automatically');
52
    }
53
54
    public function testGetOneByLogin(): void
55
    {
56
        self::assertNull($this->repository->getOneById(1), 'wrong user');
57
58
        $user = $this->repository->getOneById(1000);
59
        self::assertNotNull($user);
60
        self::assertSame(1000, $user->getId());
61
    }
62
63
    public function testGetAllAdministratorsToNotify(): void
64
    {
65
        $actual = $this->repository->getAllAdministratorsToNotify();
66
        self::assertCount(1, $actual);
67
    }
68
69
    public function testGetAllToQueueBalanceMessage(): void
70
    {
71
        $actual = $this->repository->getAllToQueueBalanceMessage();
72
        self::assertCount(2, $actual);
73
74
        $actualBookings = [];
75
        foreach ($actual[0]->getBookings() as $booking) {
76
            $actualBookings[] = $booking->getId();
77
        }
78
79
        $expected = [4004, 4005, 4015];
80
        self::assertSame($expected, $actualBookings, 'must have pre-loaded only the bookings that we are interested in');
81
    }
82
83
    public function testGetAllToQueueBalanceMessageNegative(): void
84
    {
85
        $actual = $this->repository->getAllToQueueBalanceMessage(true);
86
        self::assertCount(0, $actual);
87
    }
88
89
    /**
90
     * @dataProvider providerGetOneByLoginOrEmail
91
     */
92
    public function testGetOneByLoginOrEmail(?string $loginOrEmail, ?int $expected): void
93
    {
94
        $actual = $this->repository->getOneByLoginOrEmail($loginOrEmail);
95
        self::assertSame($expected, $actual ? $actual->getId() : $actual);
96
    }
97
98
    public function providerGetOneByLoginOrEmail(): iterable
99
    {
100
        yield [null, null];
101
        yield ['', null];
102
        yield ['non-existing', null];
103
        yield ['member', 1002];
104
        yield ['[email protected]', 1002];
105
        yield ['son', 1008];
106
    }
107
108
    public function testRecordLogin(): void
109
    {
110
        $this->setCurrentUser('administrator');
111
112
        /** @var User $user */
113
        $user = $this->getEntityManager()->getReference(User::class, 1002);
114
115
        self::assertNull($user->getFirstLogin());
116
        self::assertNull($user->getLastLogin());
117
        $this->assertNoStamp($user);
118
119
        $user->recordLogin();
120
        _em()->flush();
121
122
        $firstLogin = $user->getFirstLogin();
123
        $lastLogin = $user->getLastLogin();
124
        self::assertNotNull($firstLogin);
125
        self::assertNotNull($lastLogin);
126
        $this->assertNoStamp($user);
127
128
        $user->recordLogin();
129
        _em()->flush();
130
131
        $firstLogin2 = $user->getFirstLogin();
132
        $lastLogin2 = $user->getLastLogin();
133
        self::assertSame($firstLogin, $firstLogin2);
134
        self::assertNotSame($lastLogin, $lastLogin2);
135
        self::assertNotNull($firstLogin2);
136
        self::assertNotNull($lastLogin2);
137
        $this->assertNoStamp($user);
138
    }
139
140
    private function assertNoStamp(User $user): void
141
    {
142
        $count = $this->getEntityManager()->getConnection()->fetchOne('SELECT COUNT(*) FROM user WHERE id = ' . $user->getId() . ' AND creation_date IS NULL AND creator_id IS NULL AND update_date IS NULL AND updater_id IS NULL');
143
        self::assertSame(1, $count);
144
    }
145
}
146