DatabaseSessionTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * DatabaseSessionTest file
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  7. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * For full copyright and license information, please see the LICENSE.txt
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  14. * @link https://cakephp.org CakePHP(tm) Project
  15. * @since 2.0.0
  16. * @license https://opensource.org/licenses/mit-license.php MIT License
  17. */
  18. namespace Cake\Test\TestCase\Http\Session;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\Http\Session\DatabaseSession;
  21. use Cake\ORM\Entity;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * Database session test.
  25. */
  26. class DatabaseSessionTest extends TestCase
  27. {
  28. /**
  29. * fixtures
  30. *
  31. * @var string
  32. */
  33. protected $fixtures = ['core.Sessions'];
  34. /**
  35. * @var \Cake\Http\Session\DatabaseSession
  36. */
  37. protected $storage;
  38. /**
  39. * setUp
  40. *
  41. * @return void
  42. */
  43. public function setUp(): void
  44. {
  45. parent::setUp();
  46. static::setAppNamespace();
  47. $this->storage = new DatabaseSession();
  48. }
  49. /**
  50. * tearDown
  51. *
  52. * @return void
  53. */
  54. public function tearDown(): void
  55. {
  56. unset($this->storage);
  57. parent::tearDown();
  58. }
  59. /**
  60. * test that constructor sets the right things up.
  61. *
  62. * @return void
  63. */
  64. public function testConstructionSettings()
  65. {
  66. $this->getTableLocator()->clear();
  67. new DatabaseSession();
  68. $session = $this->getTableLocator()->get('Sessions');
  69. $this->assertInstanceOf('Cake\ORM\Table', $session);
  70. $this->assertSame('Sessions', $session->getAlias());
  71. $this->assertEquals(ConnectionManager::get('test'), $session->getConnection());
  72. $this->assertSame('sessions', $session->getTable());
  73. }
  74. /**
  75. * test opening the session
  76. *
  77. * @return void
  78. */
  79. public function testOpen()
  80. {
  81. $this->assertTrue($this->storage->open(null, null));
  82. }
  83. /**
  84. * test write()
  85. *
  86. * @return void
  87. */
  88. public function testWrite()
  89. {
  90. $result = $this->storage->write('foo', 'Some value');
  91. $this->assertTrue($result);
  92. $expires = $this->getTableLocator()->get('Sessions')->get('foo')->expires;
  93. $expected = time() + ini_get('session.gc_maxlifetime');
  94. $this->assertWithinRange($expected, $expires, 1);
  95. }
  96. /**
  97. * testReadAndWriteWithDatabaseStorage method
  98. *
  99. * @return void
  100. */
  101. public function testWriteEmptySessionId()
  102. {
  103. $result = $this->storage->write('', 'This is a Test');
  104. $this->assertFalse($result);
  105. }
  106. /**
  107. * test read()
  108. *
  109. * @return void
  110. */
  111. public function testRead()
  112. {
  113. $this->storage->write('foo', 'Some value');
  114. $result = $this->storage->read('foo');
  115. $expected = 'Some value';
  116. $this->assertEquals($expected, $result);
  117. $result = $this->storage->read('made up value');
  118. $this->assertSame('', $result);
  119. }
  120. /**
  121. * test blowing up the session.
  122. *
  123. * @return void
  124. */
  125. public function testDestroy()
  126. {
  127. $this->storage->write('foo', 'Some value');
  128. $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
  129. $this->assertSame('', $this->storage->read('foo'), 'Value still present.');
  130. $this->assertTrue($this->storage->destroy('foo'), 'Destroy should always return true');
  131. }
  132. /**
  133. * test the garbage collector
  134. *
  135. * @return void
  136. */
  137. public function testGc()
  138. {
  139. $this->getTableLocator()->clear();
  140. $storage = new DatabaseSession();
  141. $storage->setTimeout(0);
  142. $storage->write('foo', 'Some value');
  143. sleep(1);
  144. $storage->gc(0);
  145. $this->assertSame('', $storage->read('foo'));
  146. }
  147. /**
  148. * Tests serializing an entity
  149. *
  150. * @return void
  151. */
  152. public function testSerializeEntity()
  153. {
  154. $entity = new Entity();
  155. $entity->value = 'something';
  156. $result = $this->storage->write('key', serialize($entity));
  157. $data = $this->getTableLocator()->get('Sessions')->get('key')->data;
  158. $this->assertEquals(serialize($entity), stream_get_contents($data));
  159. }
  160. }