TokenGeneratorTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 91
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testWillRejectInvalidExpirationTime() 0 16 2
A invalidExpirationTimeProvider() 0 10 1
A testWillGenerateAValidJWTToken() 0 23 1
A validExpirationTimeProvider() 0 7 1
A testWillFailIfTheSessionAttributeIsNotASession() 0 17 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PSR7CsrfTest;
6
7
use Lcobucci\JWT\Signer;
8
use Lcobucci\JWT\Signer\Hmac\Sha256;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Http\Message\ServerRequestInterface;
11
use PSR7Csrf\Exception\InvalidExpirationTimeException;
12
use PSR7Csrf\Exception\SessionAttributeNotFoundException;
13
use PSR7Csrf\Session\ExtractUniqueKeyFromSessionInterface;
14
use PSR7Csrf\TokenGenerator;
15
use PSR7Sessions\Storageless\Session\SessionInterface;
16
use stdClass;
17
18
/**
19
 * @covers \PSR7Csrf\TokenGenerator
20
 */
21
final class TokenGeneratorTest extends TestCase
22
{
23
    /**
24
     * @dataProvider invalidExpirationTimeProvider
25
     *
26
     * @param int  $invalidExpirationTime
27
     * @param bool $valid
28
     */
29
    public function testWillRejectInvalidExpirationTime(int $invalidExpirationTime, bool $valid)
30
    {
31
        /* @var $signer Signer */
32
        $signer                      = $this->createMock(Signer::class);
33
        /* @var $extractUniqueKeyFromSession ExtractUniqueKeyFromSessionInterface */
34
        $extractUniqueKeyFromSession = $this->createMock(ExtractUniqueKeyFromSessionInterface::class);
35
36
        if (! $valid) {
37
            $this->expectException(InvalidExpirationTimeException::class);
38
        }
39
40
        self::assertInstanceOf(
41
            TokenGenerator::class,
42
            new TokenGenerator($signer, $extractUniqueKeyFromSession, $invalidExpirationTime, 'session')
43
        );
44
    }
45
46
    public function invalidExpirationTimeProvider() : array
47
    {
48
        return [
49
            [100, true],
50
            [1, true],
51
            [0, false],
52
            [-1, false],
53
            [-200, false],
54
        ];
55
    }
56
57
    /**
58
     * @dataProvider validExpirationTimeProvider
59
     *
60
     * @param int $validExpirationTime
61
     */
62
    public function testWillGenerateAValidJWTToken(int $validExpirationTime)
63
    {
64
        $signer = new Sha256();
65
        /* @var $extractUniqueKeyFromSession ExtractUniqueKeyFromSessionInterface|\PHPUnit_Framework_MockObject_MockObject */
66
        $extractUniqueKeyFromSession = $this->createMock(ExtractUniqueKeyFromSessionInterface::class);
67
        /* @var $session SessionInterface */
68
        $session = $this->createMock(SessionInterface::class);
69
        /* @var $request ServerRequestInterface|\PHPUnit_Framework_MockObject_MockObject */
70
        $request = $this->createMock(ServerRequestInterface::class);
71
        $sessionAttribute = uniqid('session', true);
72
73
        $generator = new TokenGenerator($signer, $extractUniqueKeyFromSession, $validExpirationTime, $sessionAttribute);
0 ignored issues
show
Bug introduced by
It seems like $extractUniqueKeyFromSession defined by $this->createMock(\PSR7C...essionInterface::class) on line 66 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PSR7Csrf\TokenGenerator::__construct() does only seem to accept object<PSR7Csrf\Session\...eyFromSessionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74
        $secretKey = uniqid('secretKey', true);
75
76
        $request->expects(self::any())->method('getAttribute')->with($sessionAttribute)->willReturn($session);
77
        $extractUniqueKeyFromSession->expects(self::any())->method('__invoke')->with($session)->willReturn($secretKey);
78
79
        $token = $generator->__invoke($request);
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->createMock(\Psr\H...equestInterface::class) on line 70 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PSR7Csrf\TokenGenerator::__invoke() does only seem to accept object<Psr\Http\Message\ServerRequestInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
80
81
        self::assertTrue($token->verify($signer, $secretKey));
82
        self::assertLessThanOrEqual(time(), $token->getClaim('iat'));
83
        self::assertGreaterThan(time(), $token->getClaim('exp'));
84
    }
85
86
    public function validExpirationTimeProvider() : array
87
    {
88
        return [
89
            [10],
90
            [100],
91
        ];
92
    }
93
94
    public function testWillFailIfTheSessionAttributeIsNotASession()
95
    {
96
        /* @var $extractUniqueKeyFromSession ExtractUniqueKeyFromSessionInterface|\PHPUnit_Framework_MockObject_MockObject */
97
        $extractUniqueKeyFromSession = $this->createMock(ExtractUniqueKeyFromSessionInterface::class);
98
        /* @var $request ServerRequestInterface|\PHPUnit_Framework_MockObject_MockObject */
99
        $request = $this->createMock(ServerRequestInterface::class);
100
        $sessionAttribute = uniqid('session', true);
101
102
        $generator = new TokenGenerator(new Sha256(), $extractUniqueKeyFromSession, 10, $sessionAttribute);
0 ignored issues
show
Bug introduced by
It seems like $extractUniqueKeyFromSession defined by $this->createMock(\PSR7C...essionInterface::class) on line 97 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PSR7Csrf\TokenGenerator::__construct() does only seem to accept object<PSR7Csrf\Session\...eyFromSessionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
103
104
        $request->expects(self::any())->method('getAttribute')->with($sessionAttribute)->willReturn(new stdClass());
105
        $request->expects(self::any())->method('getAttributes')->willReturn([]);
106
107
        $this->expectException(SessionAttributeNotFoundException::class);
108
109
        $generator->__invoke($request);
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->createMock(\Psr\H...equestInterface::class) on line 99 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PSR7Csrf\TokenGenerator::__invoke() does only seem to accept object<Psr\Http\Message\ServerRequestInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
110
    }
111
}
112