AttributeBagTest.php 4.9 KB

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