NamespacedAttributeBagTest.php 5.7 KB

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