HeaderBagTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Component\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\HeaderBag;
  12. class HeaderBagTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @covers Symfony\Component\HttpFoundation\HeaderBag::__construct
  16. */
  17. public function testConstructor()
  18. {
  19. $bag = new HeaderBag(array('foo' => 'bar'));
  20. $this->assertTrue( $bag->has('foo'));
  21. try {
  22. $bag = new HeaderBag(array('foo' => 'bar'), 'nope');
  23. $this->assertFalse(TRUE,'nope is not a valid type'); // --> enfore request or response
  24. } catch ( \InvalidArgumentException $e) {
  25. // ignore
  26. }
  27. try {
  28. $bag = new HeaderBag(array('foo' => 'bar'), 'request');
  29. } catch ( \Exception $e) {
  30. $this->assertFalse(TRUE,'request should be a valid type'); // --> enforce request or response
  31. }
  32. try {
  33. $bag = new HeaderBag(array('foo' => 'bar'), 'response');
  34. } catch ( \Exception $e) {
  35. $this->assertFalse(TRUE,'response should be a valid type'); // --> enforce request or response
  36. }
  37. }
  38. /**
  39. * @covers Symfony\Component\HttpFoundation\HeaderBag::all
  40. */
  41. public function testAll()
  42. {
  43. $bag = new HeaderBag(array('foo' => 'bar'));
  44. $this->assertEquals(array('foo' => array('bar')), $bag->all(), '->all() gets all the input');
  45. $bag = new HeaderBag(array('FOO' => 'BAR'));
  46. $this->assertEquals(array('foo' => array('BAR')), $bag->all(), '->all() gets all the input key are lower case');
  47. }
  48. /**
  49. * @covers Symfony\Component\HttpFoundation\HeaderBag::replace
  50. */
  51. public function testReplace()
  52. {
  53. $bag = new HeaderBag(array('foo' => 'bar'));
  54. $bag->replace(array('NOPE' => 'BAR'));
  55. $this->assertEquals(array('nope' => array('BAR')), $bag->all(), '->replace() replaces the input with the argument');
  56. $this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
  57. }
  58. /**
  59. * @covers Symfony\Component\HttpFoundation\HeaderBag::get
  60. */
  61. public function testGet()
  62. {
  63. $bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
  64. $this->assertEquals( 'bar', $bag->get('foo'), '->get return current value');
  65. $this->assertEquals( 'bar', $bag->get('FoO'), '->get key in case insensitive');
  66. $this->assertEquals( array('bar'), $bag->get('foo', 'nope', false), '->get return the value as array');
  67. // defaults
  68. $this->assertNull($bag->get('none'), '->get unknown values returns null');
  69. $this->assertEquals( 'default', $bag->get('none', 'default'), '->get unknown values returns default');
  70. $this->assertEquals( array('default'), $bag->get('none', 'default', false), '->get unknown values returns default as array');
  71. $bag->set('foo', 'bor', false);
  72. $this->assertEquals( 'bar', $bag->get('foo'), '->get return first value');
  73. $this->assertEquals( array('bar', 'bor'), $bag->get('foo', 'nope', false), '->get return all values as array');
  74. }
  75. /**
  76. * @covers Symfony\Component\HttpFoundation\HeaderBag::contains
  77. */
  78. public function testContains()
  79. {
  80. $bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
  81. $this->assertTrue( $bag->contains('foo', 'bar'), '->contains first value');
  82. $this->assertTrue( $bag->contains('fuzz', 'bizz'), '->contains second value');
  83. $this->assertFalse( $bag->contains('nope', 'nope'), '->contains unknown value');
  84. $this->assertFalse( $bag->contains('foo', 'nope'), '->contains unknown value');
  85. // Multiple values
  86. $bag->set('foo', 'bor', false);
  87. $this->assertTrue( $bag->contains('foo', 'bar'), '->contains first value');
  88. $this->assertTrue( $bag->contains('foo', 'bor'), '->contains second value');
  89. $this->assertFalse( $bag->contains('foo', 'nope'), '->contains unknown value');
  90. }
  91. }