Skip to content

Commit

Permalink
fix(ThrowInvalidArgumentException.php): fix ifNegative method to hand…
Browse files Browse the repository at this point in the history
…le the case when value is 0
  • Loading branch information
SandroMiguel committed Mar 28, 2024
1 parent 7420086 commit ed58ab5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
@@ -1 +1 @@
{"version":1,"defects":{"PhpThrow\\Tests\\BaseExceptionTraitTest::testCreateMethod":4,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeThrowsException":3,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeDoesNotThrowForPositiveValue":4,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testCreateMethod":4,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMessageWithPlaceholder":3},"times":{"PhpThrow\\Tests\\BaseExceptionTraitTest::testCreateMethod":0.001,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeThrowsException":0.004,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithCustomMessage":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeDoesNotThrowForPositiveValue":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testCreateMethod":0.001,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithDefaultMessage":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfPositiveValueDoesNotThrowException":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithoutCustomMessage":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMessageWithoutPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMsgNoPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMessageWithPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMsgWithPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueNotThrowException":0}}
{"version":1,"defects":{"PhpThrow\\Tests\\BaseExceptionTraitTest::testCreateMethod":4,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeThrowsException":3,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeDoesNotThrowForPositiveValue":4,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testCreateMethod":4,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMessageWithPlaceholder":3,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithZeroValue":3},"times":{"PhpThrow\\Tests\\BaseExceptionTraitTest::testCreateMethod":0.001,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeThrowsException":0.004,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithCustomMessage":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeDoesNotThrowForPositiveValue":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testCreateMethod":0.001,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithDefaultMessage":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfPositiveValueDoesNotThrowException":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithoutCustomMessage":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMessageWithoutPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMsgNoPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMessageWithPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMsgWithPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueNotThrowException":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithZeroValue":0}}
2 changes: 1 addition & 1 deletion src/ThrowInvalidArgumentException.php
Expand Up @@ -35,7 +35,7 @@ public static function ifNegative(
float|int $value,
?string $message = null,
): void {
if ($value < 0) {
if ($value <= 0) {
$message ??= 'The value must be greater than or equal to 0.';

throw new self($message);
Expand Down
21 changes: 17 additions & 4 deletions src/tests/ThrowInvalidArgumentExceptionTest.php
Expand Up @@ -38,8 +38,8 @@ public function testCreateMethod(): void
}

/**
* Test if the exception is thrown when the value is negative with default
* message.
* Test if the exception is thrown when the value is greater than or equal
* to 0 with default message.
*/
public function testIfNegativeWithDefaultMessage(): void
{
Expand All @@ -52,8 +52,21 @@ public function testIfNegativeWithDefaultMessage(): void
}

/**
* Test if the exception is thrown when the value is negative with custom
* message.
* Test if the exception is thrown when the value is 0 with default message.
*/
public function testIfNegativeWithZeroValue(): void
{
$this->expectException(\PhpThrow\ThrowInvalidArgumentException::class);
$this->expectExceptionMessage(
'The value must be greater than or equal to 0.'
);

\PhpThrow\ThrowInvalidArgumentException::ifNegative(0);
}

/**
* Test if the exception is thrown when the value is greater than or equal
* to 0 with custom message.
*/
public function testIfNegativeWithCustomMessage(): void
{
Expand Down

0 comments on commit ed58ab5

Please sign in to comment.