Open Monograph Press  3.3.0
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 
13 
14 use PHPUnit\Framework\TestCase;
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 
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 
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 
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 
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 }
Symfony\Component\HttpFoundation\Tests\AcceptHeaderTest\testSorting
testSorting($string, array $values)
Definition: AcceptHeaderTest.php:89
Symfony\Component\HttpFoundation\AcceptHeaderItem
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:19
Symfony\Component\HttpFoundation\Tests\AcceptHeaderTest\testFromString
testFromString($string, array $items)
Definition: AcceptHeaderTest.php:29
Symfony\Component\HttpFoundation\Tests\AcceptHeaderTest\testFilter
testFilter($string, $filter, array $values)
Definition: AcceptHeaderTest.php:73
Symfony\Component\HttpFoundation\Tests\AcceptHeaderTest\testFirst
testFirst()
Definition: AcceptHeaderTest.php:20
Symfony\Component\HttpFoundation\AcceptHeader\fromString
static fromString($headerValue)
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:59
Symfony\Component\HttpFoundation\Tests\AcceptHeaderTest\testToString
testToString(array $items, $string)
Definition: AcceptHeaderTest.php:54
Symfony\Component\HttpFoundation\Tests\AcceptHeaderTest\provideFilterData
provideFilterData()
Definition: AcceptHeaderTest.php:79
Symfony\Component\HttpFoundation\Tests\AcceptHeaderTest\provideFromStringData
provideFromStringData()
Definition: AcceptHeaderTest.php:40
Symfony\Component\HttpFoundation\Tests\AcceptHeaderTest\provideSortingData
provideSortingData()
Definition: AcceptHeaderTest.php:95
Symfony\Component\HttpFoundation\Tests\AcceptHeaderTest
Definition: AcceptHeaderTest.php:18
Symfony\Component\HttpFoundation\Tests
Definition: AcceptHeaderItemTest.php:12
Symfony\Component\HttpFoundation\Tests\AcceptHeaderTest\provideToStringData
provideToStringData()
Definition: AcceptHeaderTest.php:60
Symfony\Component\HttpFoundation\AcceptHeader
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:22