Skip to content

Commit

Permalink
Merge branch 'update' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisullyott committed Oct 17, 2021
2 parents 7fd91eb + 334bc47 commit 967ea33
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 54 deletions.
2 changes: 2 additions & 0 deletions src/FileSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
128 changes: 74 additions & 54 deletions tests/FileSizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,49 @@
class FileSizeTest extends TestCase
{
/**
* @test numeric string input.
* @test
*/
public function bytes()
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
*/
public function bytes_are_returned_as_an_integer()
{
$size = new FileSize('128974848');

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

/**
* @test "partial bytes" are rounded up.
* @test
*/
public function bytesRounding()
public function partial_bytes_are_rounded_up()
{
$size = new FileSize('99.7 bytes');

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

/**
* @test #add.
* @test
*/
public function add()
public function sizes_can_be_added()
{
$size = new FileSize('123 megabytes');
$size->add('150 KiB');
Expand All @@ -41,64 +61,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);
Expand All @@ -107,39 +127,39 @@ public function divideBy()
}

/**
* @test upward unit conversion.
* @test
*/
public function convertUp()
public function sizes_can_be_converted_up()
{
$size = new FileSize('123456789 TB');

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

/**
* @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);
Expand All @@ -148,52 +168,52 @@ 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');

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

/**
* @test the rounding in auto-formatting (should not leave trailing zeros).
* @test
*/
public function autoRounding()
public function custom_decimal_mark_is_supported()
{
$size = new FileSize('158.1983 mb');
$size = new FileSize(10921134, 10, ',');

$this->assertSame($size->asAuto(), '158.2 MB');
$this->assertSame($size->asAuto(), '10,92 MB');
}

/**
* @test a decimal base conversion.
* @test
*/
public function decimalBase()
public function custom_decimal_and_thousands_marks_are_supported()
{
$size = new FileSize(10921134, 10);
$size = new FileSize('1.234.522.678,12 KB', 2, ',');

$this->assertSame($size->asAuto(), '10.92 MB');
$this->assertSame($size->asAuto(), '1,15 TB');
}

/**
* @test a custom decimal separator.
* @test
*/
public function decimalMark()
public function smallest_integer_is_supported()
{
$size = new FileSize(10921134, 10, ',');
$size = new FileSize(PHP_INT_MIN);

$this->assertSame($size->asAuto(), '10,92 MB');
$this->assertIsNumeric($size->as('YB'));
}

/**
* @test custom decimal separators and thousands marks.
* @test
*/
public function decimalAndThousandsMarks()
public function largest_integer_is_supported()
{
$size = new FileSize('1.234.522.678,12 KB', 2, ',');
$size = new FileSize(PHP_INT_MAX);

$this->assertSame($size->asAuto(), '1,15 TB');
$this->assertIsNumeric($size->as('YB'));
}
}

0 comments on commit 967ea33

Please sign in to comment.