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