Skip to content

Add cookie #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
### Added

- Autoregistration of stream filters using Composer autoload
- Cookie


## 0.1.2 - 2015-12-26
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This package contains various PSR-7 tools which might be useful in an HTTP workf
- Various Stream encoding tools
- Message decorators
- Message factory implementations for Guzzle PSR-7 and Diactoros
- Cookie implementation


## Documentation
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"guzzlehttp/psr7": "^1.0",
"ext-zlib": "*",
"phpspec/phpspec": "^2.4",
"henrikbjorn/phpspec-code-coverage" : "^1.0"
"henrikbjorn/phpspec-code-coverage" : "^1.0",
"coduo/phpspec-data-provider-extension": "^1.0"
},
"suggest": {
"zendframework/zend-diactoros": "Used with Diactoros Factories",
Expand Down
1 change: 1 addition & 0 deletions phpspec.yml.ci
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ suites:
formatter.name: pretty
extensions:
- PhpSpec\Extension\CodeCoverageExtension
- Coduo\PhpSpec\DataProvider\DataProviderExtension
code_coverage:
format: clover
output: build/coverage.xml
2 changes: 2 additions & 0 deletions phpspec.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ suites:
namespace: Http\Message
psr4_prefix: Http\Message
formatter.name: pretty
extensions:
- Coduo\PhpSpec\DataProvider\DataProviderExtension
178 changes: 178 additions & 0 deletions spec/CookieJarSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

namespace spec\Http\Message;

use Http\Message\Cookie;
use PhpSpec\ObjectBehavior;

class CookieJarSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Http\Message\CookieJar');
}

function it_is_an_iterator_aggregate()
{
$this->getIterator()->shouldHaveType('Iterator');
}

function it_has_a_cookie()
{
$cookie = new Cookie('name', 'value');

$this->addCookie($cookie);

$this->hasCookie($cookie)->shouldReturn(true);
$this->hasCookies()->shouldReturn(true);
}

function it_accepts_a_cookie()
{
$cookie = new Cookie('name', 'value');
$cookie2 = new Cookie('name', 'value2');

$this->addCookie($cookie);
$this->addCookie($cookie2);

$this->hasCookie($cookie)->shouldReturn(false);
$this->hasCookie($cookie2)->shouldReturn(true);
}

function it_removes_a_cookie_with_an_empty_value()
{
$cookie = new Cookie('name', 'value');
$cookie2 = new Cookie('name');

$this->addCookie($cookie);
$this->addCookie($cookie2);

$this->hasCookie($cookie)->shouldReturn(false);
$this->hasCookie($cookie2)->shouldReturn(false);
}

function it_removes_a_cookie_with_a_lower_expiration_time()
{
$cookie = new Cookie('name', 'value', 100);
$cookie2 = new Cookie('name', 'value', 1000);

$this->addCookie($cookie);
$this->addCookie($cookie2);

$this->hasCookie($cookie)->shouldReturn(false);
$this->hasCookie($cookie2)->shouldReturn(true);
}

function it_removes_a_cookie()
{
$cookie = new Cookie('name', 'value', 100);

$this->addCookie($cookie);
$this->removeCookie($cookie);

$this->hasCookie($cookie)->shouldReturn(false);
}

function it_returns_all_cookies()
{
$cookie = new Cookie('name', 'value');
$cookie2 = new Cookie('name2', 'value');

$this->addCookie($cookie);
$this->addCookie($cookie2);

$this->getCookies()->shouldBeAnArrayOfInstance('Http\Message\Cookie');
}

function it_returns_the_matching_cookies()
{
$cookie = new Cookie('name', 'value');
$cookie2 = new Cookie('name', 'value2');

$this->addCookie($cookie);

$this->getMatchingCookies($cookie2)->shouldBeAnArrayOfInstance('Http\Message\Cookie');
}

function it_sets_cookies()
{
$cookie = new Cookie('name', 'value');

$this->setCookies([$cookie]);

$this->hasCookie($cookie)->shouldReturn(true);
$this->hasCookies()->shouldReturn(true);
$this->count()->shouldReturn(1);
}

function it_accepts_cookies()
{
$cookie = new Cookie('name', 'value');
$cookie2 = new Cookie('name2', 'value');

$this->addCookie($cookie);
$this->addCookies([$cookie2]);

$this->hasCookie($cookie)->shouldReturn(true);
$this->hasCookie($cookie2)->shouldReturn(true);
$this->hasCookies()->shouldReturn(true);
$this->count()->shouldReturn(2);
}

function it_removes_cookies()
{
$cookie = new Cookie('name', 'value');
$cookie2 = new Cookie('name2', 'value');

$this->addCookies([$cookie, $cookie2]);
$this->removeCookies([$cookie2]);

$this->hasCookie($cookie)->shouldReturn(true);
$this->hasCookie($cookie2)->shouldReturn(false);
$this->hasCookies()->shouldReturn(true);
$this->count()->shouldReturn(1);
}

function it_removes_matching_cookies()
{
$cookie = new Cookie('name', 'value');
$cookie2 = new Cookie('name2', 'value', 0, 'php-http.org');

$this->addCookies([$cookie, $cookie2]);

$this->removeMatchingCookies('name2', 'php-http.org', '/');

$this->hasCookie($cookie)->shouldReturn(true);
$this->hasCookie($cookie2)->shouldReturn(false);
$this->hasCookies()->shouldReturn(true);
$this->count()->shouldReturn(1);
}

function it_clears_cookies()
{
$cookie = new Cookie('name', 'value', 0, 'php-http.org');
$cookie2 = new Cookie('name2', 'value');

$this->addCookies([$cookie, $cookie2]);

$this->clear();

$this->hasCookies()->shouldReturn(false);
$this->count()->shouldReturn(0);
}

public function getMatchers()
{
return [
'beAnArrayOfInstance' => function ($subject, $instance) {
foreach ($subject as $element) {
if (!$element instanceof $instance) {
return false;
}
}

return true;
},
];
}
}
Loading