Passed
Push — master ( 5f2302...77d6d7 )
by Ion
02:04
created

JtwTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testJwtException() 0 5 1
A testGenerateToken() 0 5 1
A testJwtExceptionExpired() 0 9 1
A testJwtExceptionInvalidSignature() 0 11 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 JtwTest 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