Completed
Push — master ( 4adb8e...15c2d3 )
by Mārtiņš
8s
created

ContentTypeTest::provideMimeMatches()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
4
namespace Fracture\Http\Headers;
5
6
use Exception;
7
use ReflectionClass;
8
use PHPUnit_Framework_TestCase;
9
10
class ContentTypeTest extends PHPUnit_Framework_TestCase
11
{
12
13
    /**
14
     * @covers Fracture\Http\Headers\ContentType::__construct
15
     * @covers Fracture\Http\Headers\ContentType::prepare
16
     * @covers Fracture\Http\Headers\ContentType::getParsedData
17
     */
18
    public function testEmptyInstance()
19
    {
20
        $instance = new ContentType;
21
        $instance->prepare();
22
        $this->assertEquals(null, $instance->getParsedData());
23
    }
24
25
26
    /**
27
     * @covers Fracture\Http\Headers\ContentType::__construct
28
     * @covers Fracture\Http\Headers\ContentType::prepare
29
     * @covers Fracture\Http\Headers\ContentType::contains
30
     *
31
     * @covers Fracture\Http\Headers\ContentType::extractData
32
     */
33
    public function testPreparedResult()
34
    {
35
        $instance = new ContentType('text/html');
36
        $instance->prepare();
37
38
        $this->assertTrue($instance->contains('text/html'));
39
        $this->assertFalse($instance->contains('image/png'));
40
    }
41
42
43
    /**
44
     * @covers Fracture\Http\Headers\ContentType::__construct
45
     * @covers Fracture\Http\Headers\ContentType::prepare
46
     * @covers Fracture\Http\Headers\ContentType::contains
47
     *
48
     * @covers Fracture\Http\Headers\ContentType::extractData
49
     */
50
    public function testPreparedCustomTypeResult()
51
    {
52
        $instance = new ContentType('application/json;version=3');
53
        $instance->prepare();
54
55
        $this->assertTrue($instance->contains('application/json'));
56
        $this->assertFalse($instance->contains('application/json;version=3'));
57
    }
58
59
60
    /**
61
     * @covers Fracture\Http\Headers\ContentType::__construct
62
     * @covers Fracture\Http\Headers\ContentType::setValue
63
     * @covers Fracture\Http\Headers\ContentType::prepare
64
     * @covers Fracture\Http\Headers\ContentType::contains
65
     *
66
     * @covers Fracture\Http\Headers\ContentType::extractData
67
     */
68
    public function testPreparedResultAterManualAlteration()
69
    {
70
        $instance = new ContentType('application/json');
71
        $instance->setValue('image/png');
72
        $instance->prepare();
73
74
        $this->assertTrue($instance->contains('image/png'));
75
        $this->assertFalse($instance->contains('text/html'));
76
    }
77
78
79
    /**
80
     * @covers Fracture\Http\Headers\ContentType::__construct
81
     * @covers Fracture\Http\Headers\ContentType::setValue
82
     * @covers Fracture\Http\Headers\ContentType::prepare
83
     * @covers Fracture\Http\Headers\ContentType::getParsedData
84
     *
85
     * @covers Fracture\Http\Headers\ContentType::extractData
86
     *
87
     * @dataProvider provideVariousInputs
88
     */
89
    public function testVariousInputs($expected, $parameter)
90
    {
91
        $instance = new ContentType;
92
        $instance->setValue($parameter);
93
        $instance->prepare();
94
95
        $this->assertEquals($expected, $instance->getParsedData());
96
    }
97
98
99
    public function provideVariousInputs()
100
    {
101
        return [
102
            [
103
                'expected' => ['value' => 'application/json'],
104
                'data' => 'application/json',
105
            ],
106
            [
107
                'expected' => ['value' => 'application/json', 'version' => '1'],
108
                'data' => 'application/json;version=1',
109
            ],
110
            [
111
                'expected' => ['value' => 'text/html', 'charset' => 'utf-8'],
112
                'data' => 'text/html; charset=utf-8',
113
            ],
114
            [
115
                'expected' => ['value' => 'multipart/form-data', 'boundary' => 'AaB03x'],
116
                'data' => 'multipart/form-data; boundary=AaB03x',
117
            ],
118
            [
119
                'expected' => [],
120
                'data' => '; ',
121
            ],
122
        ];
123
    }
124
125
126
    /**
127
     * @covers Fracture\Http\Headers\ContentType::__construct
128
     * @covers Fracture\Http\Headers\ContentType::getName
129
     */
130
    public function testGivenName()
131
    {
132
        $instance = new ContentType;
133
        $this->assertSame('Content-Type', $instance->getName());
134
    }
135
136
137
    /**
138
     * @covers Fracture\Http\Headers\ContentType::__construct
139
     * @covers Fracture\Http\Headers\ContentType::prepare
140
     * @covers Fracture\Http\Headers\ContentType::match
141
     *
142
     * @covers Fracture\Http\Headers\ContentType::isCompatible
143
     * @covers Fracture\Http\Headers\ContentType::replaceStars
144
     *
145
     * @dataProvider provideMimeMatches
146
     */
147
    public function testMimeMatches($expected, $value, $match)
148
    {
149
        $instance = new ContentType;
150
        $instance->setValue($value);
151
        $instance->prepare();
152
        $this->assertEquals($expected, $instance->match($match));
153
    }
154
155
156
    public function provideMimeMatches()
157
    {
158
        return [
159
            [
160
                'expected' => true,
161
                'value' => 'text/html',
162
                'match' => 'text/html',
163
            ],
164
            [
165
                'expected' => false,
166
                'value' => 'text/html',
167
                'match' => 'text/plain',
168
            ],
169
            [
170
                'expected' => true,
171
                'value' => 'text/html',
172
                'match' => 'text/*',
173
            ],
174
            [
175
                'expected' => false,
176
                'value' => 'text/html',
177
                'match' => 'application/*',
178
            ],
179
            [
180
                'expected' => true,
181
                'value' => 'application/json',
182
                'match' => '*/*',
183
            ],
184
        ];
185
    }
186
}
187