public function testDeserialize() { $jwt = 'a.b.c'; // Configure encoding $this->encoding->expects($this->at(0))->method('decode')->with('a')->will($this->returnValue('{"a":"1"}')); $this->encoding->expects($this->at(1))->method('decode')->with('b')->will($this->returnValue('{"b":"2"}')); $this->encoding->expects($this->at(2))->method('decode')->with('c')->will($this->returnValue('c')); // Configure headers $headerParameter = $this->getMockBuilder('Emarref\\Jwt\\HeaderParameter\\Custom')->getMock(); $headerParameter->expects($this->once())->method('setValue')->with('1'); $headerParameter->expects($this->once())->method('getValue')->will($this->returnValue('1')); $headerParameter->expects($this->exactly(2))->method('getName')->will($this->returnValue('a')); $this->headerParameterFactory->expects($this->once())->method('get')->with('a')->will($this->returnValue($headerParameter)); // Configure claims $claim = $this->getMockBuilder('Emarref\\Jwt\\Claim\\PrivateClaim')->getMock(); $claim->expects($this->once())->method('setValue')->with('2'); $claim->expects($this->once())->method('getValue')->will($this->returnValue('2')); $claim->expects($this->exactly(2))->method('getName')->will($this->returnValue('b')); $this->claimFactory->expects($this->once())->method('get')->with('b')->will($this->returnValue($claim)); // Assert $token = $this->serializer->deserialize($jwt); $this->assertSame('{"a":"1"}', $token->getHeader()->jsonSerialize()); $this->assertSame('{"b":"2"}', $token->getPayload()->jsonSerialize()); $this->assertSame('c', $token->getSignature()); }
public function testDeserializationTokenWithoutSignature() { $token = 'header.payload'; $this->encoding->expects($this->at(0))->method('decode')->with('header')->will($this->returnValue('{"header_field":"valid_header"}')); $this->encoding->expects($this->at(1))->method('decode')->with('payload')->will($this->returnValue('{}')); $this->encoding->expects($this->at(2))->method('decode')->with(null)->will($this->returnValue(null)); $headerParameter = $this->getMockBuilder('Emarref\\Jwt\\HeaderParameter\\Custom')->getMock(); $this->headerParameterFactory->expects($this->once())->method('get')->with('header_field')->will($this->returnValue($headerParameter)); $token = $this->serializer->deserialize($token); $this->assertNull($token->getSignature()); }
/** * @param string $jwt * @return Token */ public function deserialize($jwt) { $serialization = new Serialization\Compact($this->encoder, new HeaderParameter\Factory(), new Claim\Factory()); return $serialization->deserialize($jwt); }