菜谱项目

SessionTest.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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\Component\HttpFoundation\Tests\Session;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Session\Session;
  13. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  14. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  15. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  16. /**
  17. * SessionTest.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Robert Schönthal <seroscho@googlemail.com>
  21. * @author Drak <drak@zikula.org>
  22. */
  23. class SessionTest extends TestCase
  24. {
  25. /**
  26. * @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface
  27. */
  28. protected $storage;
  29. /**
  30. * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
  31. */
  32. protected $session;
  33. protected function setUp()
  34. {
  35. $this->storage = new MockArraySessionStorage();
  36. $this->session = new Session($this->storage, new AttributeBag(), new FlashBag());
  37. }
  38. protected function tearDown()
  39. {
  40. $this->storage = null;
  41. $this->session = null;
  42. }
  43. public function testStart()
  44. {
  45. $this->assertEquals('', $this->session->getId());
  46. $this->assertTrue($this->session->start());
  47. $this->assertNotEquals('', $this->session->getId());
  48. }
  49. public function testIsStarted()
  50. {
  51. $this->assertFalse($this->session->isStarted());
  52. $this->session->start();
  53. $this->assertTrue($this->session->isStarted());
  54. }
  55. public function testSetId()
  56. {
  57. $this->assertEquals('', $this->session->getId());
  58. $this->session->setId('0123456789abcdef');
  59. $this->session->start();
  60. $this->assertEquals('0123456789abcdef', $this->session->getId());
  61. }
  62. public function testSetName()
  63. {
  64. $this->assertEquals('MOCKSESSID', $this->session->getName());
  65. $this->session->setName('session.test.com');
  66. $this->session->start();
  67. $this->assertEquals('session.test.com', $this->session->getName());
  68. }
  69. public function testGet()
  70. {
  71. // tests defaults
  72. $this->assertNull($this->session->get('foo'));
  73. $this->assertEquals(1, $this->session->get('foo', 1));
  74. }
  75. /**
  76. * @dataProvider setProvider
  77. */
  78. public function testSet($key, $value)
  79. {
  80. $this->session->set($key, $value);
  81. $this->assertEquals($value, $this->session->get($key));
  82. }
  83. /**
  84. * @dataProvider setProvider
  85. */
  86. public function testHas($key, $value)
  87. {
  88. $this->session->set($key, $value);
  89. $this->assertTrue($this->session->has($key));
  90. $this->assertFalse($this->session->has($key.'non_value'));
  91. }
  92. public function testReplace()
  93. {
  94. $this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome'));
  95. $this->assertEquals(array('happiness' => 'be good', 'symfony' => 'awesome'), $this->session->all());
  96. $this->session->replace(array());
  97. $this->assertEquals(array(), $this->session->all());
  98. }
  99. /**
  100. * @dataProvider setProvider
  101. */
  102. public function testAll($key, $value, $result)
  103. {
  104. $this->session->set($key, $value);
  105. $this->assertEquals($result, $this->session->all());
  106. }
  107. /**
  108. * @dataProvider setProvider
  109. */
  110. public function testClear($key, $value)
  111. {
  112. $this->session->set('hi', 'fabien');
  113. $this->session->set($key, $value);
  114. $this->session->clear();
  115. $this->assertEquals(array(), $this->session->all());
  116. }
  117. public function setProvider()
  118. {
  119. return array(
  120. array('foo', 'bar', array('foo' => 'bar')),
  121. array('foo.bar', 'too much beer', array('foo.bar' => 'too much beer')),
  122. array('great', 'symfony is great', array('great' => 'symfony is great')),
  123. );
  124. }
  125. /**
  126. * @dataProvider setProvider
  127. */
  128. public function testRemove($key, $value)
  129. {
  130. $this->session->set('hi.world', 'have a nice day');
  131. $this->session->set($key, $value);
  132. $this->session->remove($key);
  133. $this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all());
  134. }
  135. public function testInvalidate()
  136. {
  137. $this->session->set('invalidate', 123);
  138. $this->session->invalidate();
  139. $this->assertEquals(array(), $this->session->all());
  140. }
  141. public function testMigrate()
  142. {
  143. $this->session->set('migrate', 321);
  144. $this->session->migrate();
  145. $this->assertEquals(321, $this->session->get('migrate'));
  146. }
  147. public function testMigrateDestroy()
  148. {
  149. $this->session->set('migrate', 333);
  150. $this->session->migrate(true);
  151. $this->assertEquals(333, $this->session->get('migrate'));
  152. }
  153. public function testSave()
  154. {
  155. $this->session->start();
  156. $this->session->save();
  157. $this->assertFalse($this->session->isStarted());
  158. }
  159. public function testGetId()
  160. {
  161. $this->assertEquals('', $this->session->getId());
  162. $this->session->start();
  163. $this->assertNotEquals('', $this->session->getId());
  164. }
  165. public function testGetFlashBag()
  166. {
  167. $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface', $this->session->getFlashBag());
  168. }
  169. public function testGetIterator()
  170. {
  171. $attributes = array('hello' => 'world', 'symfony' => 'rocks');
  172. foreach ($attributes as $key => $val) {
  173. $this->session->set($key, $val);
  174. }
  175. $i = 0;
  176. foreach ($this->session as $key => $val) {
  177. $this->assertEquals($attributes[$key], $val);
  178. ++$i;
  179. }
  180. $this->assertEquals(count($attributes), $i);
  181. }
  182. public function testGetCount()
  183. {
  184. $this->session->set('hello', 'world');
  185. $this->session->set('symfony', 'rocks');
  186. $this->assertCount(2, $this->session);
  187. }
  188. public function testGetMeta()
  189. {
  190. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetadataBag', $this->session->getMetadataBag());
  191. }
  192. }