Skip to content

Commit

Permalink
Test CookieJar
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Nov 28, 2019
1 parent 30b6739 commit 2a3a7af
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 8 deletions.
32 changes: 32 additions & 0 deletions src/Cookies/AbstractCookieHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Packaged\Http\Cookies;

abstract class AbstractCookieHandler implements CookieHandler
{
public function canHandle(string $name, string $value = null): bool
{
return true;
}

public function decodeName(string $name): string
{
return $name;
}

public function encodeName(string $name): string
{
return $name;
}

public function decodeValue(string $value): string
{
return $value;
}

public function encodeValue(string $value): string
{
return $value;
}

}
35 changes: 28 additions & 7 deletions src/Cookies/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CookieJar implements ConfigurableInterface
* @var CookieHandler[]
*/
protected $_handlers = [];
protected $_handlerPriority = [];

/**
* @var Cookie[]
Expand Down Expand Up @@ -46,19 +47,39 @@ public function addHandler(CookieHandler $handler, int $priority = 10, bool $rep
{
throw new \Exception("A cookie handler already exists with priority " . $priority);
}

$this->_handlers[$priority] = $handler;
$this->_prioritiseHandlers();

return $this;
}

protected function _prioritiseHandlers()
{
$this->_handlerPriority = array_keys($this->_handlers);
sort($this->_handlerPriority);
}

public function removeHandler(int $priority): bool
{
if(isset($this->_handlers[$priority]))
{
unset($this->_handlers[$priority]);
return true;
}
return false;
}

protected function _getHandler($name, $value): CookieHandler
{
foreach($this->_handlers as $handler)
foreach($this->_handlerPriority as $priority)
{
if($handler->canHandle($name, $value))
if(isset($this->_handlers[$priority]) && $this->_handlers[$priority]->canHandle($name, $value))
{
return $handler;
return $this->_handlers[$priority];
}
}
return new DefaultHandler();
}

public function hydrate(Request $request)
Expand All @@ -72,13 +93,13 @@ public function hydrate(Request $request)

public function read(string $name, bool $checkQueued = false)
{
return $this->_requestCookies[$name] ??
($checkQueued && isset($this->_responseCookies[$name]) ? $this->_responseCookies[$name]['v'] : null);
return $checkQueued && isset($this->_responseCookies[$name]) ? $this->_responseCookies[$name]['v']
: ($this->_requestCookies[$name] ?? null);
}

public function has(string $name): bool
public function has(string $name, bool $checkQueued = false): bool
{
return isset($this->_requestCookies[$name]) || isset($this->_responseCookies[$name]);
return isset($this->_requestCookies[$name]) || ($checkQueued && isset($this->_responseCookies[$name]));
}

public function store(string $name, string $value = null, $expireSeconds = 0)
Expand Down
2 changes: 1 addition & 1 deletion src/Cookies/DefaultHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Packaged\Http\Cookies;

class DefaultHandler implements CookieHandler
class DefaultHandler extends AbstractCookieHandler
{
public function canHandle(string $name, string $value = null): bool
{
Expand Down
17 changes: 17 additions & 0 deletions src/Cookies/LowercaseCookieHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Packaged\Http\Cookies;

class LowercaseCookieHandler extends AbstractCookieHandler
{
public function decodeName(string $name): string
{
return parent::decodeName(strtolower($name));
}

public function encodeName(string $name): string
{
return parent::encodeName(strtolower($name));
}

}
65 changes: 65 additions & 0 deletions tests/CookieJarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Packaged\Tests\Http;

use Packaged\Http\Cookies\CookieJar;
use Packaged\Http\Cookies\LowercaseCookieHandler;
use Packaged\Http\Request;
use PHPUnit\Framework\TestCase;

class CookieJarTest extends TestCase
{
public function testCrud()
{
$req = new Request([], [], [], ['ABC' => 'def']);
$jar = new CookieJar();
$jar->hydrate($req);

self::assertEquals('def', $jar->read('ABC'));
self::assertNull($jar->read('xyz'));
self::assertNull($jar->read('xyz', true));
self::assertFalse($jar->has('xyz'));
self::assertFalse($jar->has('xyz', true));

$jar->store('xyz', '123', 10);
self::assertNull($jar->read('xyz'));
self::assertEquals('123', $jar->read('xyz', true));
self::assertFalse($jar->has('xyz'));
self::assertTrue($jar->has('xyz', true));

//Check for an updated value
$jar->store('xyz', '456');
self::assertEquals('456', $jar->read('xyz', true));

$jar->store('ABC', 'ghi');
self::assertEquals('def', $jar->read('ABC', false));
self::assertEquals('ghi', $jar->read('ABC', true));

//Check delete
$jar->delete('xyz');
self::assertNull($jar->read('xyz'));
self::assertNull($jar->read('xyz', true));
}

public function testHandlers()
{
$req = new Request([], [], [], ['ABC' => 'def']);

//Check removing all handlers will fallback to the default handler
$jar = new CookieJar();
self::assertTrue($jar->removeHandler(1000));
self::assertFalse($jar->removeHandler(1000));
$jar->hydrate($req);
self::assertEquals('def', $jar->read('ABC'));
self::assertNull($jar->read('abc'));

$jar = new CookieJar();
$jar->addHandler(new LowercaseCookieHandler(), 10, true);
$jar->hydrate($req);
self::assertNull($jar->read('ABC'));
self::assertEquals('def', $jar->read('abc'));

$this->expectExceptionMessage("A cookie handler already exists with priority 10");
$jar->addHandler(new LowercaseCookieHandler(), 10);
}
}

0 comments on commit 2a3a7af

Please sign in to comment.