Skip to content

Commit e121a73

Browse files
committed
Add cookie
Applied fixes from StyleCI Make CookieJar a single class Update Cookie docblocks Make properties private Remove virtual expiration attribute Improve character match tests Improve cookie tests Make Max-Age defatul null Add deprecation notice to expires constructor argument Add docblock constructor Allow null to be passed to expires Fine tune regex Add some more tests for path matching Add test for invalid max age
1 parent fe15f35 commit e121a73

File tree

9 files changed

+1138
-1
lines changed

9 files changed

+1138
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
### Added
77

88
- Autoregistration of stream filters using Composer autoload
9+
- Cookie
910

1011

1112
## 0.1.2 - 2015-12-26

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This package contains various PSR-7 tools which might be useful in an HTTP workf
2727
- Various Stream encoding tools
2828
- Message decorators
2929
- Message factory implementations for Guzzle PSR-7 and Diactoros
30+
- Cookie implementation
3031

3132

3233
## Documentation

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"guzzlehttp/psr7": "^1.0",
2222
"ext-zlib": "*",
2323
"phpspec/phpspec": "^2.4",
24-
"henrikbjorn/phpspec-code-coverage" : "^1.0"
24+
"henrikbjorn/phpspec-code-coverage" : "^1.0",
25+
"coduo/phpspec-data-provider-extension": "^1.0"
2526
},
2627
"suggest": {
2728
"zendframework/zend-diactoros": "Used with Diactoros Factories",

phpspec.yml.ci

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ suites:
55
formatter.name: pretty
66
extensions:
77
- PhpSpec\Extension\CodeCoverageExtension
8+
- Coduo\PhpSpec\DataProvider\DataProviderExtension
89
code_coverage:
910
format: clover
1011
output: build/coverage.xml

phpspec.yml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ suites:
33
namespace: Http\Message
44
psr4_prefix: Http\Message
55
formatter.name: pretty
6+
extensions:
7+
- Coduo\PhpSpec\DataProvider\DataProviderExtension

spec/CookieJarSpec.php

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
3+
namespace spec\Http\Message;
4+
5+
use Http\Message\Cookie;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class CookieJarSpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('Http\Message\CookieJar');
13+
}
14+
15+
function it_is_an_iterator_aggregate()
16+
{
17+
$this->getIterator()->shouldHaveType('Iterator');
18+
}
19+
20+
function it_has_a_cookie()
21+
{
22+
$cookie = new Cookie('name', 'value');
23+
24+
$this->addCookie($cookie);
25+
26+
$this->hasCookie($cookie)->shouldReturn(true);
27+
$this->hasCookies()->shouldReturn(true);
28+
}
29+
30+
function it_accepts_a_cookie()
31+
{
32+
$cookie = new Cookie('name', 'value');
33+
$cookie2 = new Cookie('name', 'value2');
34+
35+
$this->addCookie($cookie);
36+
$this->addCookie($cookie2);
37+
38+
$this->hasCookie($cookie)->shouldReturn(false);
39+
$this->hasCookie($cookie2)->shouldReturn(true);
40+
}
41+
42+
function it_removes_a_cookie_with_an_empty_value()
43+
{
44+
$cookie = new Cookie('name', 'value');
45+
$cookie2 = new Cookie('name');
46+
47+
$this->addCookie($cookie);
48+
$this->addCookie($cookie2);
49+
50+
$this->hasCookie($cookie)->shouldReturn(false);
51+
$this->hasCookie($cookie2)->shouldReturn(false);
52+
}
53+
54+
function it_removes_a_cookie_with_a_lower_expiration_time()
55+
{
56+
$cookie = new Cookie('name', 'value', 100);
57+
$cookie2 = new Cookie('name', 'value', 1000);
58+
59+
$this->addCookie($cookie);
60+
$this->addCookie($cookie2);
61+
62+
$this->hasCookie($cookie)->shouldReturn(false);
63+
$this->hasCookie($cookie2)->shouldReturn(true);
64+
}
65+
66+
function it_removes_a_cookie()
67+
{
68+
$cookie = new Cookie('name', 'value', 100);
69+
70+
$this->addCookie($cookie);
71+
$this->removeCookie($cookie);
72+
73+
$this->hasCookie($cookie)->shouldReturn(false);
74+
}
75+
76+
function it_returns_all_cookies()
77+
{
78+
$cookie = new Cookie('name', 'value');
79+
$cookie2 = new Cookie('name2', 'value');
80+
81+
$this->addCookie($cookie);
82+
$this->addCookie($cookie2);
83+
84+
$this->getCookies()->shouldBeAnArrayOfInstance('Http\Message\Cookie');
85+
}
86+
87+
function it_returns_the_matching_cookies()
88+
{
89+
$cookie = new Cookie('name', 'value');
90+
$cookie2 = new Cookie('name', 'value2');
91+
92+
$this->addCookie($cookie);
93+
94+
$this->getMatchingCookies($cookie2)->shouldBeAnArrayOfInstance('Http\Message\Cookie');
95+
}
96+
97+
function it_sets_cookies()
98+
{
99+
$cookie = new Cookie('name', 'value');
100+
101+
$this->setCookies([$cookie]);
102+
103+
$this->hasCookie($cookie)->shouldReturn(true);
104+
$this->hasCookies()->shouldReturn(true);
105+
$this->count()->shouldReturn(1);
106+
}
107+
108+
function it_accepts_cookies()
109+
{
110+
$cookie = new Cookie('name', 'value');
111+
$cookie2 = new Cookie('name2', 'value');
112+
113+
$this->addCookie($cookie);
114+
$this->addCookies([$cookie2]);
115+
116+
$this->hasCookie($cookie)->shouldReturn(true);
117+
$this->hasCookie($cookie2)->shouldReturn(true);
118+
$this->hasCookies()->shouldReturn(true);
119+
$this->count()->shouldReturn(2);
120+
}
121+
122+
function it_removes_cookies()
123+
{
124+
$cookie = new Cookie('name', 'value');
125+
$cookie2 = new Cookie('name2', 'value');
126+
127+
$this->addCookies([$cookie, $cookie2]);
128+
$this->removeCookies([$cookie2]);
129+
130+
$this->hasCookie($cookie)->shouldReturn(true);
131+
$this->hasCookie($cookie2)->shouldReturn(false);
132+
$this->hasCookies()->shouldReturn(true);
133+
$this->count()->shouldReturn(1);
134+
}
135+
136+
function it_removes_matching_cookies()
137+
{
138+
$cookie = new Cookie('name', 'value');
139+
$cookie2 = new Cookie('name2', 'value', 0, 'php-http.org');
140+
141+
$this->addCookies([$cookie, $cookie2]);
142+
143+
$this->removeMatchingCookies('name2', 'php-http.org', '/');
144+
145+
$this->hasCookie($cookie)->shouldReturn(true);
146+
$this->hasCookie($cookie2)->shouldReturn(false);
147+
$this->hasCookies()->shouldReturn(true);
148+
$this->count()->shouldReturn(1);
149+
}
150+
151+
function it_clears_cookies()
152+
{
153+
$cookie = new Cookie('name', 'value', 0, 'php-http.org');
154+
$cookie2 = new Cookie('name2', 'value');
155+
156+
$this->addCookies([$cookie, $cookie2]);
157+
158+
$this->clear();
159+
160+
$this->hasCookies()->shouldReturn(false);
161+
$this->count()->shouldReturn(0);
162+
}
163+
164+
public function getMatchers()
165+
{
166+
return [
167+
'beAnArrayOfInstance' => function ($subject, $instance) {
168+
foreach ($subject as $element) {
169+
if (!$element instanceof $instance) {
170+
return false;
171+
}
172+
}
173+
174+
return true;
175+
},
176+
];
177+
}
178+
}

0 commit comments

Comments
 (0)