CacheSessionTest.php 2.7 KB

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