DatabaseSessionTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\ORM\Table;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * Database session test.
  26. */
  27. class DatabaseSessionTest extends TestCase
  28. {
  29. /**
  30. * @var array
  31. */
  32. protected array $fixtures = ['core.Sessions'];
  33. /**
  34. * @var \Cake\Http\Session\DatabaseSession
  35. */
  36. protected $storage;
  37. /**
  38. * setUp
  39. */
  40. public function setUp(): void
  41. {
  42. parent::setUp();
  43. static::setAppNamespace();
  44. $this->storage = new DatabaseSession();
  45. }
  46. /**
  47. * tearDown
  48. */
  49. public function tearDown(): void
  50. {
  51. unset($this->storage);
  52. parent::tearDown();
  53. }
  54. /**
  55. * test that constructor sets the right things up.
  56. */
  57. public function testConstructionSettings(): void
  58. {
  59. $this->getTableLocator()->clear();
  60. new DatabaseSession();
  61. $session = $this->getTableLocator()->get('Sessions');
  62. $this->assertInstanceOf(Table::class, $session);
  63. $this->assertSame('Sessions', $session->getAlias());
  64. $this->assertEquals(ConnectionManager::get('test'), $session->getConnection());
  65. $this->assertSame('sessions', $session->getTable());
  66. }
  67. /**
  68. * test opening the session
  69. */
  70. public function testOpen(): void
  71. {
  72. $this->assertTrue($this->storage->open('', ''));
  73. }
  74. /**
  75. * test write()
  76. */
  77. public function testWrite(): void
  78. {
  79. $result = $this->storage->write('foo', 'Some value');
  80. $this->assertTrue($result);
  81. $expires = $this->getTableLocator()->get('Sessions')->get('foo')->expires;
  82. $expected = time() + ini_get('session.gc_maxlifetime');
  83. $this->assertWithinRange($expected, $expires, 1);
  84. }
  85. /**
  86. * testReadAndWriteWithDatabaseStorage method
  87. */
  88. public function testWriteEmptySessionId(): void
  89. {
  90. $result = $this->storage->write('', 'This is a Test');
  91. $this->assertFalse($result);
  92. }
  93. /**
  94. * test read()
  95. */
  96. public function testRead(): void
  97. {
  98. $this->storage->write('foo', 'Some value');
  99. $result = $this->storage->read('foo');
  100. $expected = 'Some value';
  101. $this->assertSame($expected, $result);
  102. $result = $this->storage->read('made up value');
  103. $this->assertSame('', $result);
  104. }
  105. /**
  106. * test blowing up the session.
  107. */
  108. public function testDestroy(): void
  109. {
  110. $this->assertTrue($this->storage->write('foo', 'Some value'));
  111. $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
  112. $this->assertSame('', $this->storage->read('foo'), 'Value still present.');
  113. $this->assertTrue($this->storage->destroy('foo'), 'Destroy should always return true');
  114. }
  115. /**
  116. * test the garbage collector
  117. */
  118. public function testGc(): void
  119. {
  120. $this->getTableLocator()->clear();
  121. $storage = new DatabaseSession();
  122. $storage->setTimeout(0);
  123. $storage->write('foo', 'Some value');
  124. sleep(1);
  125. $storage->gc(0);
  126. $this->assertSame('', $storage->read('foo'));
  127. }
  128. /**
  129. * Tests serializing an entity
  130. */
  131. public function testSerializeEntity(): void
  132. {
  133. $entity = new Entity();
  134. $entity->value = 'something';
  135. $this->storage->write('key', serialize($entity));
  136. $data = $this->getTableLocator()->get('Sessions')->get('key')->data;
  137. $this->assertSame(serialize($entity), stream_get_contents($data));
  138. }
  139. }