SessionTest.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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\Tests\Component\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\Session;
  12. use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
  13. /**
  14. * SessionTest
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. * @author Robert Schönthal <seroscho@googlemail.com>
  18. */
  19. class SessionTest extends \PHPUnit_Framework_TestCase
  20. {
  21. protected $storage;
  22. protected $session;
  23. public function setUp()
  24. {
  25. $this->storage = new ArraySessionStorage();
  26. $this->session = $this->getSession();
  27. }
  28. protected function tearDown()
  29. {
  30. $this->storage = null;
  31. $this->session = null;
  32. }
  33. public function testFlash()
  34. {
  35. $this->session->clearFlashes();
  36. $this->assertSame(array(), $this->session->getFlashes());
  37. $this->assertFalse($this->session->hasFlash('foo'));
  38. $this->session->setFlash('foo', 'bar');
  39. $this->assertTrue($this->session->hasFlash('foo'));
  40. $this->assertSame('bar', $this->session->getFlash('foo'));
  41. $this->session->removeFlash('foo');
  42. $this->assertFalse($this->session->hasFlash('foo'));
  43. $flashes = array('foo' => 'bar', 'bar' => 'foo');
  44. $this->session->setFlashes($flashes);
  45. $this->assertSame($flashes, $this->session->getFlashes());
  46. }
  47. public function testFlashesAreFlushedWhenNeeded()
  48. {
  49. $this->session->setFlash('foo', 'bar');
  50. $this->session->save();
  51. $this->session = $this->getSession();
  52. $this->assertTrue($this->session->hasFlash('foo'));
  53. $this->session->save();
  54. $this->session = $this->getSession();
  55. $this->assertFalse($this->session->hasFlash('foo'));
  56. }
  57. public function testAll()
  58. {
  59. $this->assertFalse($this->session->has('foo'));
  60. $this->assertNull($this->session->get('foo'));
  61. $this->session->set('foo', 'bar');
  62. $this->assertTrue($this->session->has('foo'));
  63. $this->assertSame('bar', $this->session->get('foo'));
  64. $this->session = $this->getSession();
  65. $this->session->remove('foo');
  66. $this->session->set('foo', 'bar');
  67. $this->session->remove('foo');
  68. $this->assertFalse($this->session->has('foo'));
  69. $attrs = array('foo' => 'bar', 'bar' => 'foo');
  70. $this->session = $this->getSession();
  71. $this->session->replace($attrs);
  72. $this->assertSame($attrs, $this->session->all());
  73. $this->session->clear();
  74. $this->assertSame(array(), $this->session->all());
  75. }
  76. public function testMigrateAndInvalidate()
  77. {
  78. $this->session->set('foo', 'bar');
  79. $this->session->setFlash('foo', 'bar');
  80. $this->assertSame('bar', $this->session->get('foo'));
  81. $this->assertSame('bar', $this->session->getFlash('foo'));
  82. $this->session->migrate();
  83. $this->assertSame('bar', $this->session->get('foo'));
  84. $this->assertSame('bar', $this->session->getFlash('foo'));
  85. $this->session = $this->getSession();
  86. $this->session->invalidate();
  87. $this->assertSame(array(), $this->session->all());
  88. $this->assertSame(array(), $this->session->getFlashes());
  89. }
  90. public function testSerialize()
  91. {
  92. $defaultLocale = 'en';
  93. $this->session = new Session($this->storage, $defaultLocale);
  94. $compare = serialize(array($this->storage, $defaultLocale));
  95. $this->assertSame($compare, $this->session->serialize());
  96. $this->session->unserialize($compare);
  97. $_defaultLocale = new \ReflectionProperty(get_class($this->session), 'defaultLocale');
  98. $_defaultLocale->setAccessible(true);
  99. $_storage = new \ReflectionProperty(get_class($this->session), 'storage');
  100. $_storage->setAccessible(true);
  101. $this->assertEquals($_defaultLocale->getValue($this->session), $defaultLocale, 'options match');
  102. $this->assertEquals($_storage->getValue($this->session), $this->storage, 'storage match');
  103. }
  104. public function testSave()
  105. {
  106. $this->storage = new ArraySessionStorage();
  107. $defaultLocale = 'fr';
  108. $this->session = new Session($this->storage, $defaultLocale);
  109. $this->session->set('foo', 'bar');
  110. $this->session->save();
  111. $compare = array('_symfony2' => array('attributes' => array('foo' => 'bar'), 'flashes' => array(), 'locale' => 'fr'));
  112. $r = new \ReflectionObject($this->storage);
  113. $p = $r->getProperty('data');
  114. $p->setAccessible(true);
  115. $this->assertSame($p->getValue($this->storage), $compare);
  116. }
  117. public function testLocale()
  118. {
  119. if (!extension_loaded('intl')) {
  120. $this->markTestSkipped('The "intl" extension is not available');
  121. }
  122. $this->assertSame('en', $this->session->getLocale(), 'default locale is en');
  123. $this->assertSame('en', \Locale::getDefault(), '\Locale::getDefault() is en');
  124. $this->session->setLocale('de');
  125. $this->assertSame('de', $this->session->getLocale(), 'locale is de');
  126. $this->assertSame('de', \Locale::getDefault(), '\Locale::getDefault() is de');
  127. $this->session = $this->getSession();
  128. $this->session->setLocale('fr');
  129. $this->assertSame('fr', $this->session->getLocale(), 'locale is fr');
  130. $this->assertSame('fr', \Locale::getDefault(), '\Locale::getDefault() is fr');
  131. }
  132. public function testLocaleAfterClear()
  133. {
  134. $this->session->clear();
  135. $this->assertEquals('en', $this->session->getLocale());
  136. }
  137. public function testGetId()
  138. {
  139. $this->assertSame(null, $this->session->getId());
  140. }
  141. public function testStart()
  142. {
  143. $this->session->start();
  144. $this->assertSame('en', $this->session->getLocale());
  145. $this->assertSame('en', \Locale::getDefault());
  146. $this->assertSame(array(), $this->session->getFlashes());
  147. $this->assertSame(array(), $this->session->all());
  148. }
  149. public function testSavedOnDestruct()
  150. {
  151. $this->session->set('foo', 'bar');
  152. $this->session->__destruct();
  153. $expected = array(
  154. 'attributes'=>array('foo'=>'bar'),
  155. 'flashes'=>array(),
  156. 'locale'=>'en'
  157. );
  158. $saved = $this->storage->read('_symfony2');
  159. $this->assertSame($expected, $saved);
  160. }
  161. public function testSavedOnDestructAfterManualSave()
  162. {
  163. $this->session->set('foo', 'nothing');
  164. $this->session->save();
  165. $this->session->set('foo', 'bar');
  166. $this->session->__destruct();
  167. $expected = array(
  168. 'attributes'=>array('foo'=>'bar'),
  169. 'flashes'=>array(),
  170. 'locale'=>'en'
  171. );
  172. $saved = $this->storage->read('_symfony2');
  173. $this->assertSame($expected, $saved);
  174. }
  175. public function testStorageRegenerate()
  176. {
  177. $this->storage->write('foo', 'bar');
  178. $this->assertTrue($this->storage->regenerate());
  179. $this->assertEquals('bar', $this->storage->read('foo'));
  180. $this->assertTrue($this->storage->regenerate(true));
  181. $this->assertNull($this->storage->read('foo'));
  182. }
  183. public function testStorageRemove()
  184. {
  185. $this->storage->write('foo', 'bar');
  186. $this->assertEquals('bar', $this->storage->read('foo'));
  187. $this->storage->remove('foo');
  188. $this->assertNull($this->storage->read('foo'));
  189. }
  190. protected function getSession()
  191. {
  192. return new Session($this->storage);
  193. }
  194. }