SessionTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. $this->assertSame('en', $this->session->getLocale(), 'default locale is en');
  120. $this->assertSame('en', \Locale::getDefault(), '\Locale::getDefault() is en');
  121. $this->session->setLocale('de');
  122. $this->assertSame('de', $this->session->getLocale(), 'locale is de');
  123. $this->assertSame('de', \Locale::getDefault(), '\Locale::getDefault() is de');
  124. $this->session = $this->getSession();
  125. $this->session->setLocale('fr');
  126. $this->assertSame('fr', $this->session->getLocale(), 'locale is fr');
  127. $this->assertSame('fr', \Locale::getDefault(), '\Locale::getDefault() is fr');
  128. }
  129. public function testLocaleAfterClear()
  130. {
  131. $this->session->clear();
  132. $this->assertEquals('en', $this->session->getLocale());
  133. }
  134. public function testGetId()
  135. {
  136. $this->assertSame(null, $this->session->getId());
  137. }
  138. public function testStart()
  139. {
  140. $this->session->start();
  141. $this->assertSame('en', $this->session->getLocale());
  142. $this->assertSame('en', \Locale::getDefault());
  143. $this->assertSame(array(), $this->session->getFlashes());
  144. $this->assertSame(array(), $this->session->all());
  145. }
  146. public function testSavedOnDestruct()
  147. {
  148. $this->session->set('foo', 'bar');
  149. $this->session->__destruct();
  150. $expected = array(
  151. 'attributes'=>array('foo'=>'bar'),
  152. 'flashes'=>array(),
  153. 'locale'=>'en'
  154. );
  155. $saved = $this->storage->read('_symfony2');
  156. $this->assertSame($expected, $saved);
  157. }
  158. public function testSavedOnDestructAfterManualSave()
  159. {
  160. $this->session->set('foo', 'nothing');
  161. $this->session->save();
  162. $this->session->set('foo', 'bar');
  163. $this->session->__destruct();
  164. $expected = array(
  165. 'attributes'=>array('foo'=>'bar'),
  166. 'flashes'=>array(),
  167. 'locale'=>'en'
  168. );
  169. $saved = $this->storage->read('_symfony2');
  170. $this->assertSame($expected, $saved);
  171. }
  172. public function testStorageRegenerate()
  173. {
  174. $this->storage->write('foo', 'bar');
  175. $this->assertTrue($this->storage->regenerate());
  176. $this->assertEquals('bar', $this->storage->read('foo'));
  177. $this->assertTrue($this->storage->regenerate(true));
  178. $this->assertNull($this->storage->read('foo'));
  179. }
  180. public function testStorageRemove()
  181. {
  182. $this->storage->write('foo', 'bar');
  183. $this->assertEquals('bar', $this->storage->read('foo'));
  184. $this->storage->remove('foo');
  185. $this->assertNull($this->storage->read('foo'));
  186. }
  187. protected function getSession()
  188. {
  189. return new Session($this->storage);
  190. }
  191. }