1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Emarref\Jwt\Verification; |
4
|
|
|
|
5
|
|
|
class ExpirationVerifierTest extends \PHPUnit_Framework_TestCase |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\Emarref\Jwt\Token\Payload |
9
|
|
|
*/ |
10
|
|
|
private $payload; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\Emarref\Jwt\Token |
14
|
|
|
*/ |
15
|
|
|
private $token; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var ExpirationVerifier |
19
|
|
|
*/ |
20
|
|
|
private $verifier; |
21
|
|
|
|
22
|
|
View Code Duplication |
public function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->payload = $this->getMockBuilder('Emarref\Jwt\Token\Payload')->getMock(); |
25
|
|
|
|
26
|
|
|
$this->token = $this->getMockBuilder('Emarref\Jwt\Token')->getMock(); |
27
|
|
|
|
28
|
|
|
$this->token->expects($this->any()) |
29
|
|
|
->method('getPayload') |
30
|
|
|
->will($this->returnValue($this->payload)); |
31
|
|
|
|
32
|
|
|
$this->verifier = new ExpirationVerifier(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
public function testMissingExpiry() |
36
|
|
|
{ |
37
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
38
|
|
|
->method('findClaimByName') |
39
|
|
|
->will($this->returnValue(null)); |
40
|
|
|
|
41
|
|
|
$this->verifier->verify($this->token); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @expectedException \Emarref\Jwt\Exception\ExpiredException |
46
|
|
|
* @expectedExceptionMessage Token expired at "Sat, 08 Nov 2014 00:00:00 +0000" |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function testExpired() |
49
|
|
|
{ |
50
|
|
|
$dateTime = new \DateTime('Sat, 08 Nov 2014 00:00:00 +0000'); |
51
|
|
|
|
52
|
|
|
$expirationClaim = $this->getMockBuilder('Emarref\Jwt\Claim\Expiration')->getMock(); |
53
|
|
|
|
54
|
|
|
$expirationClaim->expects($this->exactly(3)) |
55
|
|
|
->method('getValue') |
56
|
|
|
->will($this->returnValue($dateTime->getTimestamp())); |
57
|
|
|
|
58
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
59
|
|
|
->method('findClaimByName') |
60
|
|
|
->will($this->returnValue($expirationClaim)); |
61
|
|
|
|
62
|
|
|
$this->verifier->verify($this->token); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @expectedException \InvalidArgumentException |
67
|
|
|
* @expectedExceptionMessage Invalid expiration timestamp "foobar" |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function testUnexpectedValue() |
70
|
|
|
{ |
71
|
|
|
$expirationClaim = $this->getMockBuilder('Emarref\Jwt\Claim\Expiration')->getMock(); |
72
|
|
|
|
73
|
|
|
$expirationClaim->expects($this->exactly(3)) |
74
|
|
|
->method('getValue') |
75
|
|
|
->will($this->returnValue('foobar')); |
76
|
|
|
|
77
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
78
|
|
|
->method('findClaimByName') |
79
|
|
|
->will($this->returnValue($expirationClaim)); |
80
|
|
|
|
81
|
|
|
$this->verifier->verify($this->token); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
public function testValid() |
85
|
|
|
{ |
86
|
|
|
$future = new \DateTime('5 minutes', new \DateTimeZone('UTC')); |
87
|
|
|
|
88
|
|
|
$expirationClaim = $this->getMockBuilder('Emarref\Jwt\Claim\Expiration')->getMock(); |
89
|
|
|
|
90
|
|
|
$expirationClaim->expects($this->once()) |
91
|
|
|
->method('getValue') |
92
|
|
|
->will($this->returnValue($future->getTimestamp())); |
93
|
|
|
|
94
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
95
|
|
|
->method('findClaimByName') |
96
|
|
|
->will($this->returnValue($expirationClaim)); |
97
|
|
|
|
98
|
|
|
$this->verifier->verify($this->token); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: