Passed
Push — master ( 784e69...f2d35a )
by Alexander
07:36
created

PasswordHasherTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 18
c 4
b 1
f 0
dl 0
loc 44
rs 10
wmc 4
1
<?php
2
3
namespace Yiisoft\Security\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Yiisoft\Security\PasswordHasher;
7
8
class PasswordHasherTest extends TestCase
9
{
10
    public function testPasswordHashWithDefaults(): void
11
    {
12
        $password = new PasswordHasher();
13
14
        $secret = 'secret';
15
        $hash = $password->hash($secret);
16
17
        $this->assertTrue($password->validate($secret, $hash));
18
        $this->assertFalse($password->validate('test', $hash));
19
    }
20
21
    public function testPasswordHash(): void
22
    {
23
        $password = new PasswordHasher(
24
            PASSWORD_BCRYPT,
25
            [
26
            // minimum blowfish's value is enough for tests
27
            'cost' => 4,
28
        ]
29
        );
30
31
        $secret = 'secret';
32
        $hash = $password->hash($secret);
33
        $this->assertTrue($password->validate($secret, $hash));
34
        $this->assertFalse($password->validate('test', $hash));
35
    }
36
37
    public function testValidateEmptyPasswordException(): void
38
    {
39
        $this->expectException(\InvalidArgumentException::class);
40
41
        $password = new PasswordHasher();
42
        $password->validate('', 'test');
43
    }
44
45
    /**
46
     * In PHP 7.4 password hashing algorithm identifiers are now nullable strings rather than integers.
47
     */
48
    public function testAlgorithmString(): void
49
    {
50
        $password = new PasswordHasher('test');
51
        $this->assertTrue(true);
52
    }
53
}
54