|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Fracture\Http\Headers; |
|
5
|
|
|
|
|
6
|
|
|
use Exception; |
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
use PHPUnit_Framework_TestCase; |
|
9
|
|
|
|
|
10
|
|
|
class SetCookieTest extends PHPUnit_Framework_TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @covers Fracture\Http\Headers\SetCookie::__construct |
|
15
|
|
|
* @covers Fracture\Http\Headers\SetCookie::isFinal |
|
16
|
|
|
*/ |
|
17
|
|
|
public function testIsFinalResponse() |
|
18
|
|
|
{ |
|
19
|
|
|
$instance = new SetCookie(null, null); |
|
20
|
|
|
$this->assertTrue($instance->isFinal()); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @covers Fracture\Http\Headers\SetCookie::__construct |
|
26
|
|
|
* @covers Fracture\Http\Headers\SetCookie::getName |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testHeaderName() |
|
29
|
|
|
{ |
|
30
|
|
|
$instance = new SetCookie(null, null); |
|
31
|
|
|
$this->assertSame('Set-Cookie', $instance->getName()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @covers Fracture\Http\Headers\SetCookie::prepare |
|
36
|
|
|
* @covers Fracture\Http\Headers\SetCookie::hasInvalidOptions |
|
37
|
|
|
* |
|
38
|
|
|
* @expectedException PHPUnit_Framework_Error_Warning |
|
39
|
|
|
*/ |
|
40
|
|
|
public function testInvalidOptions() |
|
41
|
|
|
{ |
|
42
|
|
|
$instance = new SetCookie(null, null, ['foo' => 'bar']); |
|
43
|
|
|
$instance->prepare(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @covers Fracture\Http\Headers\SetCookie::__construct |
|
49
|
|
|
* @covers Fracture\Http\Headers\SetCookie::prepare |
|
50
|
|
|
* @covers Fracture\Http\Headers\SetCookie::getValue |
|
51
|
|
|
* |
|
52
|
|
|
* @covers Fracture\Http\Headers\SetCookie::hasInvalidOptions |
|
53
|
|
|
* @covers Fracture\Http\Headers\SetCookie::cleanOptions |
|
54
|
|
|
* @covers Fracture\Http\Headers\SetCookie::hasInvalidOptions |
|
55
|
|
|
* @covers Fracture\Http\Headers\SetCookie::collectFormatedOptions |
|
56
|
|
|
* @covers Fracture\Http\Headers\SetCookie::collectExpireTime |
|
57
|
|
|
* @covers Fracture\Http\Headers\SetCookie::collectDomainPathValue |
|
58
|
|
|
* @covers Fracture\Http\Headers\SetCookie::collectBooleanOptions |
|
59
|
|
|
* |
|
60
|
|
|
* @dataProvider provideHeaderValue |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testHeaderValue($options, $expected) |
|
63
|
|
|
{ |
|
64
|
|
|
$instance = new SetCookie('alpha', 'beta', $options); |
|
65
|
|
|
$instance->prepare(); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertSame($expected, $instance->getValue()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
public function provideHeaderValue() |
|
72
|
|
|
{ |
|
73
|
|
|
return [ |
|
74
|
|
|
[ |
|
75
|
|
|
'options' => [], |
|
76
|
|
|
'expected' => 'alpha=beta; Path=/; HttpOnly', |
|
77
|
|
|
], |
|
78
|
|
|
[ |
|
79
|
|
|
'options' => ['httpOnly' => false], |
|
80
|
|
|
'expected' => 'alpha=beta; Path=/', |
|
81
|
|
|
], |
|
82
|
|
|
[ |
|
83
|
|
|
'options' => ['secure' => true], |
|
84
|
|
|
'expected' => 'alpha=beta; Path=/; Secure; HttpOnly', |
|
85
|
|
|
], |
|
86
|
|
|
[ |
|
87
|
|
|
'options' => ['path' => '/gamma'], |
|
88
|
|
|
'expected' => 'alpha=beta; Path=/gamma; HttpOnly', |
|
89
|
|
|
], |
|
90
|
|
|
[ |
|
91
|
|
|
'options' => ['path' => null], |
|
92
|
|
|
'expected' => 'alpha=beta; Path=/; HttpOnly', |
|
93
|
|
|
], |
|
94
|
|
|
[ |
|
95
|
|
|
'options' => ['domain' => '.test.com'], |
|
96
|
|
|
'expected' => 'alpha=beta; Domain=.test.com; Path=/; HttpOnly', |
|
97
|
|
|
], |
|
98
|
|
|
[ |
|
99
|
|
|
'options' => ['domain' => 'site.tld', 'path' => '/gamma'], |
|
100
|
|
|
'expected' => 'alpha=beta; Domain=site.tld; Path=/gamma; HttpOnly', |
|
101
|
|
|
], |
|
102
|
|
|
[ |
|
103
|
|
|
'options' => ['expires' => 1410269554], |
|
104
|
|
|
'expected' => 'alpha=beta; Expires=Tue, 09 Sep 2014 13:32:34 GMT; Path=/; HttpOnly', |
|
105
|
|
|
], |
|
106
|
|
|
[ |
|
107
|
|
|
'options' => ['expires' => 0], |
|
108
|
|
|
'expected' => 'alpha=beta; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/; HttpOnly', |
|
109
|
|
|
], |
|
110
|
|
|
[ |
|
111
|
|
|
'options' => ['expires' => 'bad value'], |
|
112
|
|
|
'expected' => 'alpha=beta; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/; HttpOnly', |
|
113
|
|
|
], |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @covers Fracture\Http\Headers\SetCookie::__construct |
|
120
|
|
|
* @covers Fracture\Http\Headers\SetCookie::prepare |
|
121
|
|
|
* @covers Fracture\Http\Headers\SetCookie::getValue |
|
122
|
|
|
* |
|
123
|
|
|
* @covers Fracture\Http\Headers\SetCookie::collectExpireTime |
|
124
|
|
|
* @covers Fracture\Http\Headers\SetCookie::isDateTime |
|
125
|
|
|
* @covers Fracture\Http\Headers\SetCookie::convertTime |
|
126
|
|
|
* |
|
127
|
|
|
* @dataProvider provideHeaderExpireValues |
|
128
|
|
|
*/ |
|
129
|
|
|
public function testHeaderExpireValue($options, $expected) |
|
130
|
|
|
{ |
|
131
|
|
|
$instance = new SetCookie('alpha', 'beta', $options); |
|
132
|
|
|
$instance->prepare(); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertSame($expected, $instance->getValue()); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
public function provideHeaderExpireValues() |
|
139
|
|
|
{ |
|
140
|
|
|
return [ |
|
141
|
|
|
[ |
|
142
|
|
|
'options' => ['expires' => 1410269554], |
|
143
|
|
|
'expected' => 'alpha=beta; Expires=Tue, 09 Sep 2014 13:32:34 GMT; Path=/; HttpOnly', |
|
144
|
|
|
], |
|
145
|
|
|
[ |
|
146
|
|
|
'options' => ['expires' => 0], |
|
147
|
|
|
'expected' => 'alpha=beta; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/; HttpOnly', |
|
148
|
|
|
], |
|
149
|
|
|
[ |
|
150
|
|
|
'options' => ['expires' => 'bad value'], |
|
151
|
|
|
'expected' => 'alpha=beta; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/; HttpOnly', |
|
152
|
|
|
], |
|
153
|
|
|
]; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @covers Fracture\Http\Headers\SetCookie::prepare |
|
158
|
|
|
* @covers Fracture\Http\Headers\SetCookie::isDateTime |
|
159
|
|
|
* @covers Fracture\Http\Headers\SetCookie::convertTime |
|
160
|
|
|
* |
|
161
|
|
|
* @dataProvider provideeaderExpireValueAsDateTime |
|
162
|
|
|
*/ |
|
163
|
|
|
public function testHeaderExpireValueAsDateTime($string, $expected) |
|
164
|
|
|
{ |
|
165
|
|
|
$instance = new SetCookie('alpha', 'beta', [ |
|
166
|
|
|
'expires' => new \DateTime($string), |
|
167
|
|
|
]); |
|
168
|
|
|
$instance->prepare(); |
|
169
|
|
|
|
|
170
|
|
|
$this->assertSame($expected, $instance->getValue()); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
public function provideeaderExpireValueAsDateTime() |
|
175
|
|
|
{ |
|
176
|
|
|
return [ |
|
177
|
|
|
[ |
|
178
|
|
|
'string' => 'Tue, 09 Sep 2014 13:32:34 GMT', |
|
179
|
|
|
'expected' => 'alpha=beta; Expires=Tue, 09 Sep 2014 13:32:34 GMT; Path=/; HttpOnly', |
|
180
|
|
|
], |
|
181
|
|
|
[ |
|
182
|
|
|
'string' => 'Tue, 09 Sep 2014 13:00:00 +0000', |
|
183
|
|
|
'expected' => 'alpha=beta; Expires=Tue, 09 Sep 2014 13:00:00 GMT; Path=/; HttpOnly', |
|
184
|
|
|
], |
|
185
|
|
|
[ |
|
186
|
|
|
'string' => 'Tue, 09 Sep 2014 15:00:00 +0200', |
|
187
|
|
|
'expected' => 'alpha=beta; Expires=Tue, 09 Sep 2014 13:00:00 GMT; Path=/; HttpOnly', |
|
188
|
|
|
], |
|
189
|
|
|
]; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|