Version 1
[yaffs-website] / vendor / symfony / http-foundation / Tests / AcceptHeaderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpFoundation\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\AcceptHeader;
16 use Symfony\Component\HttpFoundation\AcceptHeaderItem;
17
18 class AcceptHeaderTest extends TestCase
19 {
20     public function testFirst()
21     {
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());
24     }
25
26     /**
27      * @dataProvider provideFromStringData
28      */
29     public function testFromString($string, array $items)
30     {
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) {
35             $item->setIndex(0);
36         }
37         $this->assertEquals($items, $parsed);
38     }
39
40     public function provideFromStringData()
41     {
42         return array(
43             array('', array()),
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'))),
48         );
49     }
50
51     /**
52      * @dataProvider provideToStringData
53      */
54     public function testToString(array $items, $string)
55     {
56         $header = new AcceptHeader($items);
57         $this->assertEquals($string, (string) $header);
58     }
59
60     public function provideToStringData()
61     {
62         return array(
63             array(array(), ''),
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'),
67         );
68     }
69
70     /**
71      * @dataProvider provideFilterData
72      */
73     public function testFilter($string, $filter, array $values)
74     {
75         $header = AcceptHeader::fromString($string)->filter($filter);
76         $this->assertEquals($values, array_keys($header->all()));
77     }
78
79     public function provideFilterData()
80     {
81         return array(
82             array('fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4', '/fr.*/', array('fr-FR', 'fr')),
83         );
84     }
85
86     /**
87      * @dataProvider provideSortingData
88      */
89     public function testSorting($string, array $values)
90     {
91         $header = AcceptHeader::fromString($string);
92         $this->assertEquals($values, array_keys($header->all()));
93     }
94
95     public function provideSortingData()
96     {
97         return array(
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', '*')),
101         );
102     }
103 }