1 | <?php |
||
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() |
||
41 | |||
42 | public function testDeserialize() |
||
115 | |||
116 | |||
117 | /** |
||
118 | * @expectedException \InvalidArgumentException |
||
119 | * @expectedExceptionMessage Not a valid JWT string passed for deserialization |
||
120 | */ |
||
121 | public function testDeserializationWithEmptyToken() |
||
122 | { |
||
123 | $token = ''; |
||
124 | $this->serializer->deserialize($token); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @expectedException \InvalidArgumentException |
||
129 | * @expectedExceptionMessage Not a valid header of JWT string passed for deserialization |
||
130 | */ |
||
131 | public function testDeserializationTokenWithInvalidHeader() |
||
132 | { |
||
133 | $token = 'header.payload.signature'; |
||
134 | $this->encoding->expects($this->at(0)) |
||
135 | ->method('decode') |
||
136 | ->with('header') |
||
137 | ->will($this->returnValue('{"invalid"}')); |
||
138 | |||
139 | $this->serializer->deserialize($token); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @expectedException \InvalidArgumentException |
||
144 | * @expectedExceptionMessage Not a valid payload of JWT string passed for deserialization |
||
145 | */ |
||
146 | public function testDeserializationTokenWithInvalidPayload() |
||
147 | { |
||
148 | $token = 'header.payload.signature'; |
||
149 | $this->encoding->expects($this->at(0)) |
||
150 | ->method('decode') |
||
151 | ->with('header') |
||
152 | ->will($this->returnValue('{"header_field":"valid_header"}')); |
||
153 | |||
154 | $this->encoding->expects($this->at(1)) |
||
155 | ->method('decode') |
||
156 | ->with('payload') |
||
157 | ->will($this->returnValue('{"invalid"}')); |
||
158 | |||
159 | $this->encoding->expects($this->at(2)) |
||
160 | ->method('decode') |
||
161 | ->with('signature') |
||
162 | ->will($this->returnValue('{"signature_field":"valid_signature"}')); |
||
163 | |||
164 | $headerParameter = $this->getMockBuilder('Emarref\Jwt\HeaderParameter\Custom') |
||
165 | ->getMock(); |
||
166 | |||
167 | $this->headerParameterFactory->expects($this->once()) |
||
168 | ->method('get') |
||
169 | ->with('header_field') |
||
170 | ->will($this->returnValue($headerParameter)); |
||
171 | |||
172 | $this->serializer->deserialize($token); |
||
173 | } |
||
174 | |||
175 | public function testDeserializationTokenWithoutSignature() |
||
176 | { |
||
177 | $token = 'header.payload'; |
||
178 | $this->encoding->expects($this->at(0)) |
||
179 | ->method('decode') |
||
180 | ->with('header') |
||
181 | ->will($this->returnValue('{"header_field":"valid_header"}')); |
||
182 | |||
183 | $this->encoding->expects($this->at(1)) |
||
184 | ->method('decode') |
||
185 | ->with('payload') |
||
186 | ->will($this->returnValue('{}')); |
||
187 | |||
188 | $this->encoding->expects($this->at(2)) |
||
189 | ->method('decode') |
||
190 | ->with(null) |
||
191 | ->will($this->returnValue(null)); |
||
192 | |||
193 | $headerParameter = $this->getMockBuilder('Emarref\Jwt\HeaderParameter\Custom') |
||
194 | ->getMock(); |
||
195 | |||
196 | $this->headerParameterFactory->expects($this->once()) |
||
197 | ->method('get') |
||
198 | ->with('header_field') |
||
199 | ->will($this->returnValue($headerParameter)); |
||
200 | |||
201 | $token = $this->serializer->deserialize($token); |
||
202 | |||
203 | $this->assertNull($token->getSignature()); |
||
204 | } |
||
205 | |||
206 | public function testSerialize() |
||
266 | } |
||
267 |