AttributeBagTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\Attribute;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  13. /**
  14. * Tests AttributeBag.
  15. *
  16. * @author Drak <drak@zikula.org>
  17. */
  18. class AttributeBagTest extends TestCase
  19. {
  20. /**
  21. * @var array
  22. */
  23. private $array;
  24. /**
  25. * @var AttributeBag
  26. */
  27. private $bag;
  28. protected function setUp()
  29. {
  30. $this->array = array(
  31. 'hello' => 'world',
  32. 'always' => 'be happy',
  33. 'user.login' => 'drak',
  34. 'csrf.token' => array(
  35. 'a' => '1234',
  36. 'b' => '4321',
  37. ),
  38. 'category' => array(
  39. 'fishing' => array(
  40. 'first' => 'cod',
  41. 'second' => 'sole',
  42. ),
  43. ),
  44. );
  45. $this->bag = new AttributeBag('_sf2');
  46. $this->bag->initialize($this->array);
  47. }
  48. protected function tearDown()
  49. {
  50. $this->bag = null;
  51. $this->array = array();
  52. }
  53. public function testInitialize()
  54. {
  55. $bag = new AttributeBag();
  56. $bag->initialize($this->array);
  57. $this->assertEquals($this->array, $bag->all());
  58. $array = array('should' => 'change');
  59. $bag->initialize($array);
  60. $this->assertEquals($array, $bag->all());
  61. }
  62. public function testGetStorageKey()
  63. {
  64. $this->assertEquals('_sf2', $this->bag->getStorageKey());
  65. $attributeBag = new AttributeBag('test');
  66. $this->assertEquals('test', $attributeBag->getStorageKey());
  67. }
  68. public function testGetSetName()
  69. {
  70. $this->assertEquals('attributes', $this->bag->getName());
  71. $this->bag->setName('foo');
  72. $this->assertEquals('foo', $this->bag->getName());
  73. }
  74. /**
  75. * @dataProvider attributesProvider
  76. */
  77. public function testHas($key, $value, $exists)
  78. {
  79. $this->assertEquals($exists, $this->bag->has($key));
  80. }
  81. /**
  82. * @dataProvider attributesProvider
  83. */
  84. public function testGet($key, $value, $expected)
  85. {
  86. $this->assertEquals($value, $this->bag->get($key));
  87. }
  88. public function testGetDefaults()
  89. {
  90. $this->assertNull($this->bag->get('user2.login'));
  91. $this->assertEquals('default', $this->bag->get('user2.login', 'default'));
  92. }
  93. /**
  94. * @dataProvider attributesProvider
  95. */
  96. public function testSet($key, $value, $expected)
  97. {
  98. $this->bag->set($key, $value);
  99. $this->assertEquals($value, $this->bag->get($key));
  100. }
  101. public function testAll()
  102. {
  103. $this->assertEquals($this->array, $this->bag->all());
  104. $this->bag->set('hello', 'fabien');
  105. $array = $this->array;
  106. $array['hello'] = 'fabien';
  107. $this->assertEquals($array, $this->bag->all());
  108. }
  109. public function testReplace()
  110. {
  111. $array = array();
  112. $array['name'] = 'jack';
  113. $array['foo.bar'] = 'beep';
  114. $this->bag->replace($array);
  115. $this->assertEquals($array, $this->bag->all());
  116. $this->assertNull($this->bag->get('hello'));
  117. $this->assertNull($this->bag->get('always'));
  118. $this->assertNull($this->bag->get('user.login'));
  119. }
  120. public function testRemove()
  121. {
  122. $this->assertEquals('world', $this->bag->get('hello'));
  123. $this->bag->remove('hello');
  124. $this->assertNull($this->bag->get('hello'));
  125. $this->assertEquals('be happy', $this->bag->get('always'));
  126. $this->bag->remove('always');
  127. $this->assertNull($this->bag->get('always'));
  128. $this->assertEquals('drak', $this->bag->get('user.login'));
  129. $this->bag->remove('user.login');
  130. $this->assertNull($this->bag->get('user.login'));
  131. }
  132. public function testClear()
  133. {
  134. $this->bag->clear();
  135. $this->assertEquals(array(), $this->bag->all());
  136. }
  137. public function attributesProvider()
  138. {
  139. return array(
  140. array('hello', 'world', true),
  141. array('always', 'be happy', true),
  142. array('user.login', 'drak', true),
  143. array('csrf.token', array('a' => '1234', 'b' => '4321'), true),
  144. array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
  145. array('user2.login', null, false),
  146. array('never', null, false),
  147. array('bye', null, false),
  148. array('bye/for/now', null, false),
  149. );
  150. }
  151. public function testGetIterator()
  152. {
  153. $i = 0;
  154. foreach ($this->bag as $key => $val) {
  155. $this->assertEquals($this->array[$key], $val);
  156. ++$i;
  157. }
  158. $this->assertEquals(count($this->array), $i);
  159. }
  160. public function testCount()
  161. {
  162. $this->assertEquals(count($this->array), count($this->bag));
  163. }
  164. }