Failed Conditions
Push — master ( 5e6761...398dce )
by Adrien
04:14 queued 01:57
created

HasPasswordTest.php$0 ➔ testToken()   A

Complexity

Conditions 1

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 41
cc 1
rs 9.264
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Model\Traits;
6
7
use Cake\Chronos\Chronos;
8
use Ecodev\Felix\Model\Traits\HasPassword;
9
use PHPUnit\Framework\TestCase;
10
11
class HasPasswordTest extends TestCase
12
{
13
    /**
14
     * @var \Ecodev\Felix\Model\HasPassword
15
     */
16
    private $user;
17
18
    protected function setUp(): void
19
    {
20
        $this->user = new class() implements \Ecodev\Felix\Model\HasPassword {
21
            use HasPassword;
22
        };
23
    }
24
25
    public function testSetPassword(): void
26
    {
27
        self::assertSame('', $this->user->getPassword(), 'should have no password at first');
28
29
        $this->user->setPassword('12345');
30
        $actual1 = $this->user->getPassword();
31
        self::assertNotSame('', $actual1, 'should be able to change password ');
32
        self::assertTrue(password_verify('12345', $actual1), 'password must have been hashed');
33
34
        $this->user->setPassword('');
35
        $actual2 = $this->user->getPassword();
36
        self::assertSame($actual1, $actual2, 'should ignore empty password');
37
38
        $this->user->setPassword('money');
39
        $actual3 = $this->user->getPassword();
40
        self::assertNotSame($actual1, $actual3, 'should be able to change to something else');
41
        self::assertTrue(password_verify('money', $actual3), 'password must have been hashed again');
42
    }
43
44
    public function testToken(): void
45
    {
46
        self::assertFalse($this->user->isTokenValid(), 'new user should not be valid');
47
48
        $token1 = $this->user->createToken();
49
        self::assertEquals(32, mb_strlen($token1), 'must be exactly the length of DB field');
50
        self::assertTrue($this->user->isTokenValid(), 'brand new token is valid');
51
52
        $token2 = $this->user->createToken();
53
        self::assertEquals(32, mb_strlen($token2), 'must be exactly the length of DB field');
54
        self::assertTrue($this->user->isTokenValid(), 'second created token is valid');
55
56
        $this->user->revokeToken();
57
        self::assertFalse($this->user->isTokenValid(), 'once user is logged in token is invalid');
58
59
        $token3 = $this->user->createToken();
60
        self::assertEquals(32, mb_strlen($token3), 'must be exactly the length of DB field');
61
        self::assertTrue($this->user->isTokenValid(), 'third created token is valid');
62
63
        $token4 = $this->user->createToken();
64
        self::assertEquals(32, mb_strlen($token4), 'must be exactly the length of DB field');
65
        self::assertTrue($this->user->isTokenValid(), 'third created token is valid');
66
67
        $this->user->setPassword('money');
68
        self::assertFalse($this->user->isTokenValid(), 'after password change token is invalid');
69
70
        Chronos::setTestNow((new Chronos())->subDay(1));
71
        $token5 = $this->user->createToken();
72
        Chronos::setTestNow(null);
73
        self::assertEquals(32, mb_strlen($token5), 'must be exactly the length of DB field');
74
        self::assertFalse($this->user->isTokenValid(), 'too old token is invalid');
75
76
        $allTokens = [
77
            $token1,
78
            $token2,
79
            $token3,
80
            $token4,
81
            $token5,
82
        ];
83
84
        self::assertCount(5, array_unique($allTokens), 'all tokens must be unique');
85
    }
86
}
87