Test Failed
Push — master ( 7e5dab...fc366b )
by Dan
06:52
created

PasswordTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 14
loc 98
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testNeedsRehash() 7 7 1
A testNeedsRehashNoRehash() 7 7 1
A testHash() 0 5 1
A testVerify() 0 5 1
A testFailedVerify() 0 4 1
A hashPasswordProvider() 0 12 1
A truePasswordProvider() 0 11 1
A falsePasswordProvider() 0 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * PasswordTest.php
4
 * User: dansmith
5
 * Date: 05/02/2016
6
 * Time: 10:52
7
 */
8
9
namespace Tests\Authenticate;
10
11
use Ds\Authenticate\Password;
12
13
class PasswordTest extends \PHPUnit_Framework_TestCase
14
{
15
    public $password;
16
17
    protected function setUp()
18
    {
19
        $this->password = new Password();
20
    }
21
22 View Code Duplication
    public function testNeedsRehash()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        $password = 'password';
25
        $hash = password_hash('password', PASSWORD_DEFAULT);
26
        $result = $this->password->needsRehash($password, $hash, PASSWORD_DEFAULT, 12);
27
        $this->assertNotEquals($hash, $result);
28
    }
29
30 View Code Duplication
    public function testNeedsRehashNoRehash()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        $password = 'password';
33
        $hash = password_hash('password', PASSWORD_DEFAULT, ['cost' => 12]);
34
        $result = $this->password->needsRehash($password, $hash, PASSWORD_DEFAULT, 12);
35
        $this->assertEquals(false, $result);
36
    }
37
38
    /**
39
     * @param string $password
40
     * @dataProvider hashPasswordProvider
41
     */
42
    public function testHash($password)
43
    {
44
        $hashedPassword = $this->password->hash($password);
45
        $this->assertEquals(true, $this->password->verify($password, $hashedPassword));
46
    }
47
48
    /**
49
     * @param string $password
50
     * @param string $hashedPassword
51
     * @dataProvider truePasswordProvider
52
     */
53
    public function testVerify($password, $hashedPassword)
54
    {
55
        $passwordHash = $this->password->verify($password, $hashedPassword);
56
        $this->assertEquals($passwordHash, true);
57
    }
58
59
    /**
60
     * @param string $password
61
     * @param string $hashedPassword
62
     * @dataProvider falsePasswordProvider
63
     * @expectedException \Exception
64
     */
65
    public function testFailedVerify($password, $hashedPassword)
66
    {
67
        $this->password->verify($password, $hashedPassword);
68
    }
69
70
    public function hashPasswordProvider()
71
    {
72
        return array(
73
            ['password'],
74
            ['shortpass'],
75
            ['rlysht'],
76
            ['8978798796785897890908'],
77
            ['alongpasswordwithnumbers78890231908132'],
78
            ['areallylongpasswordwithnumbersandspecialchars-890988908!98099___%%£213321'],
79
            ['somethingsolong&*()&*(&*(&**@((@)_)@__@__@)))))@((((@**@*&&&@**@()(@))@_t132123hatitsalmostcertainlyno____tgoingtobeusedinproduct123312123ionunlesssomeone____decidedthattheyarehappywrittingthiseverytimetheywishtologintothesite_withsomenumbersandspecialcharacters']
80
        );
81
    }
82
83
    public function truePasswordProvider()
84
    {
85
        return array(
86
            ['password',\password_hash('password', PASSWORD_DEFAULT)],
87
            ['foobar',\password_hash('foobar', PASSWORD_DEFAULT)],
88
            ['234234234cookie',\password_hash('234234234cookie', PASSWORD_DEFAULT)],
89
            ['fobatraz56278',\password_hash('fobatraz56278', PASSWORD_DEFAULT)],
90
            ['foo__fgg__bat__789',\password_hash('foo__fgg__bat__789', PASSWORD_DEFAULT)],
91
            ['passwdsasdads2123ord',\password_hash('passwdsasdads2123ord', PASSWORD_DEFAULT)]
92
        );
93
    }
94
95
    public function falsePasswordProvider()
96
    {
97
        return array(
98
            ['password','$2y$10$rUHpBM6hi0TyCBIgGnJYAuBPLe66sadkE.RW'],
99
            ['password','cGFzc3dvcmQ='],
100
            ['password','5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'],
101
            ['password','5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8'],
102
            ['password','74dfc2b27acfa364da55f93a5caee29ccad3557247eda238831b3e9bd931b01d77fe994e4f12b9d4cfa92a124461d2065197d8cf7f33fc88566da2db2a4d6eae'],
103
            ['password','db4d9992897eda89b50f1d3208db607902da7e79c6f3bc6e6933cc5919068564'],
104
            ['password','35c246d5'],
105
            ['password','ycbTFtbcTZUqeJ/UuIWO1w=='],
106
            ['password','c9c6d316d6dc4d952a789fd4b8858ed7'],
107
            ['password','c9:c6:d3:16:d6:dc:4d:95:2a:78:9f:d4:b8:85:8e:d7'],
108
        );
109
    }
110
}
111