Test Failed
Push — master ( fc366b...a702cc )
by Dan
03:41
created

HashHmacTest::testWithPrivateKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Tests\Authenticate;
4
5
use Ds\Authenticate\HashHmac;
6
7
class HashHmacTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @var HashHmac
11
     */
12
    public $auth;
13
    
14
    public $reflector;
15
16
    public $algo;
17
18
    public $data;
19
20
    /**
21
     * @var HashHmac
22
     */
23
    public $hmac;
24
25
    protected function setUp()
26
    {
27
28
        $this->auth = new HashHmac('privateKey');
29
        $this->reflector = new \ReflectionClass(HashHmac::class);
30
31
        $this->algo = 'sha512';
32
        $this->data = ['foo' => 'bar'];
33
        $this->privateKey = 'privateKey';
0 ignored issues
show
Bug introduced by
The property privateKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
35
        $this->hmac = new HashHmac($this->privateKey,$this->algo);
36
37
    }
38
39 View Code Duplication
    public function testHash()
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...
40
    {
41
        $actual = $this->hmac->create($this->data);
42
        $expected = hash_hmac($this->algo, json_encode($this->data) , $this->privateKey);
43
        $result = $this->hmac->verify($actual, $expected);
0 ignored issues
show
Documentation introduced by
$expected is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
        $this->assertEquals(true, $result);
45
    }
46
47
    public function testCompareHash()
48
    {
49
        $encodedHash = $this->hmac->create($this->data);
50
        $this->assertEquals(true, $this->auth->verify($encodedHash,$this->data));
51
    }
52
53
    public function testCompareHashInvalidAlgo()
54
    {
55
        $this->setExpectedException('Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
56
        $hash = 'shafoo2=647a6cbf9b30cb8cc3fd5147a8fd30a19775fe3290e3dd0e729ae2f1e8a485f833469df616374fa923fcc3533545a11e6deba25a63bcd1ec81c316c090890ddc=10f3e8ed9c5c8b33cadda267882fa6449f5af465bc1dc420=1457010889';
57
        $encoded = base64_encode($hash);
58
        $this->auth->compareHash($encoded, $this->data);
0 ignored issues
show
Bug introduced by
The method compareHash() does not seem to exist on object<Ds\Authenticate\HashHmac>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
    }
60
61
    public function testCompareHashNonMatch()
62
    {
63
64
        $this->setExpectedException('Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
65
        $encodedHash = $this->hmac->getEncodedHash($this->data);
0 ignored issues
show
Bug introduced by
The method getEncodedHash() does not seem to exist on object<Ds\Authenticate\HashHmac>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        $differentPayload = ['foos' => 'bat'];
67
        $this->auth->compareHash($encodedHash, $differentPayload);
0 ignored issues
show
Bug introduced by
The method compareHash() does not seem to exist on object<Ds\Authenticate\HashHmac>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
    }
69
70 View Code Duplication
    public function testVerify()
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...
71
    {
72
        $hash1 = hash_hmac($this->algo, json_encode($this->data), $this->privateKey);
73
        $expected = true;
74
        $actual = $this->hmac->verify($hash1, $this->data);
75
        $this->assertEquals($expected, $actual);
76
    }
77
78 View Code Duplication
    public function testVerifyHashWrongKey()
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...
79
    {
80
81
        $this->setExpectedException('Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
82
        $hash1 = hash_hmac($this->algo, json_encode($this->data), 'key');
83
        $hash2 = hash_hmac($this->algo, json_encode($this->data), 'key2');
84
        $this->hmac->verify($hash1, $hash2);
0 ignored issues
show
Documentation introduced by
$hash2 is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
    }
86
87
    public function testVerifyHashNotString()
88
    {
89
        $this->setExpectedException('Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
90
        $hash1 = 23809.123;
91
        $hash2 = 23809.123;
92
        $this->hmac->verify($hash1, $hash2);
0 ignored issues
show
Documentation introduced by
$hash2 is of type double, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
    }
94
95 View Code Duplication
    public function testVerifyHashWrongData()
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...
96
    {
97
        $this->setExpectedException('Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
98
        $data = md5('someOtherStuff');
99
        $hash1 = hash_hmac($this->algo, json_encode($this->data), 'key');
100
        $hash2 = hash_hmac($this->algo, $data, 'key');
101
        $this->hmac->verify($hash1, $hash2);
0 ignored issues
show
Documentation introduced by
$hash2 is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
    }
103
104
    public function testGetPrivateKey()
105
    {
106
        $expected = 'privateKey';
107
        $actual = $this->auth->getPrivateKey();
108
        $this->assertEquals($expected, $actual);
109
    }
110
111
    public function testWithPrivateKey()
112
    {
113
        $expected = 'newKey';
114
        $newKey = $this->auth->withPrivateKey($expected);
115
        $this->assertEquals($expected, $newKey->getPrivateKey());
116
    }
117
118
    public function testGetAlgorithm()
119
    {
120
        $expected = 'sha512';
121
        $actual = $this->auth->getAlgorithm();
122
        $this->assertEquals($expected, $actual);
123
    }
124
125
    public function testVerifyAlgorithm()
126
    {
127
        $method = $this->reflector->getMethod('verifyAlgorithm');
128
        $method->setAccessible(true);
129
        $actual = $method->invokeArgs($this->auth, ['sha256']);
130
        $this->assertEquals($actual, 'sha256');
131
    }
132
133
    public function testVerifyAlgorithmException()
134
    {
135
        $this->setExpectedException('Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
136
137
        $method = $this->reflector->getMethod('verifyAlgorithm');
138
        $method->setAccessible(true);
139
        $method->invokeArgs($this->auth, ['sha111']);
140
    }
141
142
}
143