|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Emarref\Jwt\Signature; |
|
4
|
|
|
|
|
5
|
|
|
use Emarref\Jwt\HeaderParameter\Algorithm; |
|
6
|
|
|
|
|
7
|
|
|
class JwsTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
11
|
|
|
*/ |
|
12
|
|
|
private $algorithm; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
16
|
|
|
*/ |
|
17
|
|
|
private $encryption; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
21
|
|
|
*/ |
|
22
|
|
|
private $encoder; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Jws |
|
26
|
|
|
*/ |
|
27
|
|
|
private $signer; |
|
28
|
|
|
|
|
29
|
|
|
public function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->algorithm = $this->getMockBuilder('Emarref\Jwt\Algorithm\None')->getMock(); |
|
32
|
|
|
|
|
33
|
|
|
$this->encryption = $this->getMockBuilder('Emarref\Jwt\Encryption\Symmetric') |
|
34
|
|
|
->setConstructorArgs([$this->algorithm]) |
|
35
|
|
|
->getMock(); |
|
36
|
|
|
|
|
37
|
|
|
$this->encoder = $this->getMockBuilder('Emarref\Jwt\Encoding\Base64')->getMock(); |
|
38
|
|
|
|
|
39
|
|
|
$this->signer = new Jws($this->encryption, $this->encoder); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testSign() |
|
43
|
|
|
{ |
|
44
|
|
|
$expectedSignature = 'foobar'; |
|
45
|
|
|
|
|
46
|
|
|
// Configure payload |
|
47
|
|
|
|
|
48
|
|
|
$headerParameters = $this->getMockBuilder('Emarref\Jwt\Token\PropertyList')->getMock(); |
|
49
|
|
|
|
|
50
|
|
|
$headerParameters->expects($this->once()) |
|
51
|
|
|
->method('jsonSerialize'); |
|
52
|
|
|
|
|
53
|
|
|
$this->encoder->expects($this->at(0)) |
|
54
|
|
|
->method('encode'); |
|
55
|
|
|
|
|
56
|
|
|
$header = $this->getMockBuilder('Emarref\Jwt\Token\Header')->getMock(); |
|
57
|
|
|
|
|
58
|
|
|
$header->expects($this->once()) |
|
59
|
|
|
->method('getParameters') |
|
60
|
|
|
->will($this->returnValue($headerParameters)); |
|
61
|
|
|
|
|
62
|
|
|
// Configure payload |
|
63
|
|
|
|
|
64
|
|
|
$claims = $this->getMockBuilder('Emarref\Jwt\Token\PropertyList')->getMock(); |
|
65
|
|
|
|
|
66
|
|
|
$claims->expects($this->once()) |
|
67
|
|
|
->method('jsonSerialize'); |
|
68
|
|
|
|
|
69
|
|
|
$payload = $this->getMockBuilder('Emarref\Jwt\Token\Payload')->getMock(); |
|
70
|
|
|
|
|
71
|
|
|
$payload->expects($this->once()) |
|
72
|
|
|
->method('getClaims') |
|
73
|
|
|
->will($this->returnValue($claims)); |
|
74
|
|
|
|
|
75
|
|
|
$this->encoder->expects($this->at(1)) |
|
76
|
|
|
->method('encode'); |
|
77
|
|
|
|
|
78
|
|
|
// Configure token |
|
79
|
|
|
|
|
80
|
|
|
$token = $this->getMockBuilder('Emarref\Jwt\Token')->getMock(); |
|
81
|
|
|
|
|
82
|
|
|
$token->expects($this->once()) |
|
83
|
|
|
->method('getHeader') |
|
84
|
|
|
->will($this->returnValue($header)); |
|
85
|
|
|
|
|
86
|
|
|
$token->expects($this->once()) |
|
87
|
|
|
->method('getPayload') |
|
88
|
|
|
->will($this->returnValue($payload)); |
|
89
|
|
|
|
|
90
|
|
|
$this->encryption->expects($this->once()) |
|
91
|
|
|
->method('getAlgorithmName') |
|
92
|
|
|
->will($this->returnValue('alg')); |
|
93
|
|
|
|
|
94
|
|
|
$this->encryption->expects($this->once()) |
|
95
|
|
|
->method('encrypt') |
|
96
|
|
|
->will($this->returnValue($expectedSignature)); |
|
97
|
|
|
|
|
98
|
|
|
$token->expects($this->once()) |
|
99
|
|
|
->method('addHeader') |
|
100
|
|
|
->with(new Algorithm('alg')); |
|
101
|
|
|
|
|
102
|
|
|
$token->expects($this->once()) |
|
103
|
|
|
->method('setSignature') |
|
104
|
|
|
->with($expectedSignature); |
|
105
|
|
|
|
|
106
|
|
|
$this->signer->sign($token); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|