CacheSessionTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CacheSessionTest
  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\Cache\Cache;
  20. use Cake\Http\Session\CacheSession;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * CacheSessionTest
  24. */
  25. class CacheSessionTest extends TestCase
  26. {
  27. protected static $_sessionBackup;
  28. /**
  29. * @var \Cake\Http\Session\CacheSession
  30. */
  31. protected $storage;
  32. /**
  33. * setup
  34. *
  35. * @return void
  36. */
  37. public function setUp(): void
  38. {
  39. parent::setUp();
  40. Cache::setConfig(['session_test' => ['engine' => 'File']]);
  41. $this->storage = new CacheSession(['config' => 'session_test']);
  42. }
  43. /**
  44. * tearDown
  45. *
  46. * @return void
  47. */
  48. public function tearDown(): void
  49. {
  50. parent::tearDown();
  51. Cache::clear('session_test');
  52. Cache::drop('session_test');
  53. unset($this->storage);
  54. }
  55. /**
  56. * test open
  57. *
  58. * @return void
  59. */
  60. public function testOpen()
  61. {
  62. $this->assertTrue($this->storage->open(null, null));
  63. }
  64. /**
  65. * test write()
  66. *
  67. * @return void
  68. */
  69. public function testWrite()
  70. {
  71. $this->storage->write('abc', 'Some value');
  72. $this->assertSame('Some value', Cache::read('abc', 'session_test'), 'Value was not written.');
  73. }
  74. /**
  75. * test reading.
  76. *
  77. * @return void
  78. */
  79. public function testRead()
  80. {
  81. $this->storage->write('test_one', 'Some other value');
  82. $this->assertSame('Some other value', $this->storage->read('test_one'), 'Incorrect value.');
  83. }
  84. /**
  85. * test destroy
  86. *
  87. * @return void
  88. */
  89. public function testDestroy()
  90. {
  91. $this->storage->write('test_one', 'Some other value');
  92. $this->assertTrue($this->storage->destroy('test_one'), 'Value was not deleted.');
  93. $this->assertNull(Cache::read('test_one', 'session_test'), 'Value stuck around.');
  94. }
  95. /**
  96. * Tests that a cache config is required
  97. *
  98. * @return void
  99. */
  100. public function testMissingConfig()
  101. {
  102. $this->expectException(\InvalidArgumentException::class);
  103. $this->expectExceptionMessage('The cache configuration name to use is required');
  104. new CacheSession(['foo' => 'bar']);
  105. }
  106. }