1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Authenticate; |
4
|
|
|
|
5
|
|
|
use Ds\Authenticate\Token; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class TokenTest |
9
|
|
|
* |
10
|
|
|
* @package Tests\Authenticate |
11
|
|
|
*/ |
12
|
|
|
class TokenTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Token |
16
|
|
|
*/ |
17
|
|
|
public $token; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
protected function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->token = new Token(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Test token randomness. |
29
|
|
|
*/ |
30
|
|
|
public function testTokenRandomness() |
31
|
|
|
{ |
32
|
|
|
$tokens = []; |
33
|
|
|
for ($generateToken = 0; $generateToken < 100; $generateToken++) { |
34
|
|
|
$tokens[] = $this->token->create(40); |
35
|
|
|
} |
36
|
|
|
$duplicates = array_diff_assoc($tokens, array_unique($tokens)); |
37
|
|
|
$this->assertEquals($duplicates, array()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
public function testCreateFromString() |
44
|
|
|
{ |
45
|
|
|
$string = 'foobar'; |
46
|
|
|
$key = 'a-private-key'; |
47
|
|
|
$algo = 'sha256'; |
48
|
|
|
$expected = bin2hex(hash_hmac($algo, $string, $key)); |
49
|
|
|
|
50
|
|
|
$actual = $this->token->createFromString($string, $key, $algo); |
51
|
|
|
$this->assertEquals($expected, $actual); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $length |
56
|
|
|
* @dataProvider tokenLengthProvider |
57
|
|
|
*/ |
58
|
|
|
public function testCreate($length) |
59
|
|
|
{ |
60
|
|
|
$token = $this->token->create($length); |
61
|
|
|
$this->assertEquals(strlen($token), $length); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function tokenLengthProvider() |
68
|
|
|
{ |
69
|
|
|
return array([40], [80], [100], [150], [130], [150], [200], [300]); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|