JwtTest::testJwtExceptionInvalidSignature()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
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