123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Tests\Component\HttpFoundation;
- use Symfony\Component\HttpFoundation\Session;
- use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface;
- /**
- * SessionTest
- *
- * @author Robert Schönthal <seroscho@googlemail.com>
- */
- class SessionTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @dataProvider provider
- */
- public function testFlash($s)
- {
- $this->assertFalse($s->hasFlash('foo'));
-
- $s->setFlash('foo', 'bar');
-
- $this->assertTrue($s->hasFlash('foo'));
- $this->assertSame('bar',$s->getFlash('foo'));
-
- $s->removeFlash('foo');
-
- $this->assertFalse($s->hasFlash('foo'));
-
- $flashes = array('foo'=>'bar','bar'=>'foo');
-
- $s->restart();
- $s->setFlashes($flashes);
-
- $this->assertSame($flashes,$s->getFlashes());
-
- $s->clearFlashes();
-
- $this->assertSame(array(),$s->getFlashes());
- }
-
- /**
- * @dataProvider provider
- */
- public function testAttribute($s)
- {
- $this->assertFalse($s->has('foo'));
- $this->assertNull($s->get('foo'));
-
- $s->set('foo', 'bar');
-
- $this->assertTrue($s->has('foo'));
- $this->assertSame('bar',$s->get('foo'));
-
- $s->restart();
-
- $s->remove('foo');
- $s->set('foo', 'bar');
-
- $s->remove('foo');
-
- $this->assertFalse($s->has('foo'));
-
- $attrs = array('foo'=>'bar','bar'=>'foo');
-
- $s->restart();
-
- $s->setAttributes($attrs);
-
- $this->assertSame($attrs,$s->getAttributes());
-
- $s->clear();
-
- $this->assertSame(array(),$s->getAttributes());
- }
-
- /**
- * @dataProvider provider
- */
- public function testMigrateAndInvalidate($s)
- {
- $s->set('foo', 'bar');
- $s->setFlash('foo', 'bar');
- $this->assertSame('bar',$s->get('foo'));
- $this->assertSame('bar',$s->getFlash('foo'));
-
- $s->migrate();
-
- $this->assertSame('bar',$s->get('foo'));
- $this->assertSame('bar',$s->getFlash('foo'));
-
- $s->restart();
- $s->invalidate();
-
- $this->assertSame(array(),$s->getAttributes());
- $this->assertSame(array(),$s->getFlashes());
- }
-
- public function testSerialize()
- {
- $storage = new SessionTestStorage();
- $options = array('foo'=>'bar');
- $s = new Session($storage,$options);
-
- $compare = serialize(array($storage, $options));
-
- $this->assertSame($compare, $s->serialize());
-
- $s->unserialize($compare);
-
- $_options = new \ReflectionProperty(get_class($s),'options');
- $_options->setAccessible(true);
-
- $_storage = new \ReflectionProperty(get_class($s),'storage');
- $_storage->setAccessible(true);
-
- $this->assertEquals($_options->getValue($s),$options,'options match');
- $this->assertEquals($_storage->getValue($s),$storage,'storage match');
- }
-
- public function testSave()
- {
- $storage = new SessionTestStorage();
- $options = array('foo'=>'bar');
- $s = new Session($storage,$options);
- $s->set('foo','bar');
-
- $s->save();
- $compare = array('_symfony2'=>array('_flash'=>array(),'_locale'=>'en','foo'=>'bar'));
-
- $this->assertSame($storage->attrs,$compare);
- }
-
- /**
- * @dataProvider provider
- */
- public function testLocale($s)
- {
- $this->assertSame('en',$s->getLocale(),'default locale is en');
-
- $s->set('_locale','de');
-
- $this->assertSame('de',$s->getLocale(),'locale is de');
- $s->restart();
- $s->setLocale('fr');
- $this->assertSame('fr',$s->getLocale(),'locale is fr');
- }
-
- /**
- * @dataProvider provider
- */
- public function testGetId($s)
- {
- $this->assertSame('foo',$s->getId());
- }
-
- /**
- * @dataProvider provider
- */
- public function testStart($s)
- {
- $s->start();
-
- $this->assertSame('en',$s->getLocale());
- $this->assertSame(array(),$s->getFlashes());
- $this->assertSame(array('_flash'=>array(),'_locale'=>'en'),$s->getAttributes());
- $s->start();
- $this->assertSame('en',$s->getLocale());
- }
-
-
- public function provider()
- {
- $storage = new SessionTestStorage();
- $session = new SessionTestSession($storage);
-
- return array(
- array($session)
- );
- }
- }
- class SessionTestSession extends Session
- {
- /**
- * Little helper for simulating a fresh session.
- */
- public function restart()
- {
- $this->started = false;
- }
- }
- class SessionTestStorage implements SessionStorageInterface
- {
- public $attrs;
-
- function start()
- {
- }
-
- function getId()
- {
- return 'foo';
- }
- function read($key)
- {
- }
-
- function remove($key)
- {
- }
-
- function write($key, $data)
- {
- $this->attrs[$key] = $data;
- }
-
- function regenerate($destroy = false)
- {
- }
- }
|