Skip to content

Commit 365b528

Browse files
author
Greg Bowler
committed
Clear all/specific cookies, closes #6
1 parent e8834ab commit 365b528

File tree

3 files changed

+84
-34
lines changed

3 files changed

+84
-34
lines changed

composer.lock

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CookieHandler.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,33 @@
99

1010
class CookieHandler implements ArrayAccess, Iterator, Countable {
1111
/** @var Cookie[] */
12-
protected $cookieList = [];
13-
protected $iteratorIndex = 0;
12+
protected $cookieList;
13+
protected $iteratorIndex;
1414

1515
public function __construct(array $existingCookies = []) {
16+
$this->cookieList = [];
17+
$this->iteratorIndex = 0;
18+
1619
foreach($existingCookies as $name => $value) {
1720
$this->cookieList[$name] = new Cookie($name, $value);
1821
}
1922
}
2023

24+
public function clear(string...$nameList):void {
25+
if(empty($nameList)) {
26+
$this->clear(...array_keys($this->cookieList));
27+
}
28+
29+
foreach($nameList as $name) {
30+
$this->set(
31+
$name,
32+
"",
33+
new DateTime("-1 hour")
34+
);
35+
unset($this->cookieList[$name]);
36+
}
37+
}
38+
2139
public function contains(string $name):bool {
2240
return isset($this->cookieList[$name]);
2341
}

test/unit/CookieHandlerTest.php

+59-27
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
use PHPUnit\Framework\TestCase;
1212

1313
class CookieHandlerTest extends TestCase {
14-
/**
15-
* @dataProvider dataCookie
16-
*/
14+
/** @dataProvider dataCookie */
1715
public function testcontains(array $cookieData) {
1816
$cookieHandler = new CookieHandler($cookieData);
1917

@@ -22,9 +20,7 @@ public function testcontains(array $cookieData) {
2220
}
2321
}
2422

25-
/**
26-
* @dataProvider dataCookie
27-
*/
23+
/** @dataProvider dataCookie */
2824
public function testHasNot(array $cookieData) {
2925
$cookieHandler = new CookieHandler($cookieData);
3026
$fakeData = $this->generateFakeData();
@@ -34,9 +30,7 @@ public function testHasNot(array $cookieData) {
3430
}
3531
}
3632

37-
/**
38-
* @dataProvider dataCookie
39-
*/
33+
/** @dataProvider dataCookie */
4034
public function testGet(array $cookieData) {
4135
$cookieHandler = new CookieHandler($cookieData);
4236

@@ -46,9 +40,7 @@ public function testGet(array $cookieData) {
4640
}
4741
}
4842

49-
/**
50-
* @dataProvider dataCookie
51-
*/
43+
/** @dataProvider dataCookie */
5244
public function testGetNotExists(array $cookieData) {
5345
$cookieHandler = new CookieHandler($cookieData);
5446

@@ -99,9 +91,7 @@ public function testOffsetUnset(array $cookieData) {
9991
}
10092
}
10193

102-
/**
103-
* @dataProvider dataCookie
104-
*/
94+
/** @dataProvider dataCookie */
10595
public function testOffsetGet(array $cookieData) {
10696
$cookieHandler = new CookieHandler($cookieData);
10797

@@ -111,9 +101,7 @@ public function testOffsetGet(array $cookieData) {
111101
}
112102
}
113103

114-
/**
115-
* @dataProvider dataCookie
116-
*/
104+
/** @dataProvider dataCookie */
117105
public function testOffsetGetNotExist(array $cookieData) {
118106
$cookieHandler = new CookieHandler($cookieData);
119107
$fakeData = $this->generateFakeData();
@@ -123,9 +111,7 @@ public function testOffsetGetNotExist(array $cookieData) {
123111
}
124112
}
125113

126-
/**
127-
* @dataProvider dataCookie
128-
*/
114+
/** @dataProvider dataCookie */
129115
public function testOffsetExists(array $cookieData) {
130116
$cookieHandler = new CookieHandler($cookieData);
131117

@@ -134,9 +120,7 @@ public function testOffsetExists(array $cookieData) {
134120
}
135121
}
136122

137-
/**
138-
* @dataProvider dataCookie
139-
*/
123+
/** @dataProvider dataCookie */
140124
public function testOffsetNotExists(array $cookieData) {
141125
$cookieHandler = new CookieHandler($cookieData);
142126
$fakeData = $this->generateFakeData();
@@ -213,9 +197,7 @@ function(string $name, string $value, int $expires = 0, string $path = "", strin
213197
}
214198
}
215199

216-
/**
217-
* @dataProvider dataCookie
218-
*/
200+
/** @dataProvider dataCookie */
219201
public function testIterator(array $cookieData) {
220202
$cookieHandler = new CookieHandler($cookieData);
221203

@@ -235,6 +217,56 @@ public function testOffsetSet() {
235217
$cookieHandler["anything"] = "nothing";
236218
}
237219

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+
238270
public function dataCookie():array {
239271
$data = [];
240272

0 commit comments

Comments
 (0)