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\AcceptHeaderItem;
17 class AcceptHeaderItemTest extends TestCase
20 * @dataProvider provideFromStringData
22 public function testFromString($string, $value, array $attributes)
24 $item = AcceptHeaderItem::fromString($string);
25 $this->assertEquals($value, $item->getValue());
26 $this->assertEquals($attributes, $item->getAttributes());
29 public function provideFromStringData()
37 '"this;should,not=matter"',
38 'this;should,not=matter', array(),
41 "text/plain; charset=utf-8;param=\"this;should,not=matter\";\tfootnotes=true",
42 'text/plain', array('charset' => 'utf-8', 'param' => 'this;should,not=matter', 'footnotes' => 'true'),
45 '"this;should,not=matter";charset=utf-8',
46 'this;should,not=matter', array('charset' => 'utf-8'),
52 * @dataProvider provideToStringData
54 public function testToString($value, array $attributes, $string)
56 $item = new AcceptHeaderItem($value, $attributes);
57 $this->assertEquals($string, (string) $item);
60 public function provideToStringData()
68 'text/plain', array('charset' => 'utf-8', 'param' => 'this;should,not=matter', 'footnotes' => 'true'),
69 'text/plain;charset=utf-8;param="this;should,not=matter";footnotes=true',
74 public function testValue()
76 $item = new AcceptHeaderItem('value', array());
77 $this->assertEquals('value', $item->getValue());
79 $item->setValue('new value');
80 $this->assertEquals('new value', $item->getValue());
83 $this->assertEquals('1', $item->getValue());
86 public function testQuality()
88 $item = new AcceptHeaderItem('value', array());
89 $this->assertEquals(1.0, $item->getQuality());
91 $item->setQuality(0.5);
92 $this->assertEquals(0.5, $item->getQuality());
94 $item->setAttribute('q', 0.75);
95 $this->assertEquals(0.75, $item->getQuality());
96 $this->assertFalse($item->hasAttribute('q'));
99 public function testAttribute()
101 $item = new AcceptHeaderItem('value', array());
102 $this->assertEquals(array(), $item->getAttributes());
103 $this->assertFalse($item->hasAttribute('test'));
104 $this->assertNull($item->getAttribute('test'));
105 $this->assertEquals('default', $item->getAttribute('test', 'default'));
107 $item->setAttribute('test', 'value');
108 $this->assertEquals(array('test' => 'value'), $item->getAttributes());
109 $this->assertTrue($item->hasAttribute('test'));
110 $this->assertEquals('value', $item->getAttribute('test'));
111 $this->assertEquals('value', $item->getAttribute('test', 'default'));