[HttpFoundation] Fix AcceptHeader

This commit is contained in:
Victor Berchet 2012-11-13 17:18:54 +01:00
parent b126664406
commit 395c00487c
3 changed files with 83 additions and 77 deletions

View File

@ -146,7 +146,7 @@ class AcceptHeader
{
$this->sort();
return !empty($this->items) ? current($this->items) : null;
return !empty($this->items) ? $this->items[0] : null;
}
/**

View File

@ -25,6 +25,28 @@ class AcceptHeaderItemTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($attributes, $item->getAttributes());
}
public function provideFromStringData()
{
return array(
array(
'text/html',
'text/html', array()
),
array(
'"this;should,not=matter"',
'this;should,not=matter', array()
),
array(
"text/plain; charset=utf-8;param=\"this;should,not=matter\";\tfootnotes=true",
'text/plain', array('charset' => 'utf-8', 'param' => 'this;should,not=matter', 'footnotes' => 'true')
),
array(
'"this;should,not=matter";charset=utf-8',
'this;should,not=matter', array('charset' => 'utf-8')
),
);
}
/**
* @dataProvider provideToStringData
*/
@ -34,6 +56,20 @@ class AcceptHeaderItemTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($string, (string) $item);
}
public function provideToStringData()
{
return array(
array(
'text/html', array(),
'text/html'
),
array(
'text/plain', array('charset' => 'utf-8', 'param' => 'this;should,not=matter', 'footnotes' => 'true'),
'text/plain;charset=utf-8;param="this;should,not=matter";footnotes=true'
),
);
}
public function testValue()
{
$item = new AcceptHeaderItem('value', array());
@ -73,40 +109,4 @@ class AcceptHeaderItemTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('value', $item->getAttribute('test'));
$this->assertEquals('value', $item->getAttribute('test', 'default'));
}
public function provideFromStringData()
{
return array(
array(
'text/html',
'text/html', array()
),
array(
'"this;should,not=matter"',
'this;should,not=matter', array()
),
array(
"text/plain; charset=utf-8;param=\"this;should,not=matter\";\tfootnotes=true",
'text/plain', array('charset' => 'utf-8', 'param' => 'this;should,not=matter', 'footnotes' => 'true')
),
array(
'"this;should,not=matter";charset=utf-8',
'this;should,not=matter', array('charset' => 'utf-8')
),
);
}
public function provideToStringData()
{
return array(
array(
'text/html', array(),
'text/html'
),
array(
'text/plain', array('charset' => 'utf-8', 'param' => 'this;should,not=matter', 'footnotes' => 'true'),
'text/plain;charset=utf-8;param="this;should,not=matter";footnotes=true'
),
);
}
}

View File

@ -16,6 +16,12 @@ use Symfony\Component\HttpFoundation\AcceptHeaderItem;
class AcceptHeaderTest extends \PHPUnit_Framework_TestCase
{
public function testFirst()
{
$header = AcceptHeader::fromString('text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c');
$this->assertSame('text/html', $header->first()->getValue());
}
/**
* @dataProvider provideFromStringData
*/
@ -30,33 +36,6 @@ class AcceptHeaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($items, $parsed);
}
/**
* @dataProvider provideToStringData
*/
public function testToString(array $items, $string)
{
$header = new AcceptHeader($items);
$this->assertEquals($string, (string) $header);
}
/**
* @dataProvider provideFilterData
*/
public function testFilter($string, $filter, array $values)
{
$header = AcceptHeader::fromString($string)->filter($filter);
$this->assertEquals($values, array_keys($header->all()));
}
/**
* @dataProvider provideSortingData
*/
public function testSorting($string, array $values)
{
$header = AcceptHeader::fromString($string);
$this->assertEquals($values, array_keys($header->all()));
}
public function provideFromStringData()
{
return array(
@ -68,20 +47,13 @@ class AcceptHeaderTest extends \PHPUnit_Framework_TestCase
);
}
public function provideFilterData()
/**
* @dataProvider provideToStringData
*/
public function testToString(array $items, $string)
{
return array(
array('fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4', '/fr.*/', array('fr-FR', 'fr')),
);
}
public function provideSortingData()
{
return array(
'quality has priority' => array('*;q=0.3,ISO-8859-1,utf-8;q=0.7', array('ISO-8859-1', 'utf-8', '*')),
'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', '*')),
'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', '*')),
);
$header = new AcceptHeader($items);
$this->assertEquals($string, (string) $header);
}
public function provideToStringData()
@ -93,4 +65,38 @@ class AcceptHeaderTest extends \PHPUnit_Framework_TestCase
array(array(new AcceptHeaderItem('this;should,not=matter')), 'this;should,not=matter'),
);
}
/**
* @dataProvider provideFilterData
*/
public function testFilter($string, $filter, array $values)
{
$header = AcceptHeader::fromString($string)->filter($filter);
$this->assertEquals($values, array_keys($header->all()));
}
public function provideFilterData()
{
return array(
array('fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4', '/fr.*/', array('fr-FR', 'fr')),
);
}
/**
* @dataProvider provideSortingData
*/
public function testSorting($string, array $values)
{
$header = AcceptHeader::fromString($string);
$this->assertEquals($values, array_keys($header->all()));
}
public function provideSortingData()
{
return array(
'quality has priority' => array('*;q=0.3,ISO-8859-1,utf-8;q=0.7', array('ISO-8859-1', 'utf-8', '*')),
'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', '*')),
'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', '*')),
);
}
}