Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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/Number/Integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final public function getValue(): int
final public static function fromNumeric($value): self
{
if (is_numeric($value)) {
return new self((int)$value);
return new static((int)$value);
}

throw new InvalidArgumentException('Value is not numeric.');
Expand Down
21 changes: 21 additions & 0 deletions tests/Number/NonNegativeIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,25 @@ public function test_isNotEqualToDifferentType(): void
$this->assertFalse($integer1->equals($integer2));
$this->assertFalse($integer2->equals($integer1));
}

public function test_canNotBeCreatedFromNegativeNumericString(): void
{
$this->expectException(InvalidArgumentException::class);

NonNegativeInteger::fromNumeric('-42');
}

public function test_canNotBeCreatedFromNegativeFloat(): void
{
$this->expectException(InvalidArgumentException::class);

$result = NonNegativeInteger::fromNumeric(-42.3);
}

public function test_canNotBeCreatedFromNegativeNumeric(): void
{
$this->expectException(InvalidArgumentException::class);

NonNegativeInteger::fromNumeric(-42);
}
}
28 changes: 28 additions & 0 deletions tests/Number/PositiveIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,32 @@ public function test_isNotEqualToDifferentType(): void
$this->assertFalse($integer1->equals($integer2));
$this->assertFalse($integer2->equals($integer1));
}

public function test_CannotBeCreatedFromNumericZeroString(): void
{
$this->expectException(InvalidArgumentException::class);

PositiveInteger::fromNumeric('0');
}

public function test_CannotBeCreatedFromNumericZeroFloat(): void
{
$this->expectException(InvalidArgumentException::class);

PositiveInteger::fromNumeric(0.0);
}

public function test_CannotBeCreatedFromNumericNegativeIntegerString(): void
{
$this->expectException(InvalidArgumentException::class);

PositiveInteger::fromNumeric('-1');
}

public function test_CannotBeCreatedFromNumericNegativeInteger(): void
{
$this->expectException(InvalidArgumentException::class);

PositiveInteger::fromNumeric(-1);
}
}
Loading