LegacyPdoSessionHandlerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\Component\HttpFoundation\Tests\Session\Storage\Handler;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Session\Storage\Handler\LegacyPdoSessionHandler;
  13. /**
  14. * @group legacy
  15. * @group time-sensitive
  16. * @requires extension pdo_sqlite
  17. */
  18. class LegacyPdoSessionHandlerTest extends TestCase
  19. {
  20. private $pdo;
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $this->pdo = new \PDO('sqlite::memory:');
  25. $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  26. $sql = 'CREATE TABLE sessions (sess_id VARCHAR(128) PRIMARY KEY, sess_data TEXT, sess_time INTEGER)';
  27. $this->pdo->exec($sql);
  28. }
  29. public function testIncompleteOptions()
  30. {
  31. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
  32. $storage = new LegacyPdoSessionHandler($this->pdo, array());
  33. }
  34. public function testWrongPdoErrMode()
  35. {
  36. $pdo = new \PDO('sqlite::memory:');
  37. $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
  38. $pdo->exec('CREATE TABLE sessions (sess_id VARCHAR(128) PRIMARY KEY, sess_data TEXT, sess_time INTEGER)');
  39. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
  40. $storage = new LegacyPdoSessionHandler($pdo, array('db_table' => 'sessions'));
  41. }
  42. public function testWrongTableOptionsWrite()
  43. {
  44. $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'bad_name'));
  45. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');
  46. $storage->write('foo', 'bar');
  47. }
  48. public function testWrongTableOptionsRead()
  49. {
  50. $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'bad_name'));
  51. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');
  52. $storage->read('foo');
  53. }
  54. public function testWriteRead()
  55. {
  56. $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
  57. $storage->write('foo', 'bar');
  58. $this->assertEquals('bar', $storage->read('foo'), 'written value can be read back correctly');
  59. }
  60. public function testMultipleInstances()
  61. {
  62. $storage1 = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
  63. $storage1->write('foo', 'bar');
  64. $storage2 = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
  65. $this->assertEquals('bar', $storage2->read('foo'), 'values persist between instances');
  66. }
  67. public function testSessionDestroy()
  68. {
  69. $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
  70. $storage->write('foo', 'bar');
  71. $this->assertCount(1, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
  72. $storage->destroy('foo');
  73. $this->assertCount(0, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
  74. }
  75. public function testSessionGC()
  76. {
  77. $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'));
  78. $storage->write('foo', 'bar');
  79. $storage->write('baz', 'bar');
  80. $this->assertCount(2, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
  81. $storage->gc(-1);
  82. $this->assertCount(0, $this->pdo->query('SELECT * FROM sessions')->fetchAll());
  83. }
  84. public function testGetConnection()
  85. {
  86. $storage = new LegacyPdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array());
  87. $method = new \ReflectionMethod($storage, 'getConnection');
  88. $method->setAccessible(true);
  89. $this->assertInstanceOf('\PDO', $method->invoke($storage));
  90. }
  91. }