SessionTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * This file is part of the Stash package.
  4. *
  5. * (c) Robert Hafner <tedivm@tedivm.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 Stash\Test;
  11. use Stash\Session;
  12. use Stash\Pool;
  13. /**
  14. * @package Stash
  15. * @author Robert Hafner <tedivm@tedivm.com>
  16. */
  17. class SessionTest extends \PHPUnit_Framework_TestCase
  18. {
  19. protected $testClass = '\Stash\Session';
  20. protected $poolClass = '\Stash\Pool';
  21. protected function setUp()
  22. {
  23. if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.0.0', '<')) {
  24. $this->markTestSkipped('Sessions not supported on older versions of HHVM.');
  25. }
  26. }
  27. public function testRegisterHandler()
  28. {
  29. }
  30. public function testReadAndWrite()
  31. {
  32. $session = $this->getSession();
  33. $this->assertSame('', $session->read('session_id'),
  34. 'Empty session returns empty string.');
  35. $this->assertTrue($session->write('session_id', 'session_data'),
  36. 'Data was written to the session.');
  37. $this->assertSame('session_data', $session->read('session_id'),
  38. 'Active session returns session data.');
  39. }
  40. public function testOpen()
  41. {
  42. $pool = $this->getPool();
  43. $sessionA = $this->getSession($pool);
  44. $sessionA->open('first', 'session');
  45. $sessionA->write('shared_id', "session_a_data");
  46. $sessionB = $this->getSession($pool);
  47. $sessionB->open('second', 'session');
  48. $sessionB->write('shared_id', "session_b_data");
  49. $DataA = $sessionA->read('shared_id');
  50. $DataB = $sessionB->read('shared_id');
  51. $this->assertTrue($DataA != $DataB,
  52. 'Sessions with different paths do not share data.');
  53. $pool = $this->getPool();
  54. $sessionA = $this->getSession($pool);
  55. $sessionA->open('shared_path', 'sessionA');
  56. $sessionA->write('shared_id', "session_a_data");
  57. $sessionB = $this->getSession($pool);
  58. $sessionB->open('shared_path', 'sessionB');
  59. $sessionB->write('shared_id', "session_b_data");
  60. $DataA = $sessionA->read('shared_id');
  61. $DataB = $sessionB->read('shared_id');
  62. $this->assertTrue($DataA != $DataB,
  63. 'Sessions with different names do not share data.');
  64. }
  65. public function testClose()
  66. {
  67. $session = $this->getSession();
  68. $this->assertTrue($session->close(),
  69. 'Session was closed');
  70. }
  71. public function testDestroy()
  72. {
  73. $session = $this->getSession();
  74. $session->write('session_id', 'session_data');
  75. $session->write('session_id', 'session_data');
  76. $this->assertSame('session_data', $session->read('session_id'),
  77. 'Active session returns session data.');
  78. $this->assertTrue($session->destroy('session_id'),
  79. 'Data was removed from the session.');
  80. $this->assertSame('', $session->read('session_id'),
  81. 'Destroyed session returns empty string.');
  82. }
  83. public function testGarbageCollect()
  84. {
  85. $pool = $this->getPool();
  86. $sessionA = $this->getSession($pool);
  87. $sessionA->setOptions(array('ttl' => -30));
  88. $sessionA->write('session_id', "session_a_data");
  89. $sessionB = $this->getSession($pool);
  90. $sessionB->gc(null);
  91. $sessionC = $this->getSession($pool);
  92. $this->assertSame('', $sessionC->read('session_id'),
  93. 'Purged session returns empty string.');
  94. }
  95. protected function getSession($pool = null)
  96. {
  97. if (!isset($pool)) {
  98. $pool = $this->getPool();
  99. }
  100. return new $this->testClass($pool);
  101. }
  102. protected function getPool()
  103. {
  104. return new $this->poolClass();
  105. }
  106. }