Completed
Push — next ( 3fc9f0...5d2fc6 )
by Jonathan
9s
created

BlobObjectTest::testGetValueShort()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 3
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
15
        $b = BasicObject::blank('$v');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $string is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $encoding is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $string. This often makes code more readable.
Loading history...
90
            $string = '"'.$string.'"';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $string. This often makes code more readable.
Loading history...
91
            $string = mb_convert_encoding($string, $encoding, 'UTF-8');
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $string. This often makes code more readable.
Loading history...
92
        } else {
93
            $string = '"'.$string.'"';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $string. This often makes code more readable.
Loading history...
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();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
105
        $b = BasicObject::blank('$v');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
106
        $s = '';
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $s. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
107
        $o = $p->parse($s, $b);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
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();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
120
        $b = BasicObject::blank('$v');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
121
122
        $string = 'How now brown cow';
123
124
        $o = $p->parse($string, clone $b);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
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());
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o2. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
136
137
        $o->hints[] = 'string';
138
139
        $this->assertEquals($o, $o2);
140
        $this->assertNotSame($o, $o2);
141
        $this->assertSame($o->encoding, $o2->encoding);
0 ignored issues
show
Bug introduced by
The property encoding does not seem to exist in Kint\Object\BasicObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
142
    }
143
144
    /**
145
     * @dataProvider blobProvider
146
     * @covers \Kint\Object\BlobObject::strlen
147
     */
148
    public function testStrlen(BlobObject $object, $string, $encoding)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
213
        $b = BasicObject::blank('$v');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
214
        $o = $p->parse($string, clone $b);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
215
216
        $this->assertSame('binary string', $o->getType());
217
        $this->assertSame('"'.$string.'"', $o->getValueShort());
218
    }
219
}
220