Test Setup Failed
Push — test ( 24878d...6ecee8 )
by Jonathan
02:38
created

BlobObjectTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 143
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 4
1
<?php
2
3
namespace Kint\Test\Object;
4
5
use Kint\Object\BasicObject;
6
use Kint\Object\BlobObject;
7
use Kint\Parser\Parser;
8
use Kint\Test\KintTestCase;
9
10
class BlobObjectTest extends KintTestCase
11
{
12
    public function blobProvider()
13
    {
14
        $p = new Parser();
15
        $b = BasicObject::blank('$v');
16
17
        BlobObject::$char_encodings = array(
18
            'ASCII',
19
            'UTF-8',
20
        );
21
22
        $strings = array(
23
            'empty' => array(
24
                '',
25
                'ASCII',
26
                'string',
27
            ),
28
            'ASCII' => array(
29
                "The quick brown fox jumps<br>\r\n\tover the lazy dog",
30
                'ASCII',
31
                'string',
32
            ),
33
            'UTF-8' => array(
34
                "El zorro marrón rápido salta sobre<br>\r\n\tel perro perezoso",
35
                'UTF-8',
36
                'UTF-8 string',
37
            ),
38
            'fail' => array(
39
                "The quick brown fox jumps<br>\r\n\tover the lazy dog\x90\x1b",
40
                false,
41
                'binary string',
42
            ),
43
        );
44
45
        if (!defined('HHVM_VERSION')) {
46
            BlobObject::$char_encodings[] = 'SJIS';
47
            BlobObject::$char_encodings[] = 'EUC-JP';
48
            BlobObject::$char_encodings[] = 'Windows-1252';
49
            $strings['SJIS'] = array(
50
                mb_convert_encoding("キント最強<br>\r\n\tASCII", 'SJIS', 'UTF-8'),
51
                'SJIS',
52
                'SJIS string',
53
            );
54
            $strings['EUC-JP'] = array(
55
                mb_convert_encoding("キント最強<br>\r\n\tASCII", 'EUC-JP', 'UTF-8'),
56
                'EUC-JP',
57
                'EUC-JP string',
58
            );
59
            $strings['yuck'] = array(
60
                mb_convert_encoding("El zorro marrón rápido salta sobre<br>\r\n\tel perro perezoso", 'Windows-1252', 'UTF-8'),
61
                'Windows-1252',
62
                'Windows-1252 string',
63
            );
64
        }
65
66
        foreach ($strings as $encoding => &$string) {
67
            array_unshift($string, $p->parse($string[0], clone $b));
68
        }
69
70
        return $strings;
71
    }
72
73
    /**
74
     * @dataProvider blobProvider
75
     * @covers \Kint\Object\BlobObject::getType
76
     */
77
    public function testGetType(BlobObject $object, $string, $encoding, $type)
78
    {
79
        $this->assertEquals($type, $object->getType());
80
    }
81
82
    /**
83
     * @dataProvider blobProvider
84
     * @covers \Kint\Object\BlobObject::getValueShort
85
     */
86
    public function testGetValueShort(BlobObject $object, $string, $encoding)
87
    {
88
        if ($encoding) {
89
            $string = mb_convert_encoding($string, 'UTF-8', $encoding);
90
            $string = '"'.$string.'"';
91
            $string = mb_convert_encoding($string, $encoding, 'UTF-8');
92
        } else {
93
            $string = '"'.$string.'"';
94
        }
95
96
        $this->assertEquals($string, $object->getValueShort());
97
    }
98
99
    /**
100
     * @covers \Kint\Object\BlobObject::getValueShort
101
     */
102
    public function testNoValueShort()
103
    {
104
        $p = new Parser();
105
        $b = BasicObject::blank('$v');
106
        $s = '';
107
        $o = $p->parse($s, $b);
108
109
        $this->assertEquals('""', $o->getValueShort());
110
        $o->value = null;
111
        $this->assertNull($o->getValueShort());
112
    }
113
114
    /**
115
     * @covers \Kint\Object\BlobObject::transplant
116
     */
117
    public function testTransplant()
118
    {
119
        $p = new Parser();
120
        $b = BasicObject::blank('$v');
121
122
        $string = 'How now brown cow';
123
124
        $o = $p->parse($string, clone $b);
125
126
        $this->assertInstanceOf('Kint\\Object\\BlobObject', $o);
127
        if (!$o instanceof BlobObject) {
128
            return; // phpstan
129
        }
130
131
        $this->assertNotFalse($o->encoding);
132
        $this->assertNotNull($o->encoding);
133
        $this->assertNotEmpty($o->encoding);
134
135
        $o2 = $o->transplant(new BlobObject());
136
137
        $o->hints[] = 'string';
138
139
        $this->assertEquals($o, $o2);
140
        $this->assertNotSame($o, $o2);
141
        $this->assertSame($o->encoding, $o2->encoding);
142
    }
143
144
    /**
145
     * @dataProvider blobProvider
146
     * @covers \Kint\Object\BlobObject::strlen
147
     */
148
    public function testStrlen(BlobObject $object, $string, $encoding)
149
    {
150
        if ($encoding === false) {
151
            $this->assertEquals(strlen($string), BlobObject::strlen($string));
152
            $this->assertEquals(strlen($string), BlobObject::strlen($string, false));
153
        } else {
154
            $this->assertEquals(mb_strlen($string, $encoding), BlobObject::strlen($string));
155
            $this->assertEquals(mb_strlen($string, $encoding), BlobObject::strlen($string, $encoding));
156
        }
157
    }
158
159
    /**
160
     * @dataProvider blobProvider
161
     * @covers \Kint\Object\BlobObject::substr
162
     */
163
    public function testSubstr(BlobObject $object, $string, $encoding)
164
    {
165
        $length = BlobObject::strlen($string);
166
167
        if ($encoding === false) {
168
            $this->assertEquals(
169
                substr($string, 1, $length - 1),
170
                BlobObject::substr($string, 1, $length - 1)
171
            );
172
            $this->assertEquals(
173
                substr($string, 1, $length - 1),
174
                BlobObject::substr($string, 1, $length - 1, false)
175
            );
176
        } else {
177
            $this->assertEquals(
178
                mb_substr($string, 1, $length - 1, $encoding),
179
                BlobObject::substr($string, 1, $length - 1)
180
            );
181
            $this->assertEquals(
182
                mb_substr($string, 1, $length - 1, $encoding),
183
                BlobObject::substr($string, 1, $length - 1, $encoding)
184
            );
185
        }
186
    }
187
188
    /**
189
     * @dataProvider blobProvider
190
     * @covers \Kint\Object\BlobObject::detectEncoding
191
     */
192
    public function testDetectEncoding(BlobObject $object, $string, $encoding)
193
    {
194
        $this->assertEquals($encoding, BlobObject::detectEncoding($string));
195
    }
196
197
    /**
198
     * @covers \Kint\Object\BlobObject::getType
199
     * @covers \Kint\Object\BlobObject::getValueShort
200
     * @covers \Kint\Object\BlobObject::detectEncoding
201
     */
202
    public function testWindowsDisabled()
203
    {
204
        BlobObject::$char_encodings = array(
205
            'ASCII',
206
            'UTF-8',
207
        );
208
209
        $string = mb_convert_encoding("El zorro marrón rápido salta sobre<br>\r\n\tel perro perezoso", 'Windows-1252', 'UTF-8');
210
        $this->assertFalse(BlobObject::detectEncoding($string));
211
212
        $p = new Parser();
213
        $b = BasicObject::blank('$v');
214
        $o = $p->parse($string, clone $b);
215
216
        $this->assertSame('binary string', $o->getType());
217
        $this->assertSame('"'.$string.'"', $o->getValueShort());
218
    }
219
}
220