diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index ac626fd58998d..dd050c77947d5 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -11,6 +11,7 @@ namespace Symfony\Component\HttpFoundation\Session; +use Symfony\Component\HttpFoundation\Session\Storage\LazySessionStorageInterface; use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; @@ -78,6 +79,10 @@ public function start() */ public function has($name) { + if (!$this->isExists()) { + return false; + } + return $this->storage->getBag($this->attributeName)->has($name); } @@ -86,6 +91,10 @@ public function has($name) */ public function get($name, $default = null) { + if (!$this->isExists()) { + return $default; + } + return $this->storage->getBag($this->attributeName)->get($name, $default); } @@ -102,6 +111,10 @@ public function set($name, $value) */ public function all() { + if (!$this->isExists()) { + return array(); + } + return $this->storage->getBag($this->attributeName)->all(); } @@ -154,6 +167,10 @@ public function getIterator() */ public function count() { + if (!$this->isExists()) { + return 0; + } + return count($this->storage->getBag($this->attributeName)->all()); } @@ -248,4 +265,17 @@ public function getFlashBag() { return $this->getBag($this->flashName); } + + /** + * Is Session exists + * + * @return boolean + */ + private function isExists() + { + return + $this->isStarted() + || !($this->storage instanceof LazySessionStorageInterface) + || $this->storage->hasPreviousSession(); + } } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/LazyMockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/LazyMockArraySessionStorage.php new file mode 100644 index 0000000000000..fd1b94754bfa0 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/LazyMockArraySessionStorage.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\Session\Storage; + +/** + * LazyMockArraySessionStorage mocks the session for unit tests. + * + * No PHP session is actually started since a session can be initialized + * and shutdown only once per PHP execution cycle. + * + * When doing functional testing, you should use MockFileSessionStorage instead. + * + * @author Fabien Potencier + * @author Bulat Shakirzyanov + * @author Drak + */ +class LazyMockArraySessionStorage extends MockArraySessionStorage implements LazySessionStorageInterface +{ + /** + * {@inheritdoc} + */ + public function hasPreviousSession() + { + return !empty($this->id); + } +} diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/LazySessionStorageInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/LazySessionStorageInterface.php new file mode 100644 index 0000000000000..2ae433c060be4 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/LazySessionStorageInterface.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\Session\Storage; + +/** + * LazyStorageInterface. + * + * @author Fabien Potencier + * @author Drak + * + * @api + */ +interface LazySessionStorageInterface +{ + /** + * Checks that storage has previous session + * + * @return boolean True if previous session exiting, false otherwise. + */ + public function hasPreviousSession(); +} diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionLazyStartTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionLazyStartTest.php new file mode 100644 index 0000000000000..774c09d79d694 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionLazyStartTest.php @@ -0,0 +1,158 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\Tests\Session; + +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; +use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; +use Symfony\Component\HttpFoundation\Session\Storage\LazyMockArraySessionStorage; + +/** + * LazySessionTest + * + * @author Fabien Potencier + * @author Robert Schönthal + * @author Drak + */ +class SessionLazyStartTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface + */ + protected $storage; + + /** + * @var \Symfony\Component\HttpFoundation\Session\SessionInterface + */ + protected $session; + + protected function setUp() + { + $this->storage = new LazyMockArraySessionStorage(); + $this->session = new Session($this->storage, new AttributeBag(), new FlashBag()); + } + + protected function tearDown() + { + $this->storage = null; + $this->session = null; + } + + public function testGet() + { + // tests defaults + $this->assertNull($this->session->get('foo')); + $this->assertEquals(1, $this->session->get('foo', 1)); + $this->assertFalse($this->session->isStarted()); + } + + /** + * @dataProvider setProvider + */ + public function testSet($key, $value) + { + $this->session->set($key, $value); + $this->assertEquals($value, $this->session->get($key)); + $this->assertTrue($this->session->isStarted()); + } + + /** + * @dataProvider setProvider + */ + public function testHas($key, $value) + { + $this->assertFalse($this->session->has($key.'_lazy_start')); + $this->assertFalse($this->session->isStarted()); + } + + public function testReplace() + { + $this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome')); + $this->assertTrue($this->session->isStarted()); + } + + /** + * @dataProvider setProvider + */ + public function testAll($key, $value, $result) + { + $this->assertEquals(array(), $this->session->all()); + $this->session->all(); + $this->assertFalse($this->session->isStarted()); + } + + /** + * @dataProvider setProvider + */ + public function testClear($key, $value) + { + $this->session->set('hi', 'fabien'); + $this->session->set($key, $value); + $this->session->clear(); + $this->assertEquals(array(), $this->session->all()); + $this->assertTrue($this->session->isStarted()); + } + + public function setProvider() + { + return array( + array('foo', 'bar', array('foo' => 'bar')), + array('foo.bar', 'too much beer', array('foo.bar' => 'too much beer')), + array('great', 'symfony2 is great', array('great' => 'symfony2 is great')), + ); + } + + /** + * @dataProvider setProvider + */ + public function testRemove($key, $value) + { + $this->session->set('hi.world', 'have a nice day'); + $this->session->set($key, $value); + $this->session->remove($key); + $this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all()); + $this->assertTrue($this->session->isStarted()); + } + + public function testInvalidate() + { + $this->session->set('invalidate', 123); + $this->session->invalidate(); + $this->assertEquals(array(), $this->session->all()); + $this->assertTrue($this->session->isStarted()); + } + + public function testMigrate() + { + $this->session->set('migrate', 321); + $this->session->migrate(); + $this->assertEquals(321, $this->session->get('migrate')); + $this->assertTrue($this->session->isStarted()); + } + + public function testMigrateDestroy() + { + $this->session->set('migrate', 333); + $this->session->migrate(true); + $this->assertEquals(333, $this->session->get('migrate')); + $this->assertTrue($this->session->isStarted()); + } + + /** + * @covers \Symfony\Component\HttpFoundation\Session\Session::count + */ + public function testGetCount() + { + $this->assertEquals(0, count($this->session)); + $this->assertFalse($this->session->isStarted()); + } +} diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php index 56015fdf5ab75..ccdf9a7302080 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php @@ -82,6 +82,7 @@ public function testGet() // tests defaults $this->assertNull($this->session->get('foo')); $this->assertEquals(1, $this->session->get('foo', 1)); + $this->assertTrue($this->session->isStarted()); } /** @@ -91,6 +92,7 @@ public function testSet($key, $value) { $this->session->set($key, $value); $this->assertEquals($value, $this->session->get($key)); + $this->assertTrue($this->session->isStarted()); } /** @@ -98,6 +100,9 @@ public function testSet($key, $value) */ public function testHas($key, $value) { + $this->assertFalse($this->session->has($key.'_lazy_start')); + $this->assertTrue($this->session->isStarted()); + $this->session->set($key, $value); $this->assertTrue($this->session->has($key)); $this->assertFalse($this->session->has($key.'non_value')); @@ -106,6 +111,7 @@ public function testHas($key, $value) public function testReplace() { $this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome')); + $this->assertTrue($this->session->isStarted()); $this->assertEquals(array('happiness' => 'be good', 'symfony' => 'awesome'), $this->session->all()); $this->session->replace(array()); $this->assertEquals(array(), $this->session->all()); @@ -116,6 +122,10 @@ public function testReplace() */ public function testAll($key, $value, $result) { + $this->assertEquals(array(), $this->session->all()); + $this->session->all(); + $this->assertTrue($this->session->isStarted()); + $this->session->set($key, $value); $this->assertEquals($result, $this->session->all()); } @@ -129,6 +139,7 @@ public function testClear($key, $value) $this->session->set($key, $value); $this->session->clear(); $this->assertEquals(array(), $this->session->all()); + $this->assertTrue($this->session->isStarted()); } public function setProvider() @@ -149,6 +160,7 @@ public function testRemove($key, $value) $this->session->set($key, $value); $this->session->remove($key); $this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all()); + $this->assertTrue($this->session->isStarted()); } public function testInvalidate() @@ -156,6 +168,7 @@ public function testInvalidate() $this->session->set('invalidate', 123); $this->session->invalidate(); $this->assertEquals(array(), $this->session->all()); + $this->assertTrue($this->session->isStarted()); } public function testMigrate() @@ -163,6 +176,7 @@ public function testMigrate() $this->session->set('migrate', 321); $this->session->migrate(); $this->assertEquals(321, $this->session->get('migrate')); + $this->assertTrue($this->session->isStarted()); } public function testMigrateDestroy() @@ -170,6 +184,7 @@ public function testMigrateDestroy() $this->session->set('migrate', 333); $this->session->migrate(true); $this->assertEquals(333, $this->session->get('migrate')); + $this->assertTrue($this->session->isStarted()); } public function testSave() @@ -214,6 +229,9 @@ public function testGetIterator() */ public function testGetCount() { + $this->assertEquals(0, count($this->session)); + $this->assertTrue($this->session->isStarted()); + $this->session->set('hello', 'world'); $this->session->set('symfony2', 'rocks');