Completed
Pull Request — master (#151)
by
unknown
02:09
created

HeaderHelperTest::valueAndParametersDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
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
18
    public function valueAndParametersDataProvider(): array
19
    {
20
        return [
21
            'empty' => ['', []],
22
            'noParams' => ['test', ['test']],
23
            'withParams' => ['test;q=1.0;version=2', ['test', 'q' => '1.0', 'version' => '2']],
24
        ];
25
    }
26
27
    /**
28
     * @dataProvider valueAndParametersDataProvider
29
     */
30
    public function testValueAndParameters(string $input, array $expected): void
31
    {
32
        $this->assertSame($expected, HeaderHelper::getValueAndParameters($input));
33
    }
34
35
    public function qFactorSortDataProvider(): array
36
    {
37
        return [
38
            'empty' => ['', false, []],
39
            'emptyArray' => [[], false, []],
40
            'noQ' => ['text/html,text/xml', false, [['text/html', 'q' => 1.0], ['text/xml', 'q' => 1.0]]],
41
            'noQParams' => ['text/html,text/xml', true, ['text/html', 'text/xml']],
42
            'q' => ['text/html;q=0.2,text/xml', false, [['text/xml', 'q' => 1.0], ['text/html', 'q' => 0.2]]],
43
            'qq' => ['text/html;q=0.2,text/xml;q=0.3', false, [['text/xml', 'q' => 0.3], ['text/html', 'q' => 0.2]]],
44
            'qqValues' => ['text/html;q=0.2,text/xml;q=0.3', true, ['text/xml', 'text/html']],
45
        ];
46
    }
47
48
    /**
49
     * @dataProvider qFactorSortDataProvider
50
     */
51
    public function testQFactorSort($input, bool $valuesOnly, array $expected): void
52
    {
53
        $this->assertSame($expected, HeaderHelper::getByQFactorSortedList($input, $valuesOnly));
54
    }
55
56
    public function qFactorSortFailDataProvider(): array
57
    {
58
        return [
59
            'qTooBig' => ['text/xml;q=1.001', \InvalidArgumentException::class],
60
            'qTooBig2' => ['text/xml;q=2', \InvalidArgumentException::class],
61
            'qInvalidDigits' => ['text/xml;q=0.0000', \InvalidArgumentException::class],
62
            'qInvalidDigits2' => ['text/xml;q=1.0000', \InvalidArgumentException::class],
63
            'int' => [3, \InvalidArgumentException::class],
64
            'float' => [3.0, \InvalidArgumentException::class],
65
            'request' => [new ServerRequest('get', '/'), \InvalidArgumentException::class],
66
        ];
67
    }
68
69
    /**
70
     * @dataProvider qFactorSortFailDataProvider
71
     */
72
    public function testQFactorSortFail($input, string $expected): void
73
    {
74
        $this->expectException($expected);
75
        HeaderHelper::getByQFactorSortedList($input);
76
    }
77
78
    public function sortedAcceptTypesDataProvider(): array
79
    {
80
        return [
81
            'empty' => ['', []],
82
            'emptyArray' => [[], []],
83
            'noQ' => ['text/html,text/xml', ['text/html', 'text/xml']],
84
            'q1' => ['text/html;q=1,text/xml', ['text/html', 'text/xml']],
85
            'q1End' => ['text/html,text/xml;q=1', ['text/html', 'text/xml']],
86
            'forward' => ['text/html;q=0.2,text/xml', ['text/xml', 'text/html']],
87
            'forward2' => ['text/html;q=0.4,text/xml,text/plain;q=0.8', ['text/xml', 'text/plain', 'text/html']],
88
            'specType' => ['text/html,text/html;version=2', ['text/html;version=2', 'text/html']],
89
            'specTypeQ' => ['text/html;q=0.3,text/html;version=2;q=0.2', ['text/html', 'text/html;version=2']],
90
            'qSame' => ['text/html;q=0.4,text/xml;q=0.4', ['text/html', 'text/xml']],
91
            'specFormatOrder' => [
92
                'text/html;version=2;a=b,text/html;version=2;a=a',
93
                ['text/html;a=b;version=2', 'text/html;a=a;version=2']
94
            ],
95
            'serverRequest' => [
96
                new ServerRequest('get', '/', ['accept' => ['text/html;q=0.1', 'text/xml']]),
97
                ['text/xml', 'text/html'],
98
            ],
99
            'wildcard' => [ // https://tools.ietf.org/html/rfc7231#section-5.3.2
100
                'text/*, text/plain, text/plain;format=flowed, */*',
101
                [
102
                    'text/plain;format=flowed',
103
                    'text/plain',
104
                    'text/*',
105
                    '*/*',
106
                ],
107
            ],
108
        ];
109
    }
110
111
    /**
112
     * @dataProvider sortedAcceptTypesDataProvider
113
     */
114
    public function testSortedAcceptTypes($input, array $expected): void
115
    {
116
        $this->assertSame($expected, HeaderHelper::getSortedAcceptTypes($input));
117
    }
118
}
119