11
11
use PHPUnit \Framework \TestCase ;
12
12
13
13
class CookieHandlerTest extends TestCase {
14
- /**
15
- * @dataProvider dataCookie
16
- */
14
+ /** @dataProvider dataCookie */
17
15
public function testcontains (array $ cookieData ) {
18
16
$ cookieHandler = new CookieHandler ($ cookieData );
19
17
@@ -22,9 +20,7 @@ public function testcontains(array $cookieData) {
22
20
}
23
21
}
24
22
25
- /**
26
- * @dataProvider dataCookie
27
- */
23
+ /** @dataProvider dataCookie */
28
24
public function testHasNot (array $ cookieData ) {
29
25
$ cookieHandler = new CookieHandler ($ cookieData );
30
26
$ fakeData = $ this ->generateFakeData ();
@@ -34,9 +30,7 @@ public function testHasNot(array $cookieData) {
34
30
}
35
31
}
36
32
37
- /**
38
- * @dataProvider dataCookie
39
- */
33
+ /** @dataProvider dataCookie */
40
34
public function testGet (array $ cookieData ) {
41
35
$ cookieHandler = new CookieHandler ($ cookieData );
42
36
@@ -46,9 +40,7 @@ public function testGet(array $cookieData) {
46
40
}
47
41
}
48
42
49
- /**
50
- * @dataProvider dataCookie
51
- */
43
+ /** @dataProvider dataCookie */
52
44
public function testGetNotExists (array $ cookieData ) {
53
45
$ cookieHandler = new CookieHandler ($ cookieData );
54
46
@@ -99,9 +91,7 @@ public function testOffsetUnset(array $cookieData) {
99
91
}
100
92
}
101
93
102
- /**
103
- * @dataProvider dataCookie
104
- */
94
+ /** @dataProvider dataCookie */
105
95
public function testOffsetGet (array $ cookieData ) {
106
96
$ cookieHandler = new CookieHandler ($ cookieData );
107
97
@@ -111,9 +101,7 @@ public function testOffsetGet(array $cookieData) {
111
101
}
112
102
}
113
103
114
- /**
115
- * @dataProvider dataCookie
116
- */
104
+ /** @dataProvider dataCookie */
117
105
public function testOffsetGetNotExist (array $ cookieData ) {
118
106
$ cookieHandler = new CookieHandler ($ cookieData );
119
107
$ fakeData = $ this ->generateFakeData ();
@@ -123,9 +111,7 @@ public function testOffsetGetNotExist(array $cookieData) {
123
111
}
124
112
}
125
113
126
- /**
127
- * @dataProvider dataCookie
128
- */
114
+ /** @dataProvider dataCookie */
129
115
public function testOffsetExists (array $ cookieData ) {
130
116
$ cookieHandler = new CookieHandler ($ cookieData );
131
117
@@ -134,9 +120,7 @@ public function testOffsetExists(array $cookieData) {
134
120
}
135
121
}
136
122
137
- /**
138
- * @dataProvider dataCookie
139
- */
123
+ /** @dataProvider dataCookie */
140
124
public function testOffsetNotExists (array $ cookieData ) {
141
125
$ cookieHandler = new CookieHandler ($ cookieData );
142
126
$ fakeData = $ this ->generateFakeData ();
@@ -213,9 +197,7 @@ function(string $name, string $value, int $expires = 0, string $path = "", strin
213
197
}
214
198
}
215
199
216
- /**
217
- * @dataProvider dataCookie
218
- */
200
+ /** @dataProvider dataCookie */
219
201
public function testIterator (array $ cookieData ) {
220
202
$ cookieHandler = new CookieHandler ($ cookieData );
221
203
@@ -235,6 +217,56 @@ public function testOffsetSet() {
235
217
$ cookieHandler ["anything " ] = "nothing " ;
236
218
}
237
219
220
+ /** @dataProvider dataCookie */
221
+ public function testClearAll (array $ cookieData ) {
222
+ $ setCookieCalls = [];
223
+ Override::setCallback (
224
+ "setcookie " ,
225
+ function (string $ name , string $ value , int $ expires = 0 , string $ path = "" , string $ domain = "" , bool $ secure = false , bool $ httponly = false ) use (&$ setCookieCalls ) {
226
+ $ setCookieCalls []= [$ name , $ value , $ expires , $ path , $ domain , $ secure , $ httponly ];
227
+ }
228
+ );
229
+
230
+ $ sut = new CookieHandler ($ cookieData );
231
+ self ::assertGreaterThan (0 , count ($ sut ));
232
+ $ sut ->clear ();
233
+ self ::assertEquals (0 , count ($ sut ));
234
+
235
+ self ::assertEquals (count ($ cookieData ), count ($ setCookieCalls ));
236
+ }
237
+
238
+ /** @dataProvider dataCookie */
239
+ public function testClearMultiple (array $ cookieData ) {
240
+ Override::setCallback ("setcookie " , function (){});
241
+ $ sut = new CookieHandler ($ cookieData );
242
+ $ numberToClear = rand (0 , count ($ cookieData ) - 1 );
243
+ $ cookiesToClear = [];
244
+ $ copyOfCookieData = $ cookieData ;
245
+
246
+ for ($ i = 0 ; $ i < $ numberToClear ; $ i ++) {
247
+ $ toClear = array_rand ($ copyOfCookieData );
248
+ $ cookiesToClear []= $ toClear ;
249
+ unset($ copyOfCookieData [$ toClear ]);
250
+ }
251
+
252
+ $ sut ->clear (...$ cookiesToClear );
253
+
254
+ self ::assertCount (
255
+ count ($ cookieData ) - count ($ cookiesToClear ),
256
+ $ sut
257
+ );
258
+ }
259
+
260
+ /** @dataProvider dataCookie */
261
+ public function testClearSingle (array $ cookieData ) {
262
+ $ toClear = array_rand ($ cookieData );
263
+ Override::setCallback ("setcookie " , function (){});
264
+ $ sut = new CookieHandler ($ cookieData );
265
+ self ::assertTrue ($ sut ->contains ($ toClear ));
266
+ $ sut ->clear ($ toClear );
267
+ self ::assertFalse ($ sut ->contains ($ toClear ));
268
+ }
269
+
238
270
public function dataCookie ():array {
239
271
$ data = [];
240
272
0 commit comments