SessionTest.php 6.2 KB

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