Passed
Pull Request — master (#151)
by
unknown
02:10
created

HeaderHelperTest::testValueAndParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\Helper;
4
5
use Nyholm\Psr7\Response;
6
use Nyholm\Psr7\ServerRequest;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use Yiisoft\Di\Container;
11
use Yiisoft\Yii\Web\Helper\HeaderHelper;
12
use Yiisoft\Yii\Web\Middleware\ActionCaller;
13
use PHPUnit\Framework\TestCase;
14
15
class HeaderHelperTest extends TestCase
16
{
17
    public function valueAndParametersDataProvider(): array
18
    {
19
        return [
20
            'empty' => ['', []],
21
            'noParams' => ['test', ['test']],
22
            'withParams' => ['test;q=1.0;version=2', ['test', 'q' => '1.0', 'version' => '2']],
23
        ];
24
    }
25
26
    /**
27
     * @dataProvider valueAndParametersDataProvider
28
     */
29
    public function testValueAndParameters(string $input, array $expected): void
30
    {
31
        $this->assertSame($expected, HeaderHelper::getValueAndParameters($input));
32
    }
33
34
    public function qFactorSortDataProvider(): array
35
    {
36
        return [
37
            'empty' => ['', []],
38
            'emptyArray' => [[], []],
39
            'noQ' => ['text/html,text/xml', [['text/html', 'q' => 1.0], ['text/xml', 'q' => 1.0]]],
40
            'q' => ['text/html;q=0.2,text/xml', [['text/xml', 'q' => 1.0], ['text/html', 'q' => 0.2]]],
41
            'qq' => ['text/html;q=0.2,text/xml;q=0.3', [['text/xml', 'q' => 0.3], ['text/html', 'q' => 0.2]]],
42
            'qqDigits' => ['text/html;q=0.000,text/xml;q=1.000', [['text/xml', 'q' => 1.0], ['text/html', 'q' => 0.0]]],
43
            'qqDigits0.999' => ['text/html;q=0.999,text/xml;q=1.000', [['text/xml', 'q' => 1.0], ['text/html', 'q' => 0.999]]],
44
        ];
45
    }
46
47
    /**
48
     * @dataProvider qFactorSortDataProvider
49
     */
50
    public function testQFactorSort($input, array $expected): void
51
    {
52
        $this->assertSame($expected, HeaderHelper::getByQFactorSortedList($input));
53
    }
54
55
    public function qFactorSortFailDataProvider(): array
56
    {
57
        return [
58
            'qTooBig' => ['text/xml;q=1.001', \InvalidArgumentException::class],
59
            'qTooBig2' => ['text/xml;q=2', \InvalidArgumentException::class],
60
            'qInvalidDigits' => ['text/xml;q=0.0000', \InvalidArgumentException::class],
61
            'qInvalidDigits2' => ['text/xml;q=1.0000', \InvalidArgumentException::class],
62
            'int' => [3, \InvalidArgumentException::class],
63
            'float' => [3.0, \InvalidArgumentException::class],
64
            'request' => [new ServerRequest('get', '/'), \InvalidArgumentException::class],
65
        ];
66
    }
67
68
    /**
69
     * @dataProvider qFactorSortFailDataProvider
70
     */
71
    public function testQFactorSortFail($input, string $expected): void
72
    {
73
        $this->expectException($expected);
74
        HeaderHelper::getByQFactorSortedList($input);
75
    }
76
77
    public function sortedAcceptTypesDataProvider(): array
78
    {
79
        return [
80
            'empty' => ['', []],
81
            'emptyArray' => [[], []],
82
            'noQ' => ['text/html,text/xml', ['text/html', 'text/xml']],
83
            'q1' => ['text/html;q=1,text/xml', ['text/html', 'text/xml']],
84
            'q1End' => ['text/html,text/xml;q=1', ['text/html', 'text/xml']],
85
            'forward' => ['text/html;q=0.2,text/xml', ['text/xml', 'text/html']],
86
            'forward2' => ['text/html;q=0.4,text/xml,text/plain;q=0.8', ['text/xml', 'text/plain', 'text/html']],
87
            'specType' => ['text/html,text/html;version=2', ['text/html;version=2', 'text/html']],
88
            'specTypeQ' => ['text/html;q=0.3,text/html;version=2;q=0.2', ['text/html', 'text/html;version=2']],
89
            'qSame' => ['text/html;q=0.4,text/xml;q=0.4', ['text/html', 'text/xml']],
90
            'specFormatOrder' => [
91
                'text/html;version=2;a=b,text/html;version=2;a=a',
92
                ['text/html;a=b;version=2', 'text/html;a=a;version=2']
93
            ],
94
            'serverRequest' => [
95
                new ServerRequest('get', '/', ['accept' => ['text/html;q=0.1', 'text/xml']]),
96
                ['text/xml', 'text/html'],
97
            ],
98
            'wildcard' => [ // https://tools.ietf.org/html/rfc7231#section-5.3.2
99
                'text/*, text/plain, text/plain;format=flowed, */*',
100
                [
101
                    'text/plain;format=flowed',
102
                    'text/plain',
103
                    'text/*',
104
                    '*/*',
105
                ],
106
            ],
107
        ];
108
    }
109
110
    /**
111
     * @dataProvider sortedAcceptTypesDataProvider
112
     */
113
    public function testSortedAcceptTypes($input, array $expected): void
114
    {
115
        $this->assertSame($expected, HeaderHelper::getSortedAcceptTypes($input));
116
    }
117
}
118