SessionComponentTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 Tools\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. * Test consuming session data.
  88. *
  89. * @return void
  90. */
  91. public function testConsume() {
  92. $Session = new SessionComponent($this->ComponentRegistry);
  93. $Session->write('Some.string', 'value');
  94. $Session->write('Some.array', ['key1' => 'value1', 'key2' => 'value2']);
  95. $this->assertEquals('value', $Session->read('Some.string'));
  96. $value = $Session->consume('Some.string');
  97. $this->assertEquals('value', $value);
  98. $this->assertFalse($Session->check('Some.string'));
  99. $value = $Session->consume('');
  100. $this->assertNull($value);
  101. $value = $Session->consume(null);
  102. $this->assertNull($value);
  103. $value = $Session->consume('Some.array');
  104. $expected = ['key1' => 'value1', 'key2' => 'value2'];
  105. $this->assertEquals($expected, $value);
  106. $this->assertFalse($Session->check('Some.array'));
  107. }
  108. /**
  109. * testSessionDelete method
  110. *
  111. * @return void
  112. */
  113. public function testSessionDelete() {
  114. $Session = new SessionComponent($this->ComponentRegistry);
  115. $Session->write('Test', 'some value');
  116. $Session->delete('Test');
  117. $this->assertNull($Session->read('Test'));
  118. }
  119. /**
  120. * testSessionCheck method
  121. *
  122. * @return void
  123. */
  124. public function testSessionCheck() {
  125. $Session = new SessionComponent($this->ComponentRegistry);
  126. $this->assertFalse($Session->check('Test'));
  127. $Session->write('Test', 'some value');
  128. $this->assertTrue($Session->check('Test'));
  129. $Session->delete('Test');
  130. }
  131. /**
  132. * testSessionId method
  133. *
  134. * @return void
  135. */
  136. public function testSessionId() {
  137. $Session = new SessionComponent($this->ComponentRegistry);
  138. $this->assertEquals(session_id(), $Session->id());
  139. }
  140. /**
  141. * testSessionDestroy method
  142. *
  143. * @return void
  144. */
  145. public function testSessionDestroy() {
  146. $Session = new SessionComponent($this->ComponentRegistry);
  147. $Session->write('Test', 'some value');
  148. $this->assertEquals('some value', $Session->read('Test'));
  149. $Session->destroy('Test');
  150. $this->assertNull($Session->read('Test'));
  151. }
  152. }