AcceptHeaderTest.php 3.4 KB

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