CacheSessionTest.php 2.7 KB

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