AcceptHeaderItemTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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;
  11. use Symfony\Component\HttpFoundation\AcceptHeaderItem;
  12. class AcceptHeaderItemTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider provideFromStringData
  16. */
  17. public function testFromString($string, $value, array $attributes)
  18. {
  19. $item = AcceptHeaderItem::fromString($string);
  20. $this->assertEquals($value, $item->getValue());
  21. $this->assertEquals($attributes, $item->getAttributes());
  22. }
  23. public function provideFromStringData()
  24. {
  25. return array(
  26. array(
  27. 'text/html',
  28. 'text/html', array()
  29. ),
  30. array(
  31. '"this;should,not=matter"',
  32. 'this;should,not=matter', array()
  33. ),
  34. array(
  35. "text/plain; charset=utf-8;param=\"this;should,not=matter\";\tfootnotes=true",
  36. 'text/plain', array('charset' => 'utf-8', 'param' => 'this;should,not=matter', 'footnotes' => 'true')
  37. ),
  38. array(
  39. '"this;should,not=matter";charset=utf-8',
  40. 'this;should,not=matter', array('charset' => 'utf-8')
  41. ),
  42. );
  43. }
  44. /**
  45. * @dataProvider provideToStringData
  46. */
  47. public function testToString($value, array $attributes, $string)
  48. {
  49. $item = new AcceptHeaderItem($value, $attributes);
  50. $this->assertEquals($string, (string) $item);
  51. }
  52. public function provideToStringData()
  53. {
  54. return array(
  55. array(
  56. 'text/html', array(),
  57. 'text/html'
  58. ),
  59. array(
  60. 'text/plain', array('charset' => 'utf-8', 'param' => 'this;should,not=matter', 'footnotes' => 'true'),
  61. 'text/plain;charset=utf-8;param="this;should,not=matter";footnotes=true'
  62. ),
  63. );
  64. }
  65. public function testValue()
  66. {
  67. $item = new AcceptHeaderItem('value', array());
  68. $this->assertEquals('value', $item->getValue());
  69. $item->setValue('new value');
  70. $this->assertEquals('new value', $item->getValue());
  71. $item->setValue(1);
  72. $this->assertEquals('1', $item->getValue());
  73. }
  74. public function testQuality()
  75. {
  76. $item = new AcceptHeaderItem('value', array());
  77. $this->assertEquals(1.0, $item->getQuality());
  78. $item->setQuality(0.5);
  79. $this->assertEquals(0.5, $item->getQuality());
  80. $item->setAttribute('q', 0.75);
  81. $this->assertEquals(0.75, $item->getQuality());
  82. $this->assertFalse($item->hasAttribute('q'));
  83. }
  84. public function testAttribute()
  85. {
  86. $item = new AcceptHeaderItem('value', array());
  87. $this->assertEquals(array(), $item->getAttributes());
  88. $this->assertFalse($item->hasAttribute('test'));
  89. $this->assertNull($item->getAttribute('test'));
  90. $this->assertEquals('default', $item->getAttribute('test', 'default'));
  91. $item->setAttribute('test', 'value');
  92. $this->assertEquals(array('test' => 'value'), $item->getAttributes());
  93. $this->assertTrue($item->hasAttribute('test'));
  94. $this->assertEquals('value', $item->getAttribute('test'));
  95. $this->assertEquals('value', $item->getAttribute('test', 'default'));
  96. }
  97. }