|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Emarref\Jwt\Serialization; |
|
4
|
|
|
|
|
5
|
|
|
use Emarref\Jwt\Claim; |
|
6
|
|
|
use Emarref\Jwt\HeaderParameter; |
|
7
|
|
|
use Emarref\Jwt\Token; |
|
8
|
|
|
|
|
9
|
|
|
class CompactTest extends \PHPUnit_Framework_TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
13
|
|
|
*/ |
|
14
|
|
|
private $encoding; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
18
|
|
|
*/ |
|
19
|
|
|
private $headerParameterFactory; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
23
|
|
|
*/ |
|
24
|
|
|
private $claimFactory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Compact |
|
28
|
|
|
*/ |
|
29
|
|
|
private $serializer; |
|
30
|
|
|
|
|
31
|
|
|
public function setUp() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->encoding = $this->getMockBuilder('Emarref\Jwt\Encoding\Base64')->getMock(); |
|
34
|
|
|
|
|
35
|
|
|
$this->headerParameterFactory = $this->getMockBuilder('Emarref\Jwt\HeaderParameter\Factory')->getMock(); |
|
36
|
|
|
|
|
37
|
|
|
$this->claimFactory = $this->getMockBuilder('Emarref\Jwt\Claim\Factory')->getMock(); |
|
38
|
|
|
|
|
39
|
|
|
$this->serializer = new Compact($this->encoding, $this->headerParameterFactory, $this->claimFactory); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testDeserialize() |
|
43
|
|
|
{ |
|
44
|
|
|
$jwt = 'a.b.c'; |
|
45
|
|
|
|
|
46
|
|
|
// Configure encoding |
|
47
|
|
|
|
|
48
|
|
|
$this->encoding->expects($this->at(0)) |
|
49
|
|
|
->method('decode') |
|
50
|
|
|
->with('a') |
|
51
|
|
|
->will($this->returnValue('{"a":"1"}')); |
|
52
|
|
|
|
|
53
|
|
|
$this->encoding->expects($this->at(1)) |
|
54
|
|
|
->method('decode') |
|
55
|
|
|
->with('b') |
|
56
|
|
|
->will($this->returnValue('{"b":"2"}')); |
|
57
|
|
|
|
|
58
|
|
|
$this->encoding->expects($this->at(2)) |
|
59
|
|
|
->method('decode') |
|
60
|
|
|
->with('c') |
|
61
|
|
|
->will($this->returnValue('c')); |
|
62
|
|
|
|
|
63
|
|
|
// Configure headers |
|
64
|
|
|
|
|
65
|
|
|
$headerParameter = $this->getMockBuilder('Emarref\Jwt\HeaderParameter\Custom') |
|
66
|
|
|
->getMock(); |
|
67
|
|
|
|
|
68
|
|
|
$headerParameter->expects($this->once()) |
|
69
|
|
|
->method('setValue') |
|
70
|
|
|
->with('1'); |
|
71
|
|
|
|
|
72
|
|
|
$headerParameter->expects($this->once()) |
|
73
|
|
|
->method('getValue') |
|
74
|
|
|
->will($this->returnValue('1')); |
|
75
|
|
|
|
|
76
|
|
|
$headerParameter->expects($this->exactly(2)) |
|
77
|
|
|
->method('getName') |
|
78
|
|
|
->will($this->returnValue('a')); |
|
79
|
|
|
|
|
80
|
|
|
$this->headerParameterFactory->expects($this->once()) |
|
81
|
|
|
->method('get') |
|
82
|
|
|
->with('a') |
|
83
|
|
|
->will($this->returnValue($headerParameter)); |
|
84
|
|
|
|
|
85
|
|
|
// Configure claims |
|
86
|
|
|
|
|
87
|
|
|
$claim = $this->getMockBuilder('Emarref\Jwt\Claim\PrivateClaim') |
|
88
|
|
|
->getMock(); |
|
89
|
|
|
|
|
90
|
|
|
$claim->expects($this->once()) |
|
91
|
|
|
->method('setValue') |
|
92
|
|
|
->with('2'); |
|
93
|
|
|
|
|
94
|
|
|
$claim->expects($this->once()) |
|
95
|
|
|
->method('getValue') |
|
96
|
|
|
->will($this->returnValue('2')); |
|
97
|
|
|
|
|
98
|
|
|
$claim->expects($this->exactly(2)) |
|
99
|
|
|
->method('getName') |
|
100
|
|
|
->will($this->returnValue('b')); |
|
101
|
|
|
|
|
102
|
|
|
$this->claimFactory->expects($this->once()) |
|
103
|
|
|
->method('get') |
|
104
|
|
|
->with('b') |
|
105
|
|
|
->will($this->returnValue($claim)); |
|
106
|
|
|
|
|
107
|
|
|
// Assert |
|
108
|
|
|
|
|
109
|
|
|
$token = $this->serializer->deserialize($jwt); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertSame('{"a":"1"}', $token->getHeader()->jsonSerialize()); |
|
112
|
|
|
$this->assertSame('{"b":"2"}', $token->getPayload()->jsonSerialize()); |
|
113
|
|
|
$this->assertSame('c', $token->getSignature()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function testSerialize() |
|
117
|
|
|
{ |
|
118
|
|
|
// Configure payload |
|
119
|
|
|
|
|
120
|
|
|
$headerParameters = $this->getMockBuilder('Emarref\Jwt\Token\PropertyList')->getMock(); |
|
121
|
|
|
|
|
122
|
|
|
$headerParameters->expects($this->once()) |
|
123
|
|
|
->method('jsonSerialize') |
|
124
|
|
|
->will($this->returnValue('{"a":"1"}')); |
|
125
|
|
|
|
|
126
|
|
|
$header = $this->getMockBuilder('Emarref\Jwt\Token\Header')->getMock(); |
|
127
|
|
|
|
|
128
|
|
|
$header->expects($this->once()) |
|
129
|
|
|
->method('getParameters') |
|
130
|
|
|
->will($this->returnValue($headerParameters)); |
|
131
|
|
|
|
|
132
|
|
|
// Configure payload |
|
133
|
|
|
|
|
134
|
|
|
$claims = $this->getMockBuilder('Emarref\Jwt\Token\PropertyList')->getMock(); |
|
135
|
|
|
|
|
136
|
|
|
$claims->expects($this->once()) |
|
137
|
|
|
->method('jsonSerialize') |
|
138
|
|
|
->will($this->returnValue('{"b":"2"}')); |
|
139
|
|
|
|
|
140
|
|
|
$payload = $this->getMockBuilder('Emarref\Jwt\Token\Payload')->getMock(); |
|
141
|
|
|
|
|
142
|
|
|
$payload->expects($this->once()) |
|
143
|
|
|
->method('getClaims') |
|
144
|
|
|
->will($this->returnValue($claims)); |
|
145
|
|
|
|
|
146
|
|
|
// Configure token |
|
147
|
|
|
|
|
148
|
|
|
$token = $this->getMockBuilder('Emarref\Jwt\Token')->getMock(); |
|
149
|
|
|
|
|
150
|
|
|
$token->expects($this->once()) |
|
151
|
|
|
->method('getHeader') |
|
152
|
|
|
->will($this->returnValue($header)); |
|
153
|
|
|
|
|
154
|
|
|
$token->expects($this->once()) |
|
155
|
|
|
->method('getPayload') |
|
156
|
|
|
->will($this->returnValue($payload)); |
|
157
|
|
|
|
|
158
|
|
|
$token->expects($this->once()) |
|
159
|
|
|
->method('getSignature') |
|
160
|
|
|
->will($this->returnValue('c')); |
|
161
|
|
|
|
|
162
|
|
|
// Configure encoding |
|
163
|
|
|
|
|
164
|
|
|
$this->encoding->expects($this->exactly(3)) |
|
165
|
|
|
->method('encode') |
|
166
|
|
|
->will($this->returnValueMap([ |
|
167
|
|
|
['{"a":"1"}', 'a'], |
|
168
|
|
|
['{"b":"2"}', 'b'], |
|
169
|
|
|
['c', 'c'], |
|
170
|
|
|
])); |
|
171
|
|
|
|
|
172
|
|
|
$jwt = $this->serializer->serialize($token); |
|
173
|
|
|
|
|
174
|
|
|
$this->assertSame('a.b.c', $jwt); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|