CacheSessionTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * CacheSessionTest
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since 2.0.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Network\Session;
  18. use Cake\Cache\Cache;
  19. use Cake\Core\Configure;
  20. use Cake\Network\Session;
  21. use Cake\Network\Session\CacheSession;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * Class CacheSessionTest
  25. *
  26. */
  27. class CacheSessionTest extends TestCase {
  28. protected static $_sessionBackup;
  29. /**
  30. * test case startup
  31. *
  32. * @return void
  33. */
  34. public static function setupBeforeClass() {
  35. Cache::enable();
  36. Cache::config('session_test', [
  37. 'engine' => 'File',
  38. 'path' => TMP . 'sessions',
  39. 'prefix' => 'session_test'
  40. ]);
  41. static::$_sessionBackup = Configure::read('Session');
  42. Configure::write('Session.handler.config', 'session_test');
  43. }
  44. /**
  45. * cleanup after test case.
  46. *
  47. * @return void
  48. */
  49. public static function teardownAfterClass() {
  50. Cache::clear(false, 'session_test');
  51. Cache::drop('session_test');
  52. Configure::write('Session', static::$_sessionBackup);
  53. }
  54. /**
  55. * setup
  56. *
  57. * @return void
  58. */
  59. public function setUp() {
  60. parent::setUp();
  61. $this->storage = new CacheSession();
  62. }
  63. /**
  64. * tearDown
  65. *
  66. * @return void
  67. */
  68. public function tearDown() {
  69. parent::tearDown();
  70. unset($this->storage);
  71. }
  72. /**
  73. * test open
  74. *
  75. * @return void
  76. */
  77. public function testOpen() {
  78. $this->assertTrue($this->storage->open(null, null));
  79. }
  80. /**
  81. * test write()
  82. *
  83. * @return void
  84. */
  85. public function testWrite() {
  86. $this->storage->write('abc', 'Some value');
  87. $this->assertEquals('Some value', Cache::read('abc', 'session_test'), 'Value was not written.');
  88. }
  89. /**
  90. * test reading.
  91. *
  92. * @return void
  93. */
  94. public function testRead() {
  95. $this->storage->write('test_one', 'Some other value');
  96. $this->assertEquals('Some other value', $this->storage->read('test_one'), 'Incorrect value.');
  97. }
  98. /**
  99. * test destroy
  100. *
  101. * @return void
  102. */
  103. public function testDestroy() {
  104. $this->storage->write('test_one', 'Some other value');
  105. $this->assertTrue($this->storage->destroy('test_one'), 'Value was not deleted.');
  106. $this->assertFalse(Cache::read('test_one', 'session_test'), 'Value stuck around.');
  107. }
  108. }