SessionTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. public function testFlash()
  29. {
  30. $this->assertFalse($this->session->hasFlash('foo'));
  31. $this->session->setFlash('foo', 'bar');
  32. $this->assertTrue($this->session->hasFlash('foo'));
  33. $this->assertSame('bar', $this->session->getFlash('foo'));
  34. $this->session->removeFlash('foo');
  35. $this->assertFalse($this->session->hasFlash('foo'));
  36. $flashes = array('foo' => 'bar', 'bar' => 'foo');
  37. $this->session = $this->getSession();
  38. $this->session->setFlashes($flashes);
  39. $this->assertSame($flashes, $this->session->getFlashes());
  40. $this->session->clearFlashes();
  41. $this->assertSame(array(), $this->session->getFlashes());
  42. }
  43. public function testFlashesAreFlushedWhenNeeded()
  44. {
  45. $this->session->setFlash('foo', 'bar');
  46. $this->session->save();
  47. $this->session = $this->getSession();
  48. $this->assertTrue($this->session->hasFlash('foo'));
  49. $this->session->save();
  50. $this->session = $this->getSession();
  51. $this->assertFalse($this->session->hasFlash('foo'));
  52. }
  53. public function testAttribute()
  54. {
  55. $this->assertFalse($this->session->has('foo'));
  56. $this->assertNull($this->session->get('foo'));
  57. $this->session->set('foo', 'bar');
  58. $this->assertTrue($this->session->has('foo'));
  59. $this->assertSame('bar', $this->session->get('foo'));
  60. $this->session = $this->getSession();
  61. $this->session->remove('foo');
  62. $this->session->set('foo', 'bar');
  63. $this->session->remove('foo');
  64. $this->assertFalse($this->session->has('foo'));
  65. $attrs = array('foo' => 'bar', 'bar' => 'foo');
  66. $this->session = $this->getSession();
  67. $this->session->setAttributes($attrs);
  68. $this->assertSame($attrs, $this->session->getAttributes());
  69. $this->session->clear();
  70. $this->assertSame(array(), $this->session->getAttributes());
  71. }
  72. public function testMigrateAndInvalidate()
  73. {
  74. $this->session->set('foo', 'bar');
  75. $this->session->setFlash('foo', 'bar');
  76. $this->assertSame('bar', $this->session->get('foo'));
  77. $this->assertSame('bar', $this->session->getFlash('foo'));
  78. $this->session->migrate();
  79. $this->assertSame('bar', $this->session->get('foo'));
  80. $this->assertSame('bar', $this->session->getFlash('foo'));
  81. $this->session = $this->getSession();
  82. $this->session->invalidate();
  83. $this->assertSame(array(), $this->session->getAttributes());
  84. $this->assertSame(array(), $this->session->getFlashes());
  85. }
  86. public function testSerialize()
  87. {
  88. $defaultLocale = 'en';
  89. $this->session = new Session($this->storage, $defaultLocale);
  90. $compare = serialize(array($this->storage, $defaultLocale));
  91. $this->assertSame($compare, $this->session->serialize());
  92. $this->session->unserialize($compare);
  93. $_defaultLocale = new \ReflectionProperty(get_class($this->session), 'defaultLocale');
  94. $_defaultLocale->setAccessible(true);
  95. $_storage = new \ReflectionProperty(get_class($this->session), 'storage');
  96. $_storage->setAccessible(true);
  97. $this->assertEquals($_defaultLocale->getValue($this->session), $defaultLocale, 'options match');
  98. $this->assertEquals($_storage->getValue($this->session), $this->storage, 'storage match');
  99. }
  100. public function testSave()
  101. {
  102. $this->storage = new ArraySessionStorage();
  103. $defaultLocale = 'fr';
  104. $this->session = new Session($this->storage, $defaultLocale);
  105. $this->session->set('foo', 'bar');
  106. $this->session->save();
  107. $compare = array('_symfony2' => array('_flash' => array(), '_locale' => 'fr', 'foo' => 'bar'));
  108. $r = new \ReflectionObject($this->storage);
  109. $p = $r->getProperty('data');
  110. $p->setAccessible(true);
  111. $this->assertSame($p->getValue($this->storage), $compare);
  112. }
  113. public function testLocale()
  114. {
  115. $this->assertSame('en', $this->session->getLocale(), 'default locale is en');
  116. $this->session->set('_locale','de');
  117. $this->assertSame('de', $this->session->getLocale(), 'locale is de');
  118. $this->session = $this->getSession();
  119. $this->session->setLocale('fr');
  120. $this->assertSame('fr', $this->session->getLocale(), 'locale is fr');
  121. }
  122. public function testLocaleAfterClear()
  123. {
  124. $this->session->clear();
  125. $this->assertEquals('en', $this->session->getLocale());
  126. }
  127. public function testGetId()
  128. {
  129. $this->assertSame(null, $this->session->getId());
  130. }
  131. public function testStart()
  132. {
  133. $this->session->start();
  134. $this->assertSame('en', $this->session->getLocale());
  135. $this->assertSame(array(), $this->session->getFlashes());
  136. $this->assertSame(array('_flash' => array(), '_locale' => 'en'), $this->session->getAttributes());
  137. $this->session->start();
  138. $this->assertSame('en', $this->session->getLocale());
  139. }
  140. protected function getSession()
  141. {
  142. return new Session($this->storage);
  143. }
  144. }