You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

289 lines
7.8 KiB

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