Test Setup Failed
Push — test ( 4e9ef0...2bbcf6 )
by Jonathan
02:51
created

BlobObjectTest::testDetectEncoding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
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::transplant
101
     */
102
    public function testTransplant()
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
107
        $string = 'How now brown cow';
108
109
        $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...
110
111
        $this->assertInstanceOf('Kint\\Object\\BlobObject', $o);
112
        if (!$o instanceof BlobObject) {
113
            return; // phpstan
114
        }
115
116
        $this->assertNotFalse($o->encoding);
117
        $this->assertNotNull($o->encoding);
118
        $this->assertNotEmpty($o->encoding);
119
120
        $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...
121
122
        $o->hints[] = 'string';
123
124
        $this->assertEquals($o, $o2);
125
        $this->assertNotSame($o, $o2);
126
        $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...
127
    }
128
129
    /**
130
     * @dataProvider blobProvider
131
     * @covers \Kint\Object\BlobObject::strlen
132
     */
133
    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...
134
    {
135
        if ($encoding === false) {
136
            $this->assertEquals(strlen($string), BlobObject::strlen($string));
137
            $this->assertEquals(strlen($string), BlobObject::strlen($string, false));
138
        } else {
139
            $this->assertEquals(mb_strlen($string, $encoding), BlobObject::strlen($string));
140
            $this->assertEquals(mb_strlen($string, $encoding), BlobObject::strlen($string, $encoding));
141
        }
142
    }
143
144
    /**
145
     * @dataProvider blobProvider
146
     * @covers \Kint\Object\BlobObject::detectEncoding
147
     */
148
    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...
149
    {
150
        $this->assertEquals($encoding, BlobObject::detectEncoding($string));
151
    }
152
}
153