Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usage of negative numbers in Number value object #53

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Concerns/SanitizesNumbers.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function sanitize(int|string|float|null $number): string
}

return $number
->replaceMatches('/[^0-9.]/', '')
->replaceMatches('/(?!^-)[^0-9.]/', '')
->toString();
}

Expand Down
80 changes: 80 additions & 0 deletions tests/Unit/Primitive/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,86 @@ class_uses_recursive(Number::class)
$this->assertSame('1230.12', $number->value());
});

test('number can accept negative integer', function () {
$valueObject = new Number(-1);
$this->assertSame('-1.00', $valueObject->value());
$valueObject = new Number(-2);
$this->assertSame('-2.00', $valueObject->value());
});

test('number can cast negative value to integer', function () {
$valueObject = new Number('-100');
$this->assertSame(-100, $valueObject->asInteger());
});

test('number can cast negative value to float', function () {
$valueObject = new Number('-36000.50');
$this->assertSame(-36000.50, $valueObject->asFloat());
});

test('negative number as a big number', function () {
$number = new Number('-20000.793', 3);
$this->assertEquals(new BigNumber('-20000.793', 3, false), $number->asBigNumber());
});

test('negative number can be divided using magic call', function () {
$number = new Number('-20000.793', 4);
$this->assertSame('-10000.3965', $number->divide(2));
});

test('negative number can be multiplied using magic call', function () {
$number = new Number('-20000.793', 3);
$this->assertSame('-40001.586', $number->multiply(2));
});

test('negative number strips zeros when the value starts from zero', function ($input, $result) {
$valueObject = new Number($input);
$this->assertSame($result, $valueObject->value());
})->with([
['-0000123.987', '-123.98'],
['-0000123', '-123.00'],
]);

test('negative number accepts formatted value', function ($input, $scale, $result) {
$valueObject = new Number($input, $scale);
$this->assertSame($result, $valueObject->value());
})->with([
['-1,230,00', 2, '-1230.00'],
['-123.123.123,556', 3, '-123123123.556'],
['-1 230,00', 2, '-1230.00'],
['-777.7', 3, '-777.700'],
]);

test('negative number fails when invalid text provided', function () {
$this->expectException(\InvalidArgumentException::class);

new Number('-asd');
});

test('negative number fails when empty string passed', function () {
$this->expectException(\InvalidArgumentException::class);

new Number('-');
});

test('negative number can change decimals as a string input', function ($input, $scale, $result) {
$valueObject = new Number($input, $scale);
$this->assertSame($result, $valueObject->value());
})->with([
['-111777999.97', 2, '-111777999.97'],
['-111777999,97', 2, '-111777999.97'],
['-7.99', 3, '-7.990'],
['-71.1', 5, '-71.10000'],
]);

test('negative number can handle huge numbers', function ($input, $scale, $result) {
$valueObject = new Number($input, $scale);
$this->assertSame($result, $valueObject->value());
})->with([
['-9876543210111777999.9087', 2, '-9876543210111777999.90'],
['-98765432101117779990000.9087', 1, '-98765432101117779990000.9'],
]);

class TestNumber extends Number
{
public function __construct(int|string|float $number, protected int $scale = 2)
Expand Down
Loading