1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Emarref\Jwt\Verification; |
4
|
|
|
|
5
|
|
|
use Emarref\Jwt\Claim; |
6
|
|
|
|
7
|
|
View Code Duplication |
class SubjectVerifierTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\Emarref\Jwt\Token\Payload |
11
|
|
|
*/ |
12
|
|
|
private $payload; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\Emarref\Jwt\Token |
16
|
|
|
*/ |
17
|
|
|
private $token; |
18
|
|
|
|
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->payload = $this->getMockBuilder('Emarref\Jwt\Token\Payload')->getMock(); |
22
|
|
|
|
23
|
|
|
$this->token = $this->getMockBuilder('Emarref\Jwt\Token')->getMock(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @expectedException \InvalidArgumentException |
28
|
|
|
* @expectedExceptionMessage Cannot verify invalid subject value. |
29
|
|
|
*/ |
30
|
|
|
public function testInvalidSubject() |
31
|
|
|
{ |
32
|
|
|
new SubjectVerifier(new \stdClass()); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testNoSubject() |
36
|
|
|
{ |
37
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
38
|
|
|
->method('getPayload') |
39
|
|
|
->will($this->returnValue($this->payload)); |
40
|
|
|
|
41
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
42
|
|
|
->method('findClaimByName') |
43
|
|
|
->with(Claim\Subject::NAME) |
44
|
|
|
->will($this->returnValue(null)); |
45
|
|
|
|
46
|
|
|
$verifier = new SubjectVerifier(); |
47
|
|
|
$verifier->verify($this->token); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @expectedException \Emarref\Jwt\Exception\InvalidSubjectException |
52
|
|
|
* @expectedExceptionMessage Subject is invalid. |
53
|
|
|
*/ |
54
|
|
|
public function testSubjectInPayloadOnly() |
55
|
|
|
{ |
56
|
|
|
$subjectClaim = $this->getMockBuilder('Emarref\Jwt\Claim\Subject') |
57
|
|
|
->getMock(); |
58
|
|
|
|
59
|
|
|
$subjectClaim->expects($this->once()) |
60
|
|
|
->method('getValue') |
61
|
|
|
->will($this->returnValue('a_subject')); |
62
|
|
|
|
63
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
64
|
|
|
->method('getPayload') |
65
|
|
|
->will($this->returnValue($this->payload)); |
66
|
|
|
|
67
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
68
|
|
|
->method('findClaimByName') |
69
|
|
|
->with(Claim\Subject::NAME) |
70
|
|
|
->will($this->returnValue($subjectClaim)); |
71
|
|
|
|
72
|
|
|
$verifier = new SubjectVerifier(); |
73
|
|
|
$verifier->verify($this->token); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @expectedException \Emarref\Jwt\Exception\InvalidSubjectException |
78
|
|
|
* @expectedExceptionMessage Subject is invalid. |
79
|
|
|
*/ |
80
|
|
|
public function testSubjectInContextOnly() |
81
|
|
|
{ |
82
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
83
|
|
|
->method('getPayload') |
84
|
|
|
->will($this->returnValue($this->payload)); |
85
|
|
|
|
86
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
87
|
|
|
->method('findClaimByName') |
88
|
|
|
->with(Claim\Subject::NAME) |
89
|
|
|
->will($this->returnValue(null)); |
90
|
|
|
|
91
|
|
|
$verifier = new SubjectVerifier('a_subject'); |
92
|
|
|
$verifier->verify($this->token); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @expectedException \Emarref\Jwt\Exception\InvalidSubjectException |
97
|
|
|
* @expectedExceptionMessage Subject is invalid. |
98
|
|
|
*/ |
99
|
|
|
public function testSubjectMismatch() |
100
|
|
|
{ |
101
|
|
|
$subjectClaim = $this->getMockBuilder('Emarref\Jwt\Claim\Subject') |
102
|
|
|
->getMock(); |
103
|
|
|
|
104
|
|
|
$subjectClaim->expects($this->once()) |
105
|
|
|
->method('getValue') |
106
|
|
|
->will($this->returnValue('a_subject')); |
107
|
|
|
|
108
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
109
|
|
|
->method('getPayload') |
110
|
|
|
->will($this->returnValue($this->payload)); |
111
|
|
|
|
112
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
113
|
|
|
->method('findClaimByName') |
114
|
|
|
->with(Claim\Subject::NAME) |
115
|
|
|
->will($this->returnValue($subjectClaim)); |
116
|
|
|
|
117
|
|
|
$verifier = new SubjectVerifier('some_other_subject'); |
118
|
|
|
$verifier->verify($this->token); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testSuccess() |
122
|
|
|
{ |
123
|
|
|
$subjectClaim = $this->getMockBuilder('Emarref\Jwt\Claim\Subject') |
124
|
|
|
->getMock(); |
125
|
|
|
|
126
|
|
|
$subjectClaim->expects($this->once()) |
127
|
|
|
->method('getValue') |
128
|
|
|
->will($this->returnValue('a_subject')); |
129
|
|
|
|
130
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
131
|
|
|
->method('getPayload') |
132
|
|
|
->will($this->returnValue($this->payload)); |
133
|
|
|
|
134
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
135
|
|
|
->method('findClaimByName') |
136
|
|
|
->with(Claim\Subject::NAME) |
137
|
|
|
->will($this->returnValue($subjectClaim)); |
138
|
|
|
|
139
|
|
|
$verifier = new SubjectVerifier('a_subject'); |
140
|
|
|
$verifier->verify($this->token); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.