AcceptHeaderTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\AcceptHeader;
  13. use Symfony\Component\HttpFoundation\AcceptHeaderItem;
  14. class AcceptHeaderTest extends TestCase
  15. {
  16. public function testFirst()
  17. {
  18. $header = AcceptHeader::fromString('text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c');
  19. $this->assertSame('text/html', $header->first()->getValue());
  20. }
  21. /**
  22. * @dataProvider provideFromStringData
  23. */
  24. public function testFromString($string, array $items)
  25. {
  26. $header = AcceptHeader::fromString($string);
  27. $parsed = array_values($header->all());
  28. // reset index since the fixtures don't have them set
  29. foreach ($parsed as $item) {
  30. $item->setIndex(0);
  31. }
  32. $this->assertEquals($items, $parsed);
  33. }
  34. public function provideFromStringData()
  35. {
  36. return array(
  37. array('', array()),
  38. array('gzip', array(new AcceptHeaderItem('gzip'))),
  39. array('gzip,deflate,sdch', array(new AcceptHeaderItem('gzip'), new AcceptHeaderItem('deflate'), new AcceptHeaderItem('sdch'))),
  40. array("gzip, deflate\t,sdch", array(new AcceptHeaderItem('gzip'), new AcceptHeaderItem('deflate'), new AcceptHeaderItem('sdch'))),
  41. array('"this;should,not=matter"', array(new AcceptHeaderItem('this;should,not=matter'))),
  42. );
  43. }
  44. /**
  45. * @dataProvider provideToStringData
  46. */
  47. public function testToString(array $items, $string)
  48. {
  49. $header = new AcceptHeader($items);
  50. $this->assertEquals($string, (string) $header);
  51. }
  52. public function provideToStringData()
  53. {
  54. return array(
  55. array(array(), ''),
  56. array(array(new AcceptHeaderItem('gzip')), 'gzip'),
  57. array(array(new AcceptHeaderItem('gzip'), new AcceptHeaderItem('deflate'), new AcceptHeaderItem('sdch')), 'gzip,deflate,sdch'),
  58. array(array(new AcceptHeaderItem('this;should,not=matter')), 'this;should,not=matter'),
  59. );
  60. }
  61. /**
  62. * @dataProvider provideFilterData
  63. */
  64. public function testFilter($string, $filter, array $values)
  65. {
  66. $header = AcceptHeader::fromString($string)->filter($filter);
  67. $this->assertEquals($values, array_keys($header->all()));
  68. }
  69. public function provideFilterData()
  70. {
  71. return array(
  72. array('fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4', '/fr.*/', array('fr-FR', 'fr')),
  73. );
  74. }
  75. /**
  76. * @dataProvider provideSortingData
  77. */
  78. public function testSorting($string, array $values)
  79. {
  80. $header = AcceptHeader::fromString($string);
  81. $this->assertEquals($values, array_keys($header->all()));
  82. }
  83. public function provideSortingData()
  84. {
  85. return array(
  86. 'quality has priority' => array('*;q=0.3,ISO-8859-1,utf-8;q=0.7', array('ISO-8859-1', 'utf-8', '*')),
  87. 'order matters when q is equal' => array('*;q=0.3,ISO-8859-1;q=0.7,utf-8;q=0.7', array('ISO-8859-1', 'utf-8', '*')),
  88. 'order matters when q is equal2' => array('*;q=0.3,utf-8;q=0.7,ISO-8859-1;q=0.7', array('utf-8', 'ISO-8859-1', '*')),
  89. );
  90. }
  91. }