-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
lazy session start for #6036 without bc #10310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/Symfony/Component/HttpFoundation/Session/Storage/LazyMockArraySessionStorage.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* @author Bulat Shakirzyanov <[email protected]> | ||
* @author Drak <[email protected]> | ||
*/ | ||
class LazyMockArraySessionStorage extends MockArraySessionStorage implements LazySessionStorageInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasPreviousSession() | ||
{ | ||
return !empty($this->id); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/HttpFoundation/Session/Storage/LazySessionStorageInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* @author Drak <[email protected]> | ||
* | ||
* @api | ||
*/ | ||
interface LazySessionStorageInterface | ||
{ | ||
/** | ||
* Checks that storage has previous session | ||
* | ||
* @return boolean True if previous session exiting, false otherwise. | ||
*/ | ||
public function hasPreviousSession(); | ||
} |
158 changes: 158 additions & 0 deletions
158
src/Symfony/Component/HttpFoundation/Tests/Session/SessionLazyStartTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* @author Robert Schönthal <[email protected]> | ||
* @author Drak <[email protected]> | ||
*/ | ||
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()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isExists
is a weird name. You have 2 verbs in this nameThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any suggestions? I think about this name and haven't better name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exists
,has
,isset
,isDefined
, ... ? It depends on what this method does