|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kint\Test; |
|
4
|
|
|
|
|
5
|
|
|
use Kint\Utils; |
|
6
|
|
|
use PHPUnit_Framework_TestCase; |
|
7
|
|
|
|
|
8
|
|
|
class UtilsTest extends PHPUnit_Framework_TestCase |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
protected $composer_stash; |
|
|
|
|
|
|
11
|
|
|
protected $installed_stash; |
|
|
|
|
|
|
12
|
|
|
protected $composer_test_dir; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
public function humanReadableBytesProvider() |
|
15
|
|
|
{ |
|
16
|
|
|
return array( |
|
17
|
|
|
'1 byte' => array( |
|
18
|
|
|
1, |
|
19
|
|
|
array( |
|
20
|
|
|
'value' => 1.0, |
|
21
|
|
|
'unit' => 'B', |
|
22
|
|
|
), |
|
23
|
|
|
), |
|
24
|
|
|
'1023 bytes' => array( |
|
25
|
|
|
1023, |
|
26
|
|
|
array( |
|
27
|
|
|
'value' => 1023.0, |
|
28
|
|
|
'unit' => 'B', |
|
29
|
|
|
), |
|
30
|
|
|
), |
|
31
|
|
|
'1024 bytes' => array( |
|
32
|
|
|
1024, |
|
33
|
|
|
array( |
|
34
|
|
|
'value' => 1.0, |
|
35
|
|
|
'unit' => 'KB', |
|
36
|
|
|
), |
|
37
|
|
|
), |
|
38
|
|
|
'1 meg' => array( |
|
39
|
|
|
pow(2, 20), |
|
40
|
|
|
array( |
|
41
|
|
|
'value' => 1.0, |
|
42
|
|
|
'unit' => 'MB', |
|
43
|
|
|
), |
|
44
|
|
|
), |
|
45
|
|
|
'1 gig' => array( |
|
46
|
|
|
pow(2, 30), |
|
47
|
|
|
array( |
|
48
|
|
|
'value' => 1.0, |
|
49
|
|
|
'unit' => 'GB', |
|
50
|
|
|
), |
|
51
|
|
|
), |
|
52
|
|
|
'1 tig' => array( |
|
53
|
|
|
pow(2, 40), |
|
54
|
|
|
array( |
|
55
|
|
|
'value' => 1.0, |
|
56
|
|
|
'unit' => 'TB', |
|
57
|
|
|
), |
|
58
|
|
|
), |
|
59
|
|
|
'>1 tig' => array( |
|
60
|
|
|
pow(2, 50), |
|
61
|
|
|
array( |
|
62
|
|
|
'value' => 1024.0, |
|
63
|
|
|
'unit' => 'TB', |
|
64
|
|
|
), |
|
65
|
|
|
), |
|
66
|
|
|
'halfway' => array( |
|
67
|
|
|
15000, |
|
68
|
|
|
array( |
|
69
|
|
|
'value' => 15000 / 1024, |
|
70
|
|
|
'unit' => 'KB', |
|
71
|
|
|
), |
|
72
|
|
|
), |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @covers \Kint\Utils::getHumanReadableBytes |
|
78
|
|
|
* @dataProvider humanReadableBytesProvider |
|
79
|
|
|
*/ |
|
80
|
|
|
public function testGetHumanReadableBytes($input, $expect) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->assertSame($expect, Utils::getHumanReadableBytes($input)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function sequentialArrayProvider() |
|
86
|
|
|
{ |
|
87
|
|
|
return array( |
|
88
|
|
|
'sequential array' => array( |
|
89
|
|
|
array(1, 2, 3), |
|
90
|
|
|
true, |
|
91
|
|
|
), |
|
92
|
|
|
'explicit sequential array' => array( |
|
93
|
|
|
array(0 => 1, 1 => 2, 2 => 3), |
|
94
|
|
|
true, |
|
95
|
|
|
), |
|
96
|
|
|
'arrays start at 1' => array( |
|
97
|
|
|
array(1 => 1, 2 => 2, 3 => 3), |
|
98
|
|
|
false, |
|
99
|
|
|
), |
|
100
|
|
|
'wrong order' => array( |
|
101
|
|
|
array(0 => 1, 2 => 2, 1 => 3), |
|
102
|
|
|
false, |
|
103
|
|
|
), |
|
104
|
|
|
'string keys' => array( |
|
105
|
|
|
array(0 => 1, 1 => 2, 'two' => 3), |
|
106
|
|
|
false, |
|
107
|
|
|
), |
|
108
|
|
|
'string int keys' => array( |
|
109
|
|
|
array('0' => 1, '1' => 2, '2' => 3), |
|
110
|
|
|
true, |
|
111
|
|
|
), |
|
112
|
|
|
'padded string int keys' => array( |
|
113
|
|
|
array('00' => 1, '01' => 2, '02' => 3), |
|
114
|
|
|
false, |
|
115
|
|
|
), |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @covers \Kint\Utils::isSequential |
|
121
|
|
|
* @dataProvider sequentialArrayProvider |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testIsSequential($input, $expect) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->assertSame($expect, Utils::isSequential($input)); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function setUp() |
|
129
|
|
|
{ |
|
130
|
|
|
parent::setUp(); |
|
131
|
|
|
|
|
132
|
|
|
if (getenv('KINT_FILE')) { |
|
133
|
|
|
$this->composer_test_dir = dirname(__DIR__); |
|
134
|
|
|
} else { |
|
135
|
|
|
$this->composer_test_dir = KINT_DIR; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$this->composer_stash = file_get_contents($this->composer_test_dir.'/composer.json'); |
|
139
|
|
|
$this->installed_stash = file_get_contents($this->composer_test_dir.'/vendor/composer/installed.json'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function tearDown() |
|
143
|
|
|
{ |
|
144
|
|
|
parent::tearDown(); |
|
145
|
|
|
|
|
146
|
|
|
if ($this->composer_stash) { |
|
147
|
|
|
file_put_contents($this->composer_test_dir.'/composer.json', $this->composer_stash); |
|
148
|
|
|
file_put_contents($this->composer_test_dir.'/vendor/composer/installed.json', $this->installed_stash); |
|
149
|
|
|
$this->composer_stash = null; |
|
150
|
|
|
$this->installed_stash = null; |
|
151
|
|
|
if (file_exists($this->composer_test_dir.'/composer/installed.json')) { |
|
152
|
|
|
unlink($this->composer_test_dir.'/composer/installed.json'); |
|
153
|
|
|
} |
|
154
|
|
|
if (file_exists($this->composer_test_dir.'/composer')) { |
|
155
|
|
|
rmdir($this->composer_test_dir.'/composer'); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* This is a flimsy test but it's as good as it gets without altering |
|
162
|
|
|
* composer.json mid-test without a proper setup/teardown in place. |
|
163
|
|
|
* |
|
164
|
|
|
* @covers \Kint\Utils::composerGetExtras |
|
165
|
|
|
*/ |
|
166
|
|
|
public function testComposerGetExtras() |
|
167
|
|
|
{ |
|
168
|
|
|
file_put_contents($this->composer_test_dir.'/composer.json', json_encode(array( |
|
169
|
|
|
'extra' => array( |
|
170
|
|
|
'kint' => array('test' => 'data'), |
|
171
|
|
|
), |
|
172
|
|
|
))); |
|
173
|
|
|
|
|
174
|
|
|
if (getenv('KINT_FILE')) { |
|
175
|
|
|
$this->assertEquals(array(), Utils::composerGetExtras('kint')); |
|
176
|
|
|
|
|
177
|
|
|
return; |
|
178
|
|
|
} else { |
|
179
|
|
|
$this->assertEquals(array('test' => 'data'), Utils::composerGetExtras('kint')); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
mkdir($this->composer_test_dir.'/composer'); |
|
183
|
|
|
unlink($this->composer_test_dir.'/vendor/composer/installed.json'); |
|
184
|
|
|
|
|
185
|
|
|
file_put_contents($this->composer_test_dir.'/composer/installed.json', json_encode(array( |
|
186
|
|
|
array( |
|
187
|
|
|
'extra' => array( |
|
188
|
|
|
'kint' => array('more' => 'test', 'data'), |
|
189
|
|
|
), |
|
190
|
|
|
), |
|
191
|
|
|
array( |
|
192
|
|
|
'extra' => array( |
|
193
|
|
|
'kint' => array('test' => 'ing'), |
|
194
|
|
|
), |
|
195
|
|
|
), |
|
196
|
|
|
))); |
|
197
|
|
|
|
|
198
|
|
|
$this->assertEquals(array('more' => 'test', 'data', 'test' => 'ing'), Utils::composerGetExtras('kint')); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.