Skip to content

Commit 967ea33

Browse files
committed
Merge branch 'update' into main
2 parents 7fd91eb + 334bc47 commit 967ea33

File tree

2 files changed

+76
-54
lines changed

2 files changed

+76
-54
lines changed

Diff for: src/FileSize.php

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public function __construct($size = null, $base = 2, $decimalMark = '.')
6666
*/
6767
private function sizeToBytes($size)
6868
{
69+
if (is_int($size)) return $size;
70+
6971
$object = SizeStringParser::parse($size);
7072
$value = $this->toFloatValue($object->value);
7173
$unit = $object->unit ?? UnitMap::BYTE;

Diff for: tests/FileSizeTest.php

+74-54
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,49 @@
1010
class FileSizeTest extends TestCase
1111
{
1212
/**
13-
* @test numeric string input.
13+
* @test
1414
*/
15-
public function bytes()
15+
public function base_two_conversions_are_accurate()
16+
{
17+
$size = new FileSize(10921134, 2);
18+
19+
$this->assertSame($size->asAuto(), '10.42 MB');
20+
}
21+
22+
/**
23+
* @test
24+
*/
25+
public function base_ten_conversions_are_accurate()
26+
{
27+
$size = new FileSize(10921134, 10);
28+
29+
$this->assertSame($size->asAuto(), '10.92 MB');
30+
}
31+
32+
/**
33+
* @test
34+
*/
35+
public function bytes_are_returned_as_an_integer()
1636
{
1737
$size = new FileSize('128974848');
1838

1939
$this->assertSame($size->as('B'), 128974848);
2040
}
2141

2242
/**
23-
* @test "partial bytes" are rounded up.
43+
* @test
2444
*/
25-
public function bytesRounding()
45+
public function partial_bytes_are_rounded_up()
2646
{
2747
$size = new FileSize('99.7 bytes');
2848

2949
$this->assertSame($size->as('B'), 100);
3050
}
3151

3252
/**
33-
* @test #add.
53+
* @test
3454
*/
35-
public function add()
55+
public function sizes_can_be_added()
3656
{
3757
$size = new FileSize('123 megabytes');
3858
$size->add('150 KiB');
@@ -41,64 +61,64 @@ public function add()
4161
}
4262

4363
/**
44-
* @test #add with a negative value.
64+
* @test
4565
*/
46-
public function addNegative()
66+
public function negative_sizes_can_be_added()
4767
{
48-
$size = new FileSize('10MB');
49-
$size->add('-20MB');
68+
$size = new FileSize('10 MB');
69+
$size->add('-20 MB');
5070

5171
$this->assertSame($size->asAuto(), '-10 MB');
5272
}
5373

5474
/**
55-
* @test #subtract.
75+
* @test
5676
*/
57-
public function subtract()
77+
public function sizes_can_be_subtracted()
5878
{
59-
$size = new FileSize('123M');
79+
$size = new FileSize('123 M');
6080
$size->subtract('150 kilobytes');
6181

6282
$this->assertSame($size->as('B'), 128821248);
6383
}
6484

6585
/**
66-
* @test #subtract with a negative value.
86+
* @test
6787
*/
68-
public function subtractNegative()
88+
public function negative_sizes_can_be_subtracted()
6989
{
70-
$size = new FileSize('10MB');
71-
$size->subtract('-20MB');
90+
$size = new FileSize('10 MB');
91+
$size->subtract('-20 MB');
7292

7393
$this->assertSame($size->asAuto(), '30 MB');
7494
}
7595

7696
/**
77-
* @test adding an array of items.
97+
* @test
7898
*/
79-
public function addMany()
99+
public function arrays_can_be_added()
80100
{
81101
$size = new FileSize();
82102
$size->add(['50mb', '140mb', '1.2mb']);
83103

84-
$this->assertSame($size->as('MB'), (float) 191.2);
104+
$this->assertSame($size->as('MB'), 191.2);
85105
}
86106

87107
/**
88-
* @test #multiplyBy.
108+
* @test
89109
*/
90-
public function multiplyBy()
110+
public function sizes_can_be_multiplied()
91111
{
92112
$size = new FileSize('425.51 m');
93113
$size->multiplyBy(9.125);
94114

95-
$this->assertSame($size->as('GB'), (float) 3.79);
115+
$this->assertSame($size->as('GB'), 3.79);
96116
}
97117

98118
/**
99-
* @test #divideBy.
119+
* @test
100120
*/
101-
public function divideBy()
121+
public function sizes_can_be_divided()
102122
{
103123
$size = new FileSize('300K');
104124
$size->divideBy(2);
@@ -107,39 +127,39 @@ public function divideBy()
107127
}
108128

109129
/**
110-
* @test upward unit conversion.
130+
* @test
111131
*/
112-
public function convertUp()
132+
public function sizes_can_be_converted_up()
113133
{
114134
$size = new FileSize('123456789 TB');
115135

116136
$this->assertSame($size->as('exabytes'), 5.74);
117137
}
118138

119139
/**
120-
* @test downward unit conversion.
140+
* @test
121141
*/
122-
public function convertDown()
142+
public function sizes_can_be_converted_down()
123143
{
124-
$size = new FileSize('1 Gigabyte');
144+
$size = new FileSize('1 GB');
125145

126-
$this->assertSame($size->as('B'), 1073741824);
146+
$this->assertSame($size->as('megabytes'), (float) 1024);
127147
}
128148

129149
/**
130-
* @test when the unit has not changed.
150+
* @test
131151
*/
132-
public function noConvert()
152+
public function size_value_is_unchanged_without_conversion()
133153
{
134-
$size = new FileSize('525 Gibibytes');
154+
$size = new FileSize('525 GB');
135155

136156
$this->assertSame($size->as('GB'), (float) 525);
137157
}
138158

139159
/**
140-
* @test auto-formatting for a small value.
160+
* @test
141161
*/
142-
public function autoSmall()
162+
public function friendly_formatting_is_valid_for_small_values()
143163
{
144164
$size = new FileSize('1.2345 KB');
145165
$size->divideBy(3);
@@ -148,52 +168,52 @@ public function autoSmall()
148168
}
149169

150170
/**
151-
* @test auto-formatting for a large value.
171+
* @test
152172
*/
153-
public function autoLarge()
173+
public function friendly_formatting_is_valid_for_large_values()
154174
{
155175
$size = new FileSize('1234522678.12 KB');
156176

157177
$this->assertSame($size->asAuto(), '1.15 TB');
158178
}
159179

160180
/**
161-
* @test the rounding in auto-formatting (should not leave trailing zeros).
181+
* @test
162182
*/
163-
public function autoRounding()
183+
public function custom_decimal_mark_is_supported()
164184
{
165-
$size = new FileSize('158.1983 mb');
185+
$size = new FileSize(10921134, 10, ',');
166186

167-
$this->assertSame($size->asAuto(), '158.2 MB');
187+
$this->assertSame($size->asAuto(), '10,92 MB');
168188
}
169189

170190
/**
171-
* @test a decimal base conversion.
191+
* @test
172192
*/
173-
public function decimalBase()
193+
public function custom_decimal_and_thousands_marks_are_supported()
174194
{
175-
$size = new FileSize(10921134, 10);
195+
$size = new FileSize('1.234.522.678,12 KB', 2, ',');
176196

177-
$this->assertSame($size->asAuto(), '10.92 MB');
197+
$this->assertSame($size->asAuto(), '1,15 TB');
178198
}
179199

180200
/**
181-
* @test a custom decimal separator.
201+
* @test
182202
*/
183-
public function decimalMark()
203+
public function smallest_integer_is_supported()
184204
{
185-
$size = new FileSize(10921134, 10, ',');
205+
$size = new FileSize(PHP_INT_MIN);
186206

187-
$this->assertSame($size->asAuto(), '10,92 MB');
207+
$this->assertIsNumeric($size->as('YB'));
188208
}
189209

190210
/**
191-
* @test custom decimal separators and thousands marks.
211+
* @test
192212
*/
193-
public function decimalAndThousandsMarks()
213+
public function largest_integer_is_supported()
194214
{
195-
$size = new FileSize('1.234.522.678,12 KB', 2, ',');
215+
$size = new FileSize(PHP_INT_MAX);
196216

197-
$this->assertSame($size->asAuto(), '1,15 TB');
217+
$this->assertIsNumeric($size->as('YB'));
198218
}
199219
}

0 commit comments

Comments
 (0)