| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 | <?php
/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.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\ArraySessionStorage;
/**
 * SessionTest
 *
 * @author Fabien Potencier <fabien@symfony.com>
 * @author Robert Schönthal <seroscho@googlemail.com>
 */
class SessionTest extends \PHPUnit_Framework_TestCase
{
    protected $storage;
    protected $session;
    public function setUp()
    {
        $this->storage = new ArraySessionStorage();
        $this->session = $this->getSession();
    }
    protected function tearDown()
    {
        $this->storage = null;
        $this->session = null;
    }
    public function testFlash()
    {
        $this->session->clearFlashes();
        $this->assertSame(array(), $this->session->getFlashes());
        $this->assertFalse($this->session->hasFlash('foo'));
        $this->session->setFlash('foo', 'bar');
        $this->assertTrue($this->session->hasFlash('foo'));
        $this->assertSame('bar', $this->session->getFlash('foo'));
        $this->session->removeFlash('foo');
        $this->assertFalse($this->session->hasFlash('foo'));
        $flashes = array('foo' => 'bar', 'bar' => 'foo');
        $this->session->setFlashes($flashes);
        $this->assertSame($flashes, $this->session->getFlashes());
    }
    public function testFlashesAreFlushedWhenNeeded()
    {
        $this->session->setFlash('foo', 'bar');
        $this->session->save();
        $this->session = $this->getSession();
        $this->assertTrue($this->session->hasFlash('foo'));
        $this->session->save();
        $this->session = $this->getSession();
        $this->assertFalse($this->session->hasFlash('foo'));
    }
    public function testAll()
    {
        $this->assertFalse($this->session->has('foo'));
        $this->assertNull($this->session->get('foo'));
        $this->session->set('foo', 'bar');
        $this->assertTrue($this->session->has('foo'));
        $this->assertSame('bar', $this->session->get('foo'));
        $this->session = $this->getSession();
        $this->session->remove('foo');
        $this->session->set('foo', 'bar');
        $this->session->remove('foo');
        $this->assertFalse($this->session->has('foo'));
        $attrs = array('foo' => 'bar', 'bar' => 'foo');
        $this->session = $this->getSession();
        $this->session->replace($attrs);
        $this->assertSame($attrs, $this->session->all());
        $this->session->clear();
        $this->assertSame(array(), $this->session->all());
    }
    public function testMigrateAndInvalidate()
    {
        $this->session->set('foo', 'bar');
        $this->session->setFlash('foo', 'bar');
        $this->assertSame('bar', $this->session->get('foo'));
        $this->assertSame('bar', $this->session->getFlash('foo'));
        $this->session->migrate();
        $this->assertSame('bar', $this->session->get('foo'));
        $this->assertSame('bar', $this->session->getFlash('foo'));
        $this->session = $this->getSession();
        $this->session->invalidate();
        $this->assertSame(array(), $this->session->all());
        $this->assertSame(array(), $this->session->getFlashes());
    }
    public function testSerialize()
    {
        $defaultLocale = 'en';
        $this->session = new Session($this->storage, $defaultLocale);
        $compare = serialize(array($this->storage, $defaultLocale));
        $this->assertSame($compare, $this->session->serialize());
        $this->session->unserialize($compare);
        $_defaultLocale = new \ReflectionProperty(get_class($this->session), 'defaultLocale');
        $_defaultLocale->setAccessible(true);
        $_storage = new \ReflectionProperty(get_class($this->session), 'storage');
        $_storage->setAccessible(true);
        $this->assertEquals($_defaultLocale->getValue($this->session), $defaultLocale, 'options match');
        $this->assertEquals($_storage->getValue($this->session), $this->storage, 'storage match');
    }
    public function testSave()
    {
        $this->storage = new ArraySessionStorage();
        $defaultLocale = 'fr';
        $this->session = new Session($this->storage, $defaultLocale);
        $this->session->set('foo', 'bar');
        $this->session->save();
        $compare = array('_symfony2' => array('attributes' => array('foo' => 'bar'), 'flashes' => array(), 'locale' => 'fr'));
        $r = new \ReflectionObject($this->storage);
        $p = $r->getProperty('data');
        $p->setAccessible(true);
        $this->assertSame($p->getValue($this->storage), $compare);
    }
    public function testLocale()
    {
        if (!extension_loaded('intl')) {
            $this->markTestSkipped('The "intl" extension is not available');
        }
        $this->assertSame('en', $this->session->getLocale(), 'default locale is en');
        $this->assertSame('en', \Locale::getDefault(), '\Locale::getDefault() is en');
        $this->session->setLocale('de');
        $this->assertSame('de', $this->session->getLocale(), 'locale is de');
        $this->assertSame('de', \Locale::getDefault(), '\Locale::getDefault() is de');
        $this->session = $this->getSession();
        $this->session->setLocale('fr');
        $this->assertSame('fr', $this->session->getLocale(), 'locale is fr');
        $this->assertSame('fr', \Locale::getDefault(), '\Locale::getDefault() is fr');
    }
    public function testLocaleAfterClear()
    {
        $this->session->clear();
        $this->assertEquals('en', $this->session->getLocale());
    }
    public function testGetId()
    {
        $this->assertSame(null, $this->session->getId());
    }
    public function testStart()
    {
        $this->session->start();
        $this->assertSame('en', $this->session->getLocale());
        $this->assertSame('en', \Locale::getDefault());
        $this->assertSame(array(), $this->session->getFlashes());
        $this->assertSame(array(), $this->session->all());
    }
    public function testSavedOnDestruct()
    {
        $this->session->set('foo', 'bar');
        $this->session->__destruct();
        $expected = array(
            'attributes'=>array('foo'=>'bar'),
            'flashes'=>array(),
            'locale'=>'en'
        );
        $saved = $this->storage->read('_symfony2');
        $this->assertSame($expected, $saved);
    }
    public function testSavedOnDestructAfterManualSave()
    {
        $this->session->set('foo', 'nothing');
        $this->session->save();
        $this->session->set('foo', 'bar');
        $this->session->__destruct();
        $expected = array(
            'attributes'=>array('foo'=>'bar'),
            'flashes'=>array(),
            'locale'=>'en'
        );
        $saved = $this->storage->read('_symfony2');
        $this->assertSame($expected, $saved);
    }
    public function testStorageRegenerate()
    {
        $this->storage->write('foo', 'bar');
        $this->assertTrue($this->storage->regenerate());
        $this->assertEquals('bar', $this->storage->read('foo'));
        $this->assertTrue($this->storage->regenerate(true));
        $this->assertNull($this->storage->read('foo'));
    }
    public function testStorageRemove()
    {
        $this->storage->write('foo', 'bar');
        $this->assertEquals('bar', $this->storage->read('foo'));
        $this->storage->remove('foo');
        $this->assertNull($this->storage->read('foo'));
    }
    protected function getSession()
    {
        return new Session($this->storage);
    }
}
 |