SessionTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests\Session;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  13. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  16. /**
  17. * SessionTest.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Robert Schönthal <seroscho@googlemail.com>
  21. * @author Drak <drak@zikula.org>
  22. */
  23. class SessionTest extends TestCase
  24. {
  25. /**
  26. * @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface
  27. */
  28. protected $storage;
  29. /**
  30. * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
  31. */
  32. protected $session;
  33. protected function setUp()
  34. {
  35. $this->storage = new MockArraySessionStorage();
  36. $this->session = new Session($this->storage, new AttributeBag(), new FlashBag());
  37. }
  38. protected function tearDown()
  39. {
  40. $this->storage = null;
  41. $this->session = null;
  42. }
  43. public function testStart()
  44. {
  45. $this->assertEquals('', $this->session->getId());
  46. $this->assertTrue($this->session->start());
  47. $this->assertNotEquals('', $this->session->getId());
  48. }
  49. public function testIsStarted()
  50. {
  51. $this->assertFalse($this->session->isStarted());
  52. $this->session->start();
  53. $this->assertTrue($this->session->isStarted());
  54. }
  55. public function testSetId()
  56. {
  57. $this->assertEquals('', $this->session->getId());
  58. $this->session->setId('0123456789abcdef');
  59. $this->session->start();
  60. $this->assertEquals('0123456789abcdef', $this->session->getId());
  61. }
  62. public function testSetIdAfterStart()
  63. {
  64. $this->session->start();
  65. $id = $this->session->getId();
  66. $e = null;
  67. try {
  68. $this->session->setId($id);
  69. } catch (\Exception $e) {
  70. }
  71. $this->assertNull($e);
  72. try {
  73. $this->session->setId('different');
  74. } catch (\Exception $e) {
  75. }
  76. $this->assertInstanceOf('\LogicException', $e);
  77. }
  78. public function testSetName()
  79. {
  80. $this->assertEquals('MOCKSESSID', $this->session->getName());
  81. $this->session->setName('session.test.com');
  82. $this->session->start();
  83. $this->assertEquals('session.test.com', $this->session->getName());
  84. }
  85. public function testGet()
  86. {
  87. // tests defaults
  88. $this->assertNull($this->session->get('foo'));
  89. $this->assertEquals(1, $this->session->get('foo', 1));
  90. }
  91. /**
  92. * @dataProvider setProvider
  93. */
  94. public function testSet($key, $value)
  95. {
  96. $this->session->set($key, $value);
  97. $this->assertEquals($value, $this->session->get($key));
  98. }
  99. /**
  100. * @dataProvider setProvider
  101. */
  102. public function testHas($key, $value)
  103. {
  104. $this->session->set($key, $value);
  105. $this->assertTrue($this->session->has($key));
  106. $this->assertFalse($this->session->has($key.'non_value'));
  107. }
  108. public function testReplace()
  109. {
  110. $this->session->replace(['happiness' => 'be good', 'symfony' => 'awesome']);
  111. $this->assertEquals(['happiness' => 'be good', 'symfony' => 'awesome'], $this->session->all());
  112. $this->session->replace([]);
  113. $this->assertEquals([], $this->session->all());
  114. }
  115. /**
  116. * @dataProvider setProvider
  117. */
  118. public function testAll($key, $value, $result)
  119. {
  120. $this->session->set($key, $value);
  121. $this->assertEquals($result, $this->session->all());
  122. }
  123. /**
  124. * @dataProvider setProvider
  125. */
  126. public function testClear($key, $value)
  127. {
  128. $this->session->set('hi', 'fabien');
  129. $this->session->set($key, $value);
  130. $this->session->clear();
  131. $this->assertEquals([], $this->session->all());
  132. }
  133. public function setProvider()
  134. {
  135. return [
  136. ['foo', 'bar', ['foo' => 'bar']],
  137. ['foo.bar', 'too much beer', ['foo.bar' => 'too much beer']],
  138. ['great', 'symfony is great', ['great' => 'symfony is great']],
  139. ];
  140. }
  141. /**
  142. * @dataProvider setProvider
  143. */
  144. public function testRemove($key, $value)
  145. {
  146. $this->session->set('hi.world', 'have a nice day');
  147. $this->session->set($key, $value);
  148. $this->session->remove($key);
  149. $this->assertEquals(['hi.world' => 'have a nice day'], $this->session->all());
  150. }
  151. public function testInvalidate()
  152. {
  153. $this->session->set('invalidate', 123);
  154. $this->session->invalidate();
  155. $this->assertEquals([], $this->session->all());
  156. }
  157. public function testMigrate()
  158. {
  159. $this->session->set('migrate', 321);
  160. $this->session->migrate();
  161. $this->assertEquals(321, $this->session->get('migrate'));
  162. }
  163. public function testMigrateDestroy()
  164. {
  165. $this->session->set('migrate', 333);
  166. $this->session->migrate(true);
  167. $this->assertEquals(333, $this->session->get('migrate'));
  168. }
  169. public function testSave()
  170. {
  171. $this->session->start();
  172. $this->session->save();
  173. $this->assertFalse($this->session->isStarted());
  174. }
  175. public function testGetId()
  176. {
  177. $this->assertEquals('', $this->session->getId());
  178. $this->session->start();
  179. $this->assertNotEquals('', $this->session->getId());
  180. }
  181. public function testGetFlashBag()
  182. {
  183. $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface', $this->session->getFlashBag());
  184. }
  185. public function testGetIterator()
  186. {
  187. $attributes = ['hello' => 'world', 'symfony' => 'rocks'];
  188. foreach ($attributes as $key => $val) {
  189. $this->session->set($key, $val);
  190. }
  191. $i = 0;
  192. foreach ($this->session as $key => $val) {
  193. $this->assertEquals($attributes[$key], $val);
  194. ++$i;
  195. }
  196. $this->assertEquals(\count($attributes), $i);
  197. }
  198. public function testGetCount()
  199. {
  200. $this->session->set('hello', 'world');
  201. $this->session->set('symfony', 'rocks');
  202. $this->assertCount(2, $this->session);
  203. }
  204. public function testGetMeta()
  205. {
  206. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetadataBag', $this->session->getMetadataBag());
  207. }
  208. public function testIsEmpty()
  209. {
  210. $this->assertTrue($this->session->isEmpty());
  211. $this->session->set('hello', 'world');
  212. $this->assertFalse($this->session->isEmpty());
  213. $this->session->remove('hello');
  214. $this->assertTrue($this->session->isEmpty());
  215. $flash = $this->session->getFlashBag();
  216. $flash->set('hello', 'world');
  217. $this->assertFalse($this->session->isEmpty());
  218. $flash->get('hello');
  219. $this->assertTrue($this->session->isEmpty());
  220. }
  221. }