NamespacedAttributeBagTest.php 5.7 KB

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