菜谱项目

AcceptHeaderItemTest.php 3.5KB

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