SessionComponentTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Tools\Test\TestCase\Controller\Component;
  3. use Cake\Controller\ComponentRegistry;
  4. use Tools\Controller\Component\SessionComponent;
  5. use Cake\Controller\Controller;
  6. use Cake\Core\Configure;
  7. use Cake\Network\Request;
  8. use Cake\Network\Session;
  9. use Cake\Routing\DispatcherFactory;
  10. use Cake\TestSuite\TestCase;
  11. /**
  12. * SessionComponentTest class
  13. *
  14. */
  15. class SessionComponentTest extends TestCase {
  16. protected static $_sessionBackup;
  17. /**
  18. * fixtures
  19. *
  20. * @var string
  21. */
  22. public $fixtures = array('core.sessions');
  23. /**
  24. * test case startup
  25. *
  26. * @return void
  27. */
  28. public static function setupBeforeClass() {
  29. DispatcherFactory::add('Routing');
  30. DispatcherFactory::add('ControllerFactory');
  31. }
  32. /**
  33. * cleanup after test case.
  34. *
  35. * @return void
  36. */
  37. public static function teardownAfterClass() {
  38. DispatcherFactory::clear();
  39. }
  40. /**
  41. * setUp method
  42. *
  43. * @return void
  44. */
  45. public function setUp() {
  46. parent::setUp();
  47. $_SESSION = [];
  48. Configure::write('App.namespace', 'TestApp');
  49. $controller = new Controller(new Request(['session' => new Session()]));
  50. $this->ComponentRegistry = new ComponentRegistry($controller);
  51. }
  52. /**
  53. * tearDown method
  54. *
  55. * @return void
  56. */
  57. public function tearDown() {
  58. parent::tearDown();
  59. }
  60. /**
  61. * testSessionReadWrite method
  62. *
  63. * @return void
  64. */
  65. public function testSessionReadWrite() {
  66. $Session = new SessionComponent($this->ComponentRegistry);
  67. $this->assertNull($Session->read('Test'));
  68. $Session->write('Test', 'some value');
  69. $this->assertEquals('some value', $Session->read('Test'));
  70. $Session->delete('Test');
  71. $Session->write('Test.key.path', 'some value');
  72. $this->assertEquals('some value', $Session->read('Test.key.path'));
  73. $this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
  74. $Session->write('Test.key.path2', 'another value');
  75. $this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
  76. $Session->delete('Test');
  77. $array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  78. $Session->write('Test', $array);
  79. $this->assertEquals($Session->read('Test'), $array);
  80. $Session->delete('Test');
  81. $Session->write(array('Test'), 'some value');
  82. $Session->write(array('Test' => 'some value'));
  83. $this->assertEquals('some value', $Session->read('Test'));
  84. $Session->delete('Test');
  85. }
  86. /**
  87. * testSessionDelete method
  88. *
  89. * @return void
  90. */
  91. public function testSessionDelete() {
  92. $Session = new SessionComponent($this->ComponentRegistry);
  93. $Session->write('Test', 'some value');
  94. $Session->delete('Test');
  95. $this->assertNull($Session->read('Test'));
  96. }
  97. /**
  98. * testSessionCheck method
  99. *
  100. * @return void
  101. */
  102. public function testSessionCheck() {
  103. $Session = new SessionComponent($this->ComponentRegistry);
  104. $this->assertFalse($Session->check('Test'));
  105. $Session->write('Test', 'some value');
  106. $this->assertTrue($Session->check('Test'));
  107. $Session->delete('Test');
  108. }
  109. /**
  110. * testSessionId method
  111. *
  112. * @return void
  113. */
  114. public function testSessionId() {
  115. $Session = new SessionComponent($this->ComponentRegistry);
  116. $this->assertEquals(session_id(), $Session->id());
  117. }
  118. /**
  119. * testSessionDestroy method
  120. *
  121. * @return void
  122. */
  123. public function testSessionDestroy() {
  124. $Session = new SessionComponent($this->ComponentRegistry);
  125. $Session->write('Test', 'some value');
  126. $this->assertEquals('some value', $Session->read('Test'));
  127. $Session->destroy('Test');
  128. $this->assertNull($Session->read('Test'));
  129. }
  130. }