SessionTest.php 7.9 KB

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