PdoSessionStorageTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
  11. use Symfony\Component\HttpFoundation\SessionStorage\PdoSessionStorage;
  12. class PdoSessionStorageTest extends \PHPUnit_Framework_TestCase
  13. {
  14. private $pdo;
  15. protected function setUp()
  16. {
  17. $this->pdo = new \PDO("sqlite::memory:");
  18. $sql = "CREATE TABLE sessions (sess_id VARCHAR(255) PRIMARY KEY, sess_data TEXT, sess_time INTEGER)";
  19. $this->pdo->exec($sql);
  20. }
  21. public function testMultipleInstances()
  22. {
  23. $storage1 = new PdoSessionStorage($this->pdo, array(), array('db_table' => 'sessions'));
  24. $storage1->sessionWrite('foo', 'bar');
  25. $storage2 = new PdoSessionStorage($this->pdo, array(), array('db_table' => 'sessions'));
  26. $this->assertEquals('bar', $storage2->sessionRead('foo'), 'values persist between instances');
  27. }
  28. public function testSessionDestroy()
  29. {
  30. $storage = new PdoSessionStorage($this->pdo, array(), array('db_table' => 'sessions'));
  31. $storage->sessionWrite('foo', 'bar');
  32. $this->assertEquals(1, count($this->pdo->query('SELECT * FROM sessions')->fetchAll()));
  33. $storage->sessionDestroy('foo');
  34. $this->assertEquals(0, count($this->pdo->query('SELECT * FROM sessions')->fetchAll()));
  35. }
  36. public function testSessionGC()
  37. {
  38. $storage = new PdoSessionStorage($this->pdo, array(), array('db_table' => 'sessions'));
  39. $storage->sessionWrite('foo', 'bar');
  40. $storage->sessionWrite('baz', 'bar');
  41. $this->assertEquals(2, count($this->pdo->query('SELECT * FROM sessions')->fetchAll()));
  42. $storage->sessionGC(-1);
  43. $this->assertEquals(0, count($this->pdo->query('SELECT * FROM sessions')->fetchAll()));
  44. }
  45. }