refactored session and added unit tests for manager and driver.
This commit is contained in:
@@ -1,156 +0,0 @@
|
||||
<?php
|
||||
|
||||
class AssetTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testContainerMethodReturnsContainer()
|
||||
{
|
||||
$asset = Laravel\IoC::resolve('laravel.asset');
|
||||
|
||||
$this->assertInstanceOf('Laravel\\Asset_Container', $asset->container());
|
||||
$this->assertInstanceOf('Laravel\\Asset_Container', $asset->container('footer'));
|
||||
|
||||
$this->assertEquals($asset->container()->name, 'default');
|
||||
$this->assertEquals($asset->container('footer')->name, 'footer');
|
||||
}
|
||||
|
||||
public function testAssetManagerMagicallyCallsDefaultContainer()
|
||||
{
|
||||
$asset = Laravel\IoC::resolve('laravel.asset');
|
||||
|
||||
$mock = $this->getMockBuilder('Laravel\\Asset_Container')->disableOriginalConstructor()->getMock();
|
||||
|
||||
$mock->expects($this->any())->method('styles')->will($this->returnValue('styles'));
|
||||
|
||||
$asset->containers['default'] = $mock;
|
||||
|
||||
$this->assertEquals($asset->styles(), 'styles');
|
||||
}
|
||||
|
||||
public function testAddMethodAddsAssetBasedOnExtension()
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
|
||||
$container->add('jquery', 'js/jquery.js');
|
||||
$container->add('jquery-css', 'css/jquery.css');
|
||||
|
||||
$this->assertEquals($container->assets['script']['jquery']['source'], 'js/jquery.js');
|
||||
$this->assertEquals($container->assets['style']['jquery-css']['source'], 'css/jquery.css');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider assetProvider
|
||||
*/
|
||||
public function testStyleMethodRegistersStylesheetAsset($type, $source, $attributes, $testAttributes)
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
|
||||
$dependencies = array('jquery');
|
||||
|
||||
$container->$type('reset', $source, $dependencies, $attributes);
|
||||
|
||||
$this->assertEquals($container->assets[$type]['reset']['source'], $source);
|
||||
$this->assertEquals($container->assets[$type]['reset']['dependencies'], $dependencies);
|
||||
$this->assertEquals($container->assets[$type]['reset']['attributes'], $testAttributes);
|
||||
}
|
||||
|
||||
public function assetProvider()
|
||||
{
|
||||
$attributes = array('test' => 'test');
|
||||
|
||||
return array(
|
||||
array('style', 'css/reset.css', $attributes, array_merge($attributes, array('media' => 'all'))),
|
||||
array('script', 'js/jquery.js', $attributes, $attributes),
|
||||
);
|
||||
}
|
||||
|
||||
public function testAllStylesCanBeRetrievedViaStylesMethod()
|
||||
{
|
||||
$container = new Laravel\Asset_Container('default', new HTMLAssetStub);
|
||||
|
||||
$container->style('reset', 'css/reset.css');
|
||||
$container->style('jquery', 'css/jquery.css');
|
||||
|
||||
$this->assertEquals($container->styles(), 'css/reset.css media:allcss/jquery.css media:all');
|
||||
}
|
||||
|
||||
public function testAllScriptsCanBeRetrievedViaScriptsMethod()
|
||||
{
|
||||
$container = new Laravel\Asset_Container('default', new HTMLAssetStub);
|
||||
|
||||
$container->script('jquery-ui', 'js/jquery-ui.js');
|
||||
$container->script('jquery', 'js/jquery.js', array(), array('test' => 'value'));
|
||||
|
||||
$this->assertEquals($container->scripts(), 'js/jquery-ui.js js/jquery.js test:value');
|
||||
}
|
||||
|
||||
public function testAssetsAreSortedBasedOnDependencies()
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
|
||||
$container->script('jquery', 'js/jquery.js', array('jquery-ui'));
|
||||
$container->script('jquery-ui', 'js/jquery-ui.js');
|
||||
|
||||
$scripts = $container->scripts();
|
||||
|
||||
$this->assertTrue(strpos($scripts, 'js/jquery-ui.js') < strpos($scripts, 'js/jquery.js'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
*/
|
||||
public function testAssetsCannotBeDependentOnSelf()
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
|
||||
$container->script('jquery', 'js/jquery.js', array('jquery'));
|
||||
|
||||
$container->scripts();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
*/
|
||||
public function testAssetDependenciesCannotBeCircular()
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
|
||||
$container->script('jquery', 'js/jquery.js', array('jquery-ui'));
|
||||
$container->script('jquery-ui', 'js/jquery-ui.js', array('jquery'));
|
||||
|
||||
$container->scripts();
|
||||
}
|
||||
|
||||
private function getContainer()
|
||||
{
|
||||
return new Laravel\Asset_Container('default', Laravel\IoC::resolve('laravel.html'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class HTMLAssetStub extends Laravel\HTML {
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
public function style($source, $attributes)
|
||||
{
|
||||
return $source.' '.$this->getAttributes($attributes);
|
||||
}
|
||||
|
||||
public function script($source, $attributes)
|
||||
{
|
||||
return $source.' '.$this->getAttributes($attributes);
|
||||
}
|
||||
|
||||
private function getAttributes($attributes)
|
||||
{
|
||||
$html = '';
|
||||
|
||||
foreach ($attributes as $key => $value)
|
||||
{
|
||||
$html .= $key.':'.$value;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -41,7 +41,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testConfigClassCanRetrieveItems()
|
||||
{
|
||||
$config = IoC::container()->config;
|
||||
$config = IoC::container()->resolve('laravel.config');
|
||||
|
||||
$this->assertTrue(is_array($config->get('application')));
|
||||
$this->assertEquals($config->get('application.url'), 'http://localhost');
|
||||
@@ -49,7 +49,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testGetMethodReturnsDefaultWhenItemDoesntExist()
|
||||
{
|
||||
$config = IoC::container()->config;
|
||||
$config = IoC::container()->resolve('laravel.config');
|
||||
|
||||
$this->assertNull($config->get('config.item'));
|
||||
$this->assertEquals($config->get('config.item', 'test'), 'test');
|
||||
@@ -58,7 +58,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testConfigClassCanSetItems()
|
||||
{
|
||||
$config = IoC::container()->config;
|
||||
$config = IoC::container()->resolve('laravel.config');
|
||||
|
||||
$config->set('application.names.test', 'test');
|
||||
$config->set('application.url', 'test');
|
||||
|
||||
346
tests/Session/SessionDriverTest.php
Normal file
346
tests/Session/SessionDriverTest.php
Normal file
@@ -0,0 +1,346 @@
|
||||
<?php
|
||||
|
||||
use Laravel\IoC;
|
||||
|
||||
class SessionDriverTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testStartMethodStartsNewSessionWhenNullIDGiven()
|
||||
{
|
||||
$driver = IoC::resolve('laravel.session.file');
|
||||
|
||||
$driver->start(IoC::resolve('laravel.config'), null);
|
||||
|
||||
$this->assertTrue(is_string($driver->session['id']));
|
||||
$this->assertEquals(strlen($driver->session['id']), 40);
|
||||
$this->assertTrue(is_array($driver->session['data']));
|
||||
$this->assertEquals(strlen($driver->session['data']['csrf_token']), 16);
|
||||
}
|
||||
|
||||
|
||||
public function testStartMethodCallsLoadWhenIDIsGiven()
|
||||
{
|
||||
$mock = $this->getFileDriverMock();
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('load')
|
||||
->with($this->equalTo('something'));
|
||||
|
||||
$mock->start(IoC::resolve('laravel.config'), 'something');
|
||||
}
|
||||
|
||||
|
||||
public function testSessionIsLoadedWhenIDIsValid()
|
||||
{
|
||||
$mock = $this->getFileDriverMock();
|
||||
|
||||
$time = time();
|
||||
|
||||
$session = array('id' => 'something', 'last_activity' => $time, 'data' => array('name' => 'Taylor', 'csrf_token' => 'token'));
|
||||
|
||||
$this->setMockLoadExpectations($mock, $session);
|
||||
|
||||
$mock->start(IoC::resolve('laravel.config'), 'something');
|
||||
|
||||
$this->assertEquals($mock->session['id'], 'something');
|
||||
$this->assertEquals($mock->session['last_activity'], $time);
|
||||
$this->assertEquals($mock->session['data'], array('name' => 'Taylor', 'csrf_token' => 'token'));
|
||||
}
|
||||
|
||||
|
||||
public function testSessionIsRestartedWhenLoadedSessionIsExpired()
|
||||
{
|
||||
$mock = $this->getFileDriverMock();
|
||||
|
||||
$time = new DateTime('2009-01-01');
|
||||
$time = $time->getTimestamp();
|
||||
|
||||
$session = array('id' => 'something', 'last_activity' => $time, 'data' => array('name' => 'Taylor'));
|
||||
|
||||
$this->setMockLoadExpectations($mock, $session);
|
||||
|
||||
$mock->start(IoC::resolve('laravel.config'), 'something');
|
||||
|
||||
$this->assertEquals(strlen($mock->session['id']), 40);
|
||||
$this->assertFalse(isset($mock->session['data']['name']));
|
||||
$this->assertTrue(isset($mock->session['data']['csrf_token']));
|
||||
}
|
||||
|
||||
|
||||
public function testHasMethodIndicatesIfItemExistsInSession()
|
||||
{
|
||||
$mock = $this->getSessionDriverWithData();
|
||||
|
||||
$this->assertTrue($mock->has('name'));
|
||||
$this->assertFalse($mock->has('test'));
|
||||
}
|
||||
|
||||
|
||||
public function testGetMethodGetsItemsFromTheSession()
|
||||
{
|
||||
$mock = $this->getSessionDriverWithData();
|
||||
|
||||
$this->assertNull($mock->get('test'));
|
||||
$this->assertEquals($mock->get('name'), 'Taylor');
|
||||
$this->assertEquals($mock->name, 'Taylor');
|
||||
$this->assertEquals($mock->get('test', 'Taylor'), 'Taylor');
|
||||
$this->assertEquals($mock->get('test', function() {return 'Taylor';}), 'Taylor');
|
||||
|
||||
$mock->session['data'][':old:test1'] = 'test1';
|
||||
$mock->session['data'][':new:test2'] = 'test2';
|
||||
|
||||
$this->assertEquals($mock->get('test1'), 'test1');
|
||||
$this->assertEquals($mock->get('test2'), 'test2');
|
||||
}
|
||||
|
||||
|
||||
public function testPutMethodPutsItemsInTheSession()
|
||||
{
|
||||
$mock = $this->getSessionDriverWithData();
|
||||
|
||||
$mock->put('name', 'Tony');
|
||||
$mock->age = 30;
|
||||
|
||||
$this->assertEquals($mock->session['data']['name'], 'Tony');
|
||||
$this->assertEquals($mock->session['data']['age'], 30);
|
||||
}
|
||||
|
||||
|
||||
public function testFlashMethodPutsItemsInFlashData()
|
||||
{
|
||||
$mock = $this->getSessionDriverWithData();
|
||||
|
||||
$mock->flash('name', 'James');
|
||||
|
||||
$this->assertEquals($mock->session['data'][':new:name'], 'James');
|
||||
}
|
||||
|
||||
|
||||
public function testKeepMethodRejuvenatesFlashData()
|
||||
{
|
||||
$mock = $this->getSessionDriverWithData();
|
||||
|
||||
$mock->session['data'][':old:test'] = 'test';
|
||||
$mock->keep('test');
|
||||
|
||||
$this->assertFalse(isset($mock->session['data'][':old:test']));
|
||||
$this->assertEquals($mock->session['data'][':new:test'], 'test');
|
||||
}
|
||||
|
||||
|
||||
public function testKeepMethodRejuvenatesAllFlashDataInArray()
|
||||
{
|
||||
$mock = $this->getSessionDriverWithData();
|
||||
|
||||
$mock->session['data'][':old:test1'] = 'test1';
|
||||
$mock->session['data'][':old:test2'] = 'test2';
|
||||
|
||||
$mock->keep(array('test1', 'test2'));
|
||||
|
||||
$this->assertFalse(isset($mock->session['data'][':old:test1']));
|
||||
$this->assertFalse(isset($mock->session['data'][':old:test2']));
|
||||
$this->assertEquals($mock->session['data'][':new:test1'], 'test1');
|
||||
$this->assertEquals($mock->session['data'][':new:test2'], 'test2');
|
||||
}
|
||||
|
||||
|
||||
public function testReflashMethodRejuvenatesAllFlashData()
|
||||
{
|
||||
$mock = $this->getSessionDriverWithData();
|
||||
|
||||
$mock->session['data'][':old:test1'] = 'test1';
|
||||
$mock->session['data'][':old:test2'] = 'test2';
|
||||
|
||||
$mock->reflash();
|
||||
|
||||
$this->assertFalse(isset($mock->session['data'][':old:test1']));
|
||||
$this->assertFalse(isset($mock->session['data'][':old:test2']));
|
||||
$this->assertEquals($mock->session['data'][':new:test1'], 'test1');
|
||||
$this->assertEquals($mock->session['data'][':new:test2'], 'test2');
|
||||
}
|
||||
|
||||
|
||||
public function testForgetMethodRemovesDataFromSession()
|
||||
{
|
||||
$mock = $this->getSessionDriverWithData();
|
||||
|
||||
$mock->forget('name');
|
||||
|
||||
$this->assertFalse(isset($mock->session['data']['name']));
|
||||
}
|
||||
|
||||
|
||||
public function testFlushMethodsClearsEntireSessionData()
|
||||
{
|
||||
$mock = $this->getSessionDriverWithData();
|
||||
|
||||
$mock->flush();
|
||||
|
||||
$this->assertEquals(count($mock->session['data']), 0);
|
||||
}
|
||||
|
||||
|
||||
public function testRegenerateMethodDeletesSessionAndResetsID()
|
||||
{
|
||||
$mock = $this->getMock('Laravel\\Session\\Drivers\\File', array('load', 'delete'), $this->getFileDriverConstructor());
|
||||
|
||||
$this->setMockLoadExpectations($mock, $this->getDummySession());
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('delete')
|
||||
->with($this->equalTo('something'));
|
||||
|
||||
$mock->start(IoC::resolve('laravel.config'), 'something');
|
||||
|
||||
$mock->regenerate();
|
||||
|
||||
$this->assertEquals(strlen($mock->session['id']), 40);
|
||||
}
|
||||
|
||||
|
||||
public function testCloseMethodFlashesOldInputData()
|
||||
{
|
||||
$mock = $this->getMock('Laravel\\Session\\Drivers\\File', array('save'), $this->getFileDriverConstructor());
|
||||
|
||||
$this->setMockLoadExpectations($mock, $this->getDummySession());
|
||||
|
||||
$mock->start(IoC::resolve('laravel.config'), 'something');
|
||||
|
||||
$mock->close(new InputStub, time());
|
||||
|
||||
$this->assertEquals($mock->session['data'][':old:laravel_old_input'], array('name' => 'Taylor'));
|
||||
}
|
||||
|
||||
|
||||
public function testCloseMethodAgesFlashData()
|
||||
{
|
||||
$mock = $this->getSessionDriverWithData();
|
||||
|
||||
$mock->session['data'][':old:old'] = 'old';
|
||||
$mock->flash('flash', 'flash');
|
||||
|
||||
$mock->close(new InputStub, time());
|
||||
|
||||
$this->assertFalse(isset($mock->session['data'][':old:old']));
|
||||
$this->assertFalse(isset($mock->session['data'][':new:flash']));
|
||||
$this->assertEquals($mock->session['data'][':old:flash'], 'flash');
|
||||
}
|
||||
|
||||
|
||||
public function testCloseMethodSavesSession()
|
||||
{
|
||||
$mock = $this->getMock('Laravel\\Session\\Drivers\\File', array('load', 'save', 'sweep'), $this->getFileDriverConstructor());
|
||||
|
||||
$session = $this->getDummySession();
|
||||
|
||||
$session['data']['csrf_token'] = 'token';
|
||||
|
||||
$this->setMockLoadExpectations($mock, $session);
|
||||
|
||||
$expect = $session;
|
||||
|
||||
Laravel\Arr::set($expect, 'data.:old:laravel_old_input', array('name' => 'Taylor'));
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('save')
|
||||
->with($this->equalTo($expect));
|
||||
|
||||
$mock->start(IoC::resolve('laravel.config'), 'something');
|
||||
|
||||
$mock->close(new InputStub, $mock->session['last_activity']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider cookieMethodProvider
|
||||
*/
|
||||
public function testCookieMethodWritesCookie($expire_on_close, $minutes)
|
||||
{
|
||||
$mock = $this->getSessionDriverWithData();
|
||||
|
||||
$config = IoC::resolve('laravel.config');
|
||||
|
||||
$config->set('session.expire_on_close', $expire_on_close);
|
||||
|
||||
$mock->start($config, 'something');
|
||||
|
||||
$cookieMock = $this->getMock('Laravel\\Cookie', array('put'), array(array()));
|
||||
|
||||
$cookieMock->expects($this->once())
|
||||
->method('put')
|
||||
->with('laravel_session', 'something', $minutes, $config->get('session.path'), $config->get('session.domain'));
|
||||
|
||||
$mock->cookie($cookieMock);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------
|
||||
// Utility Methods & Providers
|
||||
// -----------------------------------------------------------------------------------
|
||||
|
||||
public function getSessionDriverWithData()
|
||||
{
|
||||
$mock = $this->getFileDriverMock();
|
||||
|
||||
$this->setMockLoadExpectations($mock, $this->getDummySession());
|
||||
|
||||
$mock->start(IoC::resolve('laravel.config'), 'something');
|
||||
|
||||
return $mock;
|
||||
}
|
||||
|
||||
|
||||
private function getFileDriverMock()
|
||||
{
|
||||
return $this->getMock('Laravel\\Session\\Drivers\\File', array('load', 'save'), $this->getFileDriverConstructor());
|
||||
}
|
||||
|
||||
|
||||
private function getFileDriverConstructor()
|
||||
{
|
||||
return array(IoC::resolve('laravel.file'), null);
|
||||
}
|
||||
|
||||
|
||||
private function setMockLoadExpectations($mock, $session)
|
||||
{
|
||||
$mock->expects($this->any())
|
||||
->method('load')
|
||||
->will($this->returnValue($session));
|
||||
}
|
||||
|
||||
|
||||
private function getDummySession()
|
||||
{
|
||||
return array('id' => 'something', 'last_activity' => time(), 'data' => array('name' => 'Taylor'));
|
||||
}
|
||||
|
||||
|
||||
public function cookieMethodProvider()
|
||||
{
|
||||
return array(
|
||||
array(false, 60),
|
||||
array(true, 0),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------
|
||||
// Stub Classes
|
||||
// -----------------------------------------------------------------------------------
|
||||
|
||||
class InputStub extends Laravel\Input {
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
public function get($key = null, $default = null)
|
||||
{
|
||||
return array('name' => 'Taylor');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class CookieStub extends Laravel\Cookie {
|
||||
|
||||
public function put() {}
|
||||
|
||||
}
|
||||
29
tests/Session/SessionManagerTest.php
Normal file
29
tests/Session/SessionManagerTest.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
class SessionManagerTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testDriverMethodReturnsDriverWhenOneIsRegistered()
|
||||
{
|
||||
$dependencies = array(
|
||||
'laravel.session.test' => array('resolver' => function($container)
|
||||
{
|
||||
return new stdClass;
|
||||
})
|
||||
);
|
||||
|
||||
$manager = new Laravel\Session\Manager(new Laravel\Container($dependencies));
|
||||
|
||||
$this->assertInstanceOf('stdClass', $manager->driver('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
*/
|
||||
public function testDriverMethodThrowsExceptionForUndefinedDriver()
|
||||
{
|
||||
$manager = new Laravel\Session\Manager(new Laravel\Container(array()));
|
||||
|
||||
$manager->driver('test');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user