|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Koded\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Koded\Http\AcceptHeaderNegotiator; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
class AcceptLanguageHeaderTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @dataProvider dataForSupportedLanguagesWithAsterisk |
|
12
|
|
|
*/ |
|
13
|
|
|
public function test_match_with_asterisk($accept, $expect, $quality) |
|
14
|
|
|
{ |
|
15
|
|
|
$negotiator = (new AcceptHeaderNegotiator('*'))->match($accept); |
|
16
|
|
|
|
|
17
|
|
|
$this->assertSame($expect, $negotiator->value(), 'Expects ' . $expect); |
|
18
|
|
|
$this->assertSame($quality, $negotiator->quality(), 'Expects q=' . $quality); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @dataProvider dataForSupportedLanguages |
|
23
|
|
|
*/ |
|
24
|
|
|
public function test_with_preferred_languages($accept, $expect, $quality) |
|
25
|
|
|
{ |
|
26
|
|
|
$negotiator = (new AcceptHeaderNegotiator('de,fr,en'))->match($accept); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertSame($expect, $negotiator->value(), 'Expects ' . $expect); |
|
29
|
|
|
$this->assertSame($quality, $negotiator->quality(), 'Expects q=' . $quality); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function test_empty_accept_and_support_headers() |
|
33
|
|
|
{ |
|
34
|
|
|
$match = (new AcceptHeaderNegotiator(''))->match(''); |
|
35
|
|
|
$this->assertEquals('', $match->value(), 'Empty headers returns empty matched value'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function dataForSupportedLanguagesWithAsterisk() |
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
|
|
['*;q=0.5, fr;q=0.9, en;q=0.8, de;q=0.7', 'fr', 0.9], |
|
42
|
|
|
['en-US,en;q=0.5', 'en-US', 1.0], |
|
43
|
|
|
['*', '*', 1.0] |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function dataForSupportedLanguages() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
['fr;q=0.7, en;q=0.8, de;q=0.9, *;q=0.5', 'de', 0.9], |
|
51
|
|
|
['en-US,en;q=0.5', 'en', 0.5], |
|
52
|
|
|
['*', 'de', 1.0] |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|