CacheSessionTest.php 2.6 KB

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