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

TokenTest::testCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

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