PageRenderTime 22ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Tests/Session/Attribute/NamespacedAttributeBagTest.php

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