SessionTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\SessionStorageInterface;
  13. /**
  14. * SessionTest
  15. *
  16. * @author Robert Schönthal <seroscho@googlemail.com>
  17. */
  18. class SessionTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @dataProvider provider
  22. */
  23. public function testFlash($s)
  24. {
  25. $this->assertFalse($s->hasFlash('foo'));
  26. $s->setFlash('foo', 'bar');
  27. $this->assertTrue($s->hasFlash('foo'));
  28. $this->assertSame('bar',$s->getFlash('foo'));
  29. $s->removeFlash('foo');
  30. $this->assertFalse($s->hasFlash('foo'));
  31. $flashes = array('foo'=>'bar','bar'=>'foo');
  32. $s->restart();
  33. $s->setFlashes($flashes);
  34. $this->assertSame($flashes,$s->getFlashes());
  35. $s->clearFlashes();
  36. $this->assertSame(array(),$s->getFlashes());
  37. }
  38. /**
  39. * @dataProvider provider
  40. */
  41. public function testAttribute($s)
  42. {
  43. $this->assertFalse($s->has('foo'));
  44. $this->assertNull($s->get('foo'));
  45. $s->set('foo', 'bar');
  46. $this->assertTrue($s->has('foo'));
  47. $this->assertSame('bar',$s->get('foo'));
  48. $s->restart();
  49. $s->remove('foo');
  50. $s->set('foo', 'bar');
  51. $s->remove('foo');
  52. $this->assertFalse($s->has('foo'));
  53. $attrs = array('foo'=>'bar','bar'=>'foo');
  54. $s->restart();
  55. $s->setAttributes($attrs);
  56. $this->assertSame($attrs,$s->getAttributes());
  57. $s->clear();
  58. $this->assertSame(array(),$s->getAttributes());
  59. }
  60. /**
  61. * @dataProvider provider
  62. */
  63. public function testMigrateAndInvalidate($s)
  64. {
  65. $s->set('foo', 'bar');
  66. $s->setFlash('foo', 'bar');
  67. $this->assertSame('bar',$s->get('foo'));
  68. $this->assertSame('bar',$s->getFlash('foo'));
  69. $s->migrate();
  70. $this->assertSame('bar',$s->get('foo'));
  71. $this->assertSame('bar',$s->getFlash('foo'));
  72. $s->restart();
  73. $s->invalidate();
  74. $this->assertSame(array(),$s->getAttributes());
  75. $this->assertSame(array(),$s->getFlashes());
  76. }
  77. public function testSerialize()
  78. {
  79. $storage = new SessionTestStorage();
  80. $options = array('foo'=>'bar');
  81. $s = new Session($storage,$options);
  82. $compare = serialize(array($storage, $options));
  83. $this->assertSame($compare, $s->serialize());
  84. $s->unserialize($compare);
  85. $_options = new \ReflectionProperty(get_class($s),'options');
  86. $_options->setAccessible(true);
  87. $_storage = new \ReflectionProperty(get_class($s),'storage');
  88. $_storage->setAccessible(true);
  89. $this->assertEquals($_options->getValue($s),$options,'options match');
  90. $this->assertEquals($_storage->getValue($s),$storage,'storage match');
  91. }
  92. public function testSave()
  93. {
  94. $storage = new SessionTestStorage();
  95. $options = array('foo'=>'bar');
  96. $s = new Session($storage,$options);
  97. $s->set('foo','bar');
  98. $s->save();
  99. $compare = array('_symfony2'=>array('_flash'=>array(),'_locale'=>'en','foo'=>'bar'));
  100. $this->assertSame($storage->attrs,$compare);
  101. }
  102. /**
  103. * @dataProvider provider
  104. */
  105. public function testLocale($s)
  106. {
  107. $this->assertSame('en',$s->getLocale(),'default locale is en');
  108. $s->set('_locale','de');
  109. $this->assertSame('de',$s->getLocale(),'locale is de');
  110. $s->restart();
  111. $s->setLocale('fr');
  112. $this->assertSame('fr',$s->getLocale(),'locale is fr');
  113. }
  114. /**
  115. * @dataProvider provider
  116. */
  117. public function testGetId($s)
  118. {
  119. $this->assertSame('foo',$s->getId());
  120. }
  121. /**
  122. * @dataProvider provider
  123. */
  124. public function testStart($s)
  125. {
  126. $s->start();
  127. $this->assertSame('en',$s->getLocale());
  128. $this->assertSame(array(),$s->getFlashes());
  129. $this->assertSame(array('_flash'=>array(),'_locale'=>'en'),$s->getAttributes());
  130. $s->start();
  131. $this->assertSame('en',$s->getLocale());
  132. }
  133. public function provider()
  134. {
  135. $storage = new SessionTestStorage();
  136. $session = new SessionTestSession($storage);
  137. return array(
  138. array($session)
  139. );
  140. }
  141. }
  142. class SessionTestSession extends Session
  143. {
  144. /**
  145. * Little helper for simulating a fresh session.
  146. */
  147. public function restart()
  148. {
  149. $this->started = false;
  150. }
  151. }
  152. class SessionTestStorage implements SessionStorageInterface
  153. {
  154. public $attrs;
  155. function start()
  156. {
  157. }
  158. function getId()
  159. {
  160. return 'foo';
  161. }
  162. function read($key)
  163. {
  164. }
  165. function remove($key)
  166. {
  167. }
  168. function write($key, $data)
  169. {
  170. $this->attrs[$key] = $data;
  171. }
  172. function regenerate($destroy = false)
  173. {
  174. }
  175. }