4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\AcceptHeader;
16 use Symfony\Component\HttpFoundation\AcceptHeaderItem;
18 class AcceptHeaderTest extends TestCase
20 public function testFirst()
22 $header = AcceptHeader::fromString('text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c');
23 $this->assertSame('text/html', $header->first()->getValue());
27 * @dataProvider provideFromStringData
29 public function testFromString($string, array $items)
31 $header = AcceptHeader::fromString($string);
32 $parsed = array_values($header->all());
33 // reset index since the fixtures don't have them set
34 foreach ($parsed as $item) {
37 $this->assertEquals($items, $parsed);
40 public function provideFromStringData()
44 array('gzip', array(new AcceptHeaderItem('gzip'))),
45 array('gzip,deflate,sdch', array(new AcceptHeaderItem('gzip'), new AcceptHeaderItem('deflate'), new AcceptHeaderItem('sdch'))),
46 array("gzip, deflate\t,sdch", array(new AcceptHeaderItem('gzip'), new AcceptHeaderItem('deflate'), new AcceptHeaderItem('sdch'))),
47 array('"this;should,not=matter"', array(new AcceptHeaderItem('this;should,not=matter'))),
52 * @dataProvider provideToStringData
54 public function testToString(array $items, $string)
56 $header = new AcceptHeader($items);
57 $this->assertEquals($string, (string) $header);
60 public function provideToStringData()
64 array(array(new AcceptHeaderItem('gzip')), 'gzip'),
65 array(array(new AcceptHeaderItem('gzip'), new AcceptHeaderItem('deflate'), new AcceptHeaderItem('sdch')), 'gzip,deflate,sdch'),
66 array(array(new AcceptHeaderItem('this;should,not=matter')), 'this;should,not=matter'),
71 * @dataProvider provideFilterData
73 public function testFilter($string, $filter, array $values)
75 $header = AcceptHeader::fromString($string)->filter($filter);
76 $this->assertEquals($values, array_keys($header->all()));
79 public function provideFilterData()
82 array('fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4', '/fr.*/', array('fr-FR', 'fr')),
87 * @dataProvider provideSortingData
89 public function testSorting($string, array $values)
91 $header = AcceptHeader::fromString($string);
92 $this->assertEquals($values, array_keys($header->all()));
95 public function provideSortingData()
98 'quality has priority' => array('*;q=0.3,ISO-8859-1,utf-8;q=0.7', array('ISO-8859-1', 'utf-8', '*')),
99 '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', '*')),
100 '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', '*')),