From 3f27acaa3d3972ec3e3e9b711ba35ddc66005365 Mon Sep 17 00:00:00 2001 From: Chris Ullyott Date: Sun, 17 Oct 2021 15:11:32 -0700 Subject: [PATCH 1/4] Tidy tests --- tests/FileSizeTest.php | 98 +++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 54 deletions(-) diff --git a/tests/FileSizeTest.php b/tests/FileSizeTest.php index 29c78f0..499e8aa 100644 --- a/tests/FileSizeTest.php +++ b/tests/FileSizeTest.php @@ -10,9 +10,9 @@ class FileSizeTest extends TestCase { /** - * @test numeric string input. + * @test */ - public function bytes() + public function bytes_are_returned_as_an_integer() { $size = new FileSize('128974848'); @@ -20,9 +20,9 @@ public function bytes() } /** - * @test "partial bytes" are rounded up. + * @test */ - public function bytesRounding() + public function partial_bytes_are_rounded_up() { $size = new FileSize('99.7 bytes'); @@ -30,9 +30,9 @@ public function bytesRounding() } /** - * @test #add. + * @test */ - public function add() + public function sizes_can_be_added() { $size = new FileSize('123 megabytes'); $size->add('150 KiB'); @@ -41,64 +41,64 @@ public function add() } /** - * @test #add with a negative value. + * @test */ - public function addNegative() + public function negative_sizes_can_be_added() { - $size = new FileSize('10MB'); - $size->add('-20MB'); + $size = new FileSize('10 MB'); + $size->add('-20 MB'); $this->assertSame($size->asAuto(), '-10 MB'); } /** - * @test #subtract. + * @test */ - public function subtract() + public function sizes_can_be_subtracted() { - $size = new FileSize('123M'); + $size = new FileSize('123 M'); $size->subtract('150 kilobytes'); $this->assertSame($size->as('B'), 128821248); } /** - * @test #subtract with a negative value. + * @test */ - public function subtractNegative() + public function negative_sizes_can_be_subtracted() { - $size = new FileSize('10MB'); - $size->subtract('-20MB'); + $size = new FileSize('10 MB'); + $size->subtract('-20 MB'); $this->assertSame($size->asAuto(), '30 MB'); } /** - * @test adding an array of items. + * @test */ - public function addMany() + public function arrays_can_be_added() { $size = new FileSize(); $size->add(['50mb', '140mb', '1.2mb']); - $this->assertSame($size->as('MB'), (float) 191.2); + $this->assertSame($size->as('MB'), 191.2); } /** - * @test #multiplyBy. + * @test */ - public function multiplyBy() + public function sizes_can_be_multiplied() { $size = new FileSize('425.51 m'); $size->multiplyBy(9.125); - $this->assertSame($size->as('GB'), (float) 3.79); + $this->assertSame($size->as('GB'), 3.79); } /** - * @test #divideBy. + * @test */ - public function divideBy() + public function sizes_can_be_divided() { $size = new FileSize('300K'); $size->divideBy(2); @@ -107,9 +107,9 @@ public function divideBy() } /** - * @test upward unit conversion. + * @test */ - public function convertUp() + public function sizes_can_be_converted_up() { $size = new FileSize('123456789 TB'); @@ -117,29 +117,29 @@ public function convertUp() } /** - * @test downward unit conversion. + * @test */ - public function convertDown() + public function sizes_can_be_converted_down() { - $size = new FileSize('1 Gigabyte'); + $size = new FileSize('1 GB'); - $this->assertSame($size->as('B'), 1073741824); + $this->assertSame($size->as('megabytes'), (float) 1024); } /** - * @test when the unit has not changed. + * @test */ - public function noConvert() + public function size_value_is_unchanged_without_conversion() { - $size = new FileSize('525 Gibibytes'); + $size = new FileSize('525 GB'); $this->assertSame($size->as('GB'), (float) 525); } /** - * @test auto-formatting for a small value. + * @test */ - public function autoSmall() + public function friendly_formatting_is_valid_for_small_values() { $size = new FileSize('1.2345 KB'); $size->divideBy(3); @@ -148,9 +148,9 @@ public function autoSmall() } /** - * @test auto-formatting for a large value. + * @test */ - public function autoLarge() + public function friendly_formatting_is_valid_for_large_values() { $size = new FileSize('1234522678.12 KB'); @@ -158,19 +158,9 @@ public function autoLarge() } /** - * @test the rounding in auto-formatting (should not leave trailing zeros). + * @test */ - public function autoRounding() - { - $size = new FileSize('158.1983 mb'); - - $this->assertSame($size->asAuto(), '158.2 MB'); - } - - /** - * @test a decimal base conversion. - */ - public function decimalBase() + public function base_ten_conversions_are_accurate() { $size = new FileSize(10921134, 10); @@ -178,9 +168,9 @@ public function decimalBase() } /** - * @test a custom decimal separator. + * @test */ - public function decimalMark() + public function custom_decimal_mark_is_supported() { $size = new FileSize(10921134, 10, ','); @@ -188,9 +178,9 @@ public function decimalMark() } /** - * @test custom decimal separators and thousands marks. + * @test */ - public function decimalAndThousandsMarks() + public function custom_decimal_and_thousands_marks_are_supported() { $size = new FileSize('1.234.522.678,12 KB', 2, ','); From 19cffab61e37791971af34eeb2b6ba049acf267d Mon Sep 17 00:00:00 2001 From: Chris Ullyott Date: Sun, 17 Oct 2021 15:30:26 -0700 Subject: [PATCH 2/4] Clarify base 2, base 10 tests --- tests/FileSizeTest.php | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/tests/FileSizeTest.php b/tests/FileSizeTest.php index 499e8aa..899b00f 100644 --- a/tests/FileSizeTest.php +++ b/tests/FileSizeTest.php @@ -9,6 +9,26 @@ class FileSizeTest extends TestCase { + /** + * @test + */ + public function base_two_conversions_are_accurate() + { + $size = new FileSize(10921134, 2); + + $this->assertSame($size->asAuto(), '10.42 MB'); + } + + /** + * @test + */ + public function base_ten_conversions_are_accurate() + { + $size = new FileSize(10921134, 10); + + $this->assertSame($size->asAuto(), '10.92 MB'); + } + /** * @test */ @@ -157,16 +177,6 @@ public function friendly_formatting_is_valid_for_large_values() $this->assertSame($size->asAuto(), '1.15 TB'); } - /** - * @test - */ - public function base_ten_conversions_are_accurate() - { - $size = new FileSize(10921134, 10); - - $this->assertSame($size->asAuto(), '10.92 MB'); - } - /** * @test */ From 1245ae556bd4751159077cfedb93d0606f6adbac Mon Sep 17 00:00:00 2001 From: Chris Ullyott Date: Sun, 17 Oct 2021 15:51:27 -0700 Subject: [PATCH 3/4] Return early if size is already an integer --- src/FileSize.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/FileSize.php b/src/FileSize.php index 5bbccb4..bdfb1eb 100644 --- a/src/FileSize.php +++ b/src/FileSize.php @@ -66,6 +66,8 @@ public function __construct($size = null, $base = 2, $decimalMark = '.') */ private function sizeToBytes($size) { + if (is_int($size)) return $size; + $object = SizeStringParser::parse($size); $value = $this->toFloatValue($object->value); $unit = $object->unit ?? UnitMap::BYTE; From 334bc475b6b55bfbb279e3f9d2076bbcb4f852d5 Mon Sep 17 00:00:00 2001 From: Chris Ullyott Date: Sun, 17 Oct 2021 15:51:39 -0700 Subject: [PATCH 4/4] Add tests for smallest, largest integers --- tests/FileSizeTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/FileSizeTest.php b/tests/FileSizeTest.php index 899b00f..3860e04 100644 --- a/tests/FileSizeTest.php +++ b/tests/FileSizeTest.php @@ -196,4 +196,24 @@ public function custom_decimal_and_thousands_marks_are_supported() $this->assertSame($size->asAuto(), '1,15 TB'); } + + /** + * @test + */ + public function smallest_integer_is_supported() + { + $size = new FileSize(PHP_INT_MIN); + + $this->assertIsNumeric($size->as('YB')); + } + + /** + * @test + */ + public function largest_integer_is_supported() + { + $size = new FileSize(PHP_INT_MAX); + + $this->assertIsNumeric($size->as('YB')); + } }