@@ -47,6 +47,15 @@ public function testInstantiationThrowsExceptionIfRawCookieNameContainsSpecialCh
47
47
Cookie::create ($ name , null , 0 , null , null , null , false , true );
48
48
}
49
49
50
+ /**
51
+ * @dataProvider namesWithSpecialCharacters
52
+ */
53
+ public function testWithRawThrowsExceptionIfCookieNameContainsSpecialCharacters ($ name )
54
+ {
55
+ $ this ->expectException ('InvalidArgumentException ' );
56
+ Cookie::create ($ name )->withRaw ();
57
+ }
58
+
50
59
/**
51
60
* @dataProvider namesWithSpecialCharacters
52
61
*/
@@ -72,6 +81,10 @@ public function testNegativeExpirationIsNotPossible()
72
81
$ cookie = Cookie::create ('foo ' , 'bar ' , -100 );
73
82
74
83
$ this ->assertSame (0 , $ cookie ->getExpiresTime ());
84
+
85
+ $ cookie = Cookie::create ('foo ' , 'bar ' )->withExpires (-100 );
86
+
87
+ $ this ->assertSame (0 , $ cookie ->getExpiresTime ());
75
88
}
76
89
77
90
public function testGetValue ()
@@ -98,13 +111,21 @@ public function testGetExpiresTime()
98
111
$ cookie = Cookie::create ('foo ' , 'bar ' , $ expire = time () + 3600 );
99
112
100
113
$ this ->assertEquals ($ expire , $ cookie ->getExpiresTime (), '->getExpiresTime() returns the expire date ' );
114
+
115
+ $ cookie = Cookie::create ('foo ' )->withExpires ($ expire = time () + 3600 );
116
+
117
+ $ this ->assertEquals ($ expire , $ cookie ->getExpiresTime (), '->getExpiresTime() returns the expire date ' );
101
118
}
102
119
103
120
public function testGetExpiresTimeIsCastToInt ()
104
121
{
105
122
$ cookie = Cookie::create ('foo ' , 'bar ' , 3600.9 );
106
123
107
124
$ this ->assertSame (3600 , $ cookie ->getExpiresTime (), '->getExpiresTime() returns the expire date as an integer ' );
125
+
126
+ $ cookie = Cookie::create ('foo ' )->withExpires (3600.6 );
127
+
128
+ $ this ->assertSame (3600 , $ cookie ->getExpiresTime (), '->getExpiresTime() returns the expire date as an integer ' );
108
129
}
109
130
110
131
public function testConstructorWithDateTime ()
@@ -113,6 +134,10 @@ public function testConstructorWithDateTime()
113
134
$ cookie = Cookie::create ('foo ' , 'bar ' , $ expire );
114
135
115
136
$ this ->assertEquals ($ expire ->format ('U ' ), $ cookie ->getExpiresTime (), '->getExpiresTime() returns the expire date ' );
137
+
138
+ $ cookie = Cookie::create ('foo ' )->withExpires ($ expire );
139
+
140
+ $ this ->assertEquals ($ expire ->format ('U ' ), $ cookie ->getExpiresTime (), '->getExpiresTime() returns the expire date ' );
116
141
}
117
142
118
143
public function testConstructorWithDateTimeImmutable ()
@@ -121,6 +146,10 @@ public function testConstructorWithDateTimeImmutable()
121
146
$ cookie = Cookie::create ('foo ' , 'bar ' , $ expire );
122
147
123
148
$ this ->assertEquals ($ expire ->format ('U ' ), $ cookie ->getExpiresTime (), '->getExpiresTime() returns the expire date ' );
149
+
150
+ $ cookie = Cookie::create ('foo ' )->withValue ('bar ' )->withExpires ($ expire );
151
+
152
+ $ this ->assertEquals ($ expire ->format ('U ' ), $ cookie ->getExpiresTime (), '->getExpiresTime() returns the expire date ' );
124
153
}
125
154
126
155
public function testGetExpiresTimeWithStringValue ()
@@ -130,34 +159,54 @@ public function testGetExpiresTimeWithStringValue()
130
159
$ expire = strtotime ($ value );
131
160
132
161
$ this ->assertEqualsWithDelta ($ expire , $ cookie ->getExpiresTime (), 1 , '->getExpiresTime() returns the expire date ' );
162
+
163
+ $ cookie = Cookie::create ('foo ' )->withVal
10000
ue ('bar ' )->withExpires ($ value );
164
+
165
+ $ this ->assertEqualsWithDelta ($ expire , $ cookie ->getExpiresTime (), 1 , '->getExpiresTime() returns the expire date ' );
133
166
}
134
167
135
168
public function testGetDomain ()
136
169
{
137
170
$ cookie = Cookie::create ('foo ' , 'bar ' , 0 , '/ ' , '.myfoodomain.com ' );
138
171
139
172
$ this ->assertEquals ('.myfoodomain.com ' , $ cookie ->getDomain (), '->getDomain() returns the domain name on which the cookie is valid ' );
173
+
174
+ $ cookie = Cookie::create ('foo ' )->withDomain ('.mybardomain.com ' );
175
+
176
+ $ this ->assertEquals ('.mybardomain.com ' , $ cookie ->getDomain (), '->getDomain() returns the domain name on which the cookie is valid ' );
140
177
}
141
178
142
179
public function testIsSecure ()
143
180
{
144
181
$ cookie = Cookie::create ('foo ' , 'bar ' , 0 , '/ ' , '.myfoodomain.com ' , true );
145
182
146
183
$ this ->assertTrue ($ cookie ->isSecure (), '->isSecure() returns whether the cookie is transmitted over HTTPS ' );
184
+
185
+ $ cookie = Cookie::create ('foo ' )->withSecure (true );
186
+
187
+ $ this ->assertTrue ($ cookie ->isSecure (), '->isSecure() returns whether the cookie is transmitted over HTTPS ' );
147
188
}
148
189
149
190
public function testIsHttpOnly ()
150
191
{
151
192
$ cookie = Cookie::create ('foo ' , 'bar ' , 0 , '/ ' , '.myfoodomain.com ' , false , true );
152
193
153
194
$ this ->assertTrue ($ cookie ->isHttpOnly (), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP ' );
195
+
196
+ $ cookie = Cookie::create ('foo ' )->withHttpOnly (true );
197
+
198
+ $ this ->assertTrue ($ cookie ->isHttpOnly (), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP ' );
154
199
}
155
200
156
201
public function testCookieIsNotCleared ()
157
202
{
158
203
$ cookie = Cookie::create ('foo ' , 'bar ' , time () + 3600 * 24 );
159
204
160
205
$ this ->assertFalse ($ cookie ->isCleared (), '->isCleared() returns false if the cookie did not expire yet ' );
206
+
207
+ $ cookie = Cookie::create ('foo ' )->withExpires (time () + 3600 * 24 );
208
+
209
+ $ this ->assertFalse ($ cookie ->isCleared (), '->isCleared() returns false if the cookie did not expire yet ' );
161
210
}
162
211
163
212
public function testCookieIsCleared ()
@@ -166,6 +215,10 @@ public function testCookieIsCleared()
166
215
167
216
$ this ->assertTrue ($ cookie ->isCleared (), '->isCleared() returns true if the cookie has expired ' );
168
217
218
+ $ cookie = Cookie::create ('foo ' )->withExpires (time () - 20 );
219
+
220
+ $ this ->assertTrue ($ cookie ->isCleared (), '->isCleared() returns true if the cookie has expired ' );
221
+
169
222
$ cookie = Cookie::create ('foo ' , 'bar ' );
170
223
171
224
$ this ->assertFalse ($ cookie ->isCleared ());
@@ -177,21 +230,55 @@ public function testCookieIsCleared()
177
230
$ cookie = Cookie::create ('foo ' , 'bar ' , -1 );
178
231
179
232
$ this ->assertFalse ($ cookie ->isCleared ());
233
+
234
+ $ cookie = Cookie::create ('foo ' )->withExpires (-1 );
235
+
236
+ $ this ->assertFalse ($ cookie ->isCleared ());
180
237
}
181
238
182
239
public function testToString ()
183
240
{
241
+ $ expected = 'foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/; domain=.myfoodomain.com; secure; httponly ' ;
184
242
$ cookie = Cookie::create ('foo ' , 'bar ' , $ expire = strtotime ('Fri, 20-May-2011 15:25:52 GMT ' ), '/ ' , '.myfoodomain.com ' , true , true , false , null );
185
- $ this ->assertEquals ('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/; domain=.myfoodomain.com; secure; httponly ' , (string ) $ cookie , '->__toString() returns string representation of the cookie ' );
243
+ $ this ->assertEquals ($ expected , (string ) $ cookie , '->__toString() returns string representation of the cookie ' );
244
+
245
+ $ cookie = Cookie::create ('foo ' )
246
+ ->withValue ('bar ' )
247
+ ->withExpires (strtotime ('Fri, 20-May-2011 15:25:52 GMT ' ))
248
+ ->withDomain ('.myfoodomain.com ' )
249
+ ->withSecure (true )
250
+ ->withSameSite (null );
251
+ $ this ->assertEquals ($ expected , (string ) $ cookie , '->__toString() returns string representation of the cookie ' );
186
252
253
+ $ expected = 'foo=bar%20with%20white%20spaces; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/; domain=.myfoodomain.com; secure; httponly ' ;
187
254
$ cookie = Cookie::create ('foo ' , 'bar with white spaces ' , strtotime ('Fri, 20-May-2011 15:25:52 GMT ' ), '/ ' , '.myfoodomain.com ' , true , true , false , null );
188
- $ this ->assertEquals (' foo=bar%20with%20white%20spaces; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/; domain=.myfoodomain.com; secure; httponly ' , (string ) $ cookie , '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20) ' );
255
+ $ this ->assertEquals ($ expected , (string ) $ cookie , '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20) ' );
189
256
257
+ $ cookie = Cookie::create ('foo ' )
258
+ ->withValue ('bar with white spaces ' )
259
+ ->withExpires (strtotime ('Fri, 20-May-2011 15:25:52 GMT ' ))
260
+ ->withDomain ('.myfoodomain.com ' )
261
+ ->withSecure (true )
262
+ ->withSameSite (null );
263
+ $ this ->assertEquals ($ expected , (string ) $ cookie , '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20) ' );
264
+
265
+ $ expected = 'foo=deleted; expires= ' .gmdate ('D, d-M-Y H:i:s T ' , $ expire = time () - 31536001 ).'; Max-Age=0; path=/admin/; domain=.myfoodomain.com; httponly ' ;
190
266
$ cookie = Cookie::create ('foo ' , null , 1 , '/admin/ ' , '.myfoodomain.com ' , false , true , false , null );
191
- $ this ->assertEquals ('foo=deleted; expires= ' .gmdate ('D, d-M-Y H:i:s T ' , $ expire = time () - 31536001 ).'; Max-Age=0; path=/admin/; domain=.myfoodomain.com; httponly ' , (string ) $ cookie , '->__toString() returns string representation of a cleared cookie if value is NULL ' );
267
+ $ this ->assertEquals ($ expected , (string ) $ cookie , '->__toString() returns string representation of a cleared cookie if value is NULL ' );
268
+
269
+ $ cookie = Cookie::create ('foo ' )
270
+ ->withExpires (1 )
271
+ ->withPath ('/admin/ ' )
272
+ ->withDomain ('.myfoodomain.com ' )
273
+ ->withSameSite (null );
274
+ $ this ->assertEquals ($ expected , (string ) $ cookie , '->__toString() returns string representation of a cleared cookie if value is NULL ' );
192
275
276
+ $ expected = 'foo=bar; path=/; httponly; samesite=lax ' ;
193
277
$ cookie = Cookie::create ('foo ' , 'bar ' );
194
- $ this ->assertEquals ('foo=bar; path=/; httponly; samesite=lax ' , (string ) $ cookie );
278
+ $ this ->assertEquals ($ expected , (string ) $ cookie );
279
+
280
+ $ cookie = Cookie::create ('foo ' )->withValue ('bar ' );
281
+ $ this ->assertEquals ($ expected , (string ) $ cookie );
195
282
}
196
283
197
284
public function testRawCookie ()
@@ -200,9 +287,21 @@ public function testRawCookie()
200
287
$ this ->assertFalse ($ cookie ->isRaw ());
201
288
$ this ->assertEquals ('foo=b%20a%20r; path=/ ' , (string ) $ cookie );
202
289
290
+ $ cookie = Cookie::create ('test ' )->withValue ('t e s t ' )->withHttpOnly (false )->withSameSite (null );
291
+ $ this ->assertFalse ($ cookie ->isRaw ());
292
+ $ this ->assertEquals ('test=t%20e%20s%20t; path=/ ' , (string ) $ cookie );
293
+
203
294
$ cookie = Cookie::create ('foo ' , 'b+a+r ' , 0 , '/ ' , null , false , false , true , null );
204
295
$ this ->assertTrue ($ cookie ->isRaw ());
205
296
$ this ->assertEquals ('foo=b+a+r; path=/ ' , (string ) $ cookie );
297
+
298
+ $ cookie = Cookie::create ('foo ' )
299
+ ->withValue ('t+e+s+t ' )
300
+ ->withHttpOnly (false )
301
+ ->withRaw (true )
302
+ ->withSameSite (null );
303
+ $ this ->assertTrue ($ cookie ->isRaw ());
304
+ $ this ->assertEquals ('foo=t+e+s+t; path=/ ' , (string ) $ cookie );
206
305
}
207
306
208
307
public function testGetMaxAge ()
@@ -245,6 +344,9 @@ public function testSameSiteAttribute()
245
344
246
345
$ cookie = new Cookie ('foo ' , 'bar ' , 0 , '/ ' , null , false , true , false , '' );
247
346
$ this ->assertNull ($ cookie ->getSameSite ());
347
+
348
+ $ cookie = Cookie::create ('foo ' )->withSameSite ('Lax ' );
349
+ $ this ->assertEquals ('lax ' , $ cookie ->getSameSite ());
248
350
}
249
351
250
352
public function testSetSecureDefault ()
0 commit comments