Skip to content

Commit

Permalink
Reduce responsibility of Lily\Middleware\Session\*Store
Browse files Browse the repository at this point in the history
By simplifying the responsibility of session stores and moving logic to
session middleware we can keep behaviour consistent in one place rather
than in each store.
  • Loading branch information
lukemorton committed Mar 11, 2014
1 parent d126781 commit a9ece75
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 41 deletions.
8 changes: 7 additions & 1 deletion src/Lily/Middleware/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public function __invoke($handler)
return function ($request) use ($handler, $store) {
$request = $store->get($request);
$response = $handler($request);
$response = $store->set($request, $response);

if (empty($response['session'])) {
$response['session'] = array();
}

$response['session'] += $request['session'];
$response = $store->set($response);
return $response;
};
}
Expand Down
10 changes: 2 additions & 8 deletions src/Lily/Middleware/Session/CookieStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,10 @@ public function get($request)
return $request;
}

public function set($request, $response)
public function set($response)
{
if (isset($response['session'])) {
$session = $response['session'];

if (isset($request['session'])) {
$session += $request['session'];
}

$value = json_encode($session);
$value = json_encode($response['session']);
$response['cookies'][$this->name] = compact('value') + $this->cookie;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Lily/Middleware/Session/NativeStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public function get($request)
return $request;
}

public function set($request, $response)
public function set($response)
{
if (isset($response['session'])) {
$_SESSION = $response['session'] + $_SESSION;
$_SESSION = $response['session'];
}

return $response;
Expand Down
4 changes: 2 additions & 2 deletions test/Lily/Mock/MockSessionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class MockSessionStore
{
public $session = array();

public function set($request, $response)
public function set($response)
{
if (isset($response['session'])) {
$this->session = $response['session'] + $this->session;
$this->session = $response['session'];
}

return $response;
Expand Down
30 changes: 2 additions & 28 deletions test/Lily/Test/Middleware/Session/SessionStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,20 @@

abstract class SessionStoreTest extends \PHPUnit_Framework_TestCase
{
public function testItShouldAddArrayToSession()
public function testItShouldUseArrayAsSession()
{
$expectedValue = 'some value';
$response = $this->store()->set(array(), array(
$response = $this->store()->set(array(
'session' => array('a' => $expectedValue),
));
$this->assertSame($expectedValue, $this->getFromStore($response, 'a'));
}

public function testItShouldOverwriteArrayKeyInSession()
{
$expectedValue = 'overwritten value';
$request = $this->addToStore('b', 'initial value');
$response = $this->store()->set($request, array(
'session' => array('b' => $expectedValue),
));
$this->assertSame($expectedValue, $this->getFromStore($response, 'b'));
}

public function testItShouldNotOverwriteOtherKeys()
{
$expectedValue = 'other key value';
$request = $this->addToStore('a', $expectedValue);
$response = $this->store()->set($request, array(
'session' => array('b' => 'unrelated'),
));
$this->assertSame($expectedValue, $this->getFromStore($response, 'a'));
}

public function testItShouldGetArrayFromSession()
{
$expectedValue = 'something';
$request = $this->addToStore('c', $expectedValue);
$request = $this->store()->get($request);
$this->assertSame($expectedValue, $request['session']['c']);
}

public function testItShouldAlwaysAddSessionKey()
{
$request = $this->store()->get(array());
$this->assertArrayHasKey('session', $request);
}
}

0 comments on commit a9ece75

Please sign in to comment.