Skip to content

Commit

Permalink
Improve Lily\Middleware\Session tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemorton committed Mar 9, 2014
1 parent 6ae2485 commit d126781
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 26 deletions.
4 changes: 2 additions & 2 deletions test/Lily/Mock/MockSessionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

class MockSessionStore
{
public $session;
public $session = array();

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

return $response;
Expand Down
102 changes: 78 additions & 24 deletions test/Lily/Test/Middleware/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,98 @@

class SessionTest extends \PHPUnit_Framework_TestCase
{
public function testWriteToSession()
private function wrapWithSessionMiddleware($store, $handler)
{
$mw = new Session(compact('store'));
return $mw($handler);
}

private function callHandler($handler)
{
return $handler(array());
}

public function testItShouldAddArrayToSession()
{
$store = new MockSessionStore;
$expectedValue = 'some value';

$expectedSession = array('a' => 1);
$this->callHandler(
$this->wrapWithSessionMiddleware(
$store,
function ($request) use (& $expectedValue) {
return array(
'session' => array('a' => $expectedValue),
);
}));

$mw = new Session(compact('store'));
$this->assertSame($expectedValue, $store->session['a']);
}

$wrappedHandler =
$mw(
function () use ($expectedSession) {
$session = $expectedSession;
return Res::ok() + compact('session');
});
public function testItShouldOverwriteArrayKeyInSession()
{
$store = new MockSessionStore;
$store->session['b'] = 'initial value';
$expectedValue = 'overwritten value';

$wrappedHandler(Req::get('/'));
$this->callHandler(
$this->wrapWithSessionMiddleware(
$store,
function ($request) use (& $expectedValue) {
return array(
'session' => array('b' => $expectedValue),
);
}));

$this->assertSame($expectedSession, $store->session);
$this->assertSame($expectedValue, $store->session['b']);
}

public function testReadSession()
public function testItShouldNotOverwriteOtherKeys()
{
$expectedValue = 'other key value';
$store = new MockSessionStore;
$store->session = array('b' => 2);
$store->session['a'] = $expectedValue;

$actualSession = NULL;
$this->callHandler(
$this->wrapWithSessionMiddleware(
$store,
function ($request) {
return array(
'session' => array('b' => 'unrelated'),
);
}));

$mw = new Session(compact('store'));

$wrappedHandler =
$mw(
function ($request) use (& $actualSession) {
$actualSession = $request['session'];
return Res::ok();
});
$this->assertSame($expectedValue, $store->session['a']);
}

public function testItShouldGetArrayFromSession()
{
$expectedValue = 'something';
$actualValue = NULL;
$store = new MockSessionStore;
$store->session['c'] = $expectedValue;

$this->callHandler(
$this->wrapWithSessionMiddleware(
$store,
function ($request) use (& $actualValue) {
$actualValue = $request['session']['c'];
}));

$this->assertSame($expectedValue, $actualValue);
}

public function testItShouldAlwaysAddSessionKey()
{
$sessionKeyIsSet = FALSE;

$wrappedHandler(Req::get('/'));
$this->callHandler(
$this->wrapWithSessionMiddleware(
new MockSessionStore,
function ($request) use (& $sessionKeyIsSet) {
$sessionKeyIsSet = isset($request['session']);
}));

$this->assertSame($store->session, $actualSession);
$this->assertTrue($sessionKeyIsSet);
}
}

0 comments on commit d126781

Please sign in to comment.