SessionTest.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 testStorageRegenerate()
  147. {
  148. $this->storage->write('foo', 'bar');
  149. $this->assertTrue($this->storage->regenerate());
  150. $this->assertEquals('bar', $this->storage->read('foo'));
  151. $this->assertTrue($this->storage->regenerate(true));
  152. $this->assertNull($this->storage->read('foo'));
  153. }
  154. public function testStorageRemove()
  155. {
  156. $this->storage->write('foo', 'bar');
  157. $this->assertEquals('bar', $this->storage->read('foo'));
  158. $this->storage->remove('foo');
  159. $this->assertNull($this->storage->read('foo'));
  160. }
  161. protected function getSession()
  162. {
  163. return new Session($this->storage);
  164. }
  165. }