JwtTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 58
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testJwtExceptionInvalidSignature() 0 11 1
A testJwtException() 0 5 1
A testJwtExceptionExpired() 0 9 1
A testGenerateToken() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace IonGhitun\JwtToken\Tests;
5
6
use Carbon\Carbon;
7
use IonGhitun\JwtToken\Exceptions\JwtException;
8
use IonGhitun\JwtToken\Jwt;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * Class JtwTest
13
 *
14
 * @package IonGhitun\JwtToken\Tests
15
 */
16
class JwtTest extends TestCase
17
{
18
    /**
19
     * Test generate and validate token
20
     *
21
     * @throws JwtException
22
     */
23
    public function testGenerateToken()
24
    {
25
        $token = Jwt::generateToken(['test' => 'phpunit']);
26
27
        $this->assertArrayHasKey('test', Jwt::validateToken($token));
28
    }
29
30
    /**
31
     * Test not valid jwt token
32
     *
33
     * @throws JwtException
34
     */
35
    public function testJwtException()
36
    {
37
        $this->expectException(JwtException::class);
38
39
        Jwt::validateToken('token');
40
    }
41
42
    /**
43
     * Test jwt token expired
44
     *
45
     * @throws JwtException
46
     */
47
    public function testJwtExceptionExpired()
48
    {
49
        $this->expectException(JwtException::class);
50
51
        $payload = ['test' => 'phpunit', 'expiration' => Carbon::now()->subDay()->format('Y-m-d H:i:s')];
52
53
        $token = Jwt::generateToken($payload);
54
55
        Jwt::validateToken($token);
56
    }
57
58
    /**
59
     * Test jwt token invalid signature
60
     *
61
     * @throws JwtException
62
     */
63
    public function testJwtExceptionInvalidSignature()
64
    {
65
        $this->expectException(JwtException::class);
66
67
        $payload = ['test' => 'phpunit', 'expiration' => Carbon::now()->addDay()->format('Y-m-d H:i:s')];
68
69
        $token = Jwt::generateToken($payload);
70
71
        $invalidTokenArray = explode('.', $token);
72
73
        Jwt::validateToken($invalidTokenArray[0].'.'.$invalidTokenArray[1].'.'.'signature');
74
    }
75
}
76