Skip to content

Commit

Permalink
feat(ThrowInvalidArgumentException.php): add ifNegativeWithValue()
Browse files Browse the repository at this point in the history
Throws an exception if the numeric value is negative, including the value in the error message
  • Loading branch information
SandroMiguel committed Mar 28, 2024
1 parent d26d202 commit 825bcbd
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 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},"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}}
{"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}}
34 changes: 30 additions & 4 deletions src/ThrowInvalidArgumentException.php
Expand Up @@ -23,13 +23,13 @@ class ThrowInvalidArgumentException extends \InvalidArgumentException
use BaseExceptionTrait;

/**
* Throws an exception if the numeric value is less than 0.
* Throws an exception if the numeric value is negative.
*
* @param float|int $value The numeric value to check.
* @param string|null $message The error message to use if the value is less
* than 0. If not provided, a default message will be used.
* @param string|null $message The error message to use if the value is
* negative. If not provided, a default message will be used.
*
* @throws ThrowInvalidArgumentException If the value is less than 0.
* @throws ThrowInvalidArgumentException If the value is negative.
*/
public static function ifNegative(
float|int $value,
Expand All @@ -41,4 +41,30 @@ public static function ifNegative(
throw new self($message);
}
}

/**
* Throws an exception if the numeric value is negative, including the
* value in the error message.
*
* @param float|int $value The numeric value to check.
* @param string|null $message The error message to use if the value is
* negative. If not provided, a default message will be used.
*
* @throws ThrowInvalidArgumentException If the value is negative.
*/
public static function ifNegativeWithValue(
float|int $value,
?string $message = null,
): void {
if ($message !== null && !\str_contains($message, '%d')) {
$message = \sprintf('%s. Provided %d', $message, $value);
}

$message = \sprintf(
$message ?? 'The value %d must be greater than or equal to 0.',
$value
);

self::ifNegative($value, $message);
}
}
68 changes: 68 additions & 0 deletions src/tests/ThrowInvalidArgumentExceptionTest.php
Expand Up @@ -75,4 +75,72 @@ public function testIfPositiveValueDoesNotThrowException(): void

$this->assertTrue(true);
}

/**
* Test if an exception is thrown when the value is negative, without a
* custom message.
*/
public function testIfNegativeWithValueWithoutCustomMessage(): void
{
$value = -5;

$this->expectException(\PhpThrow\ThrowInvalidArgumentException::class);
$this->expectExceptionMessage(
\sprintf('The value %d must be greater than or equal to 0.', $value)
);

\PhpThrow\ThrowInvalidArgumentException::ifNegativeWithValue($value);
}

/**
* Test if an exception is thrown when the value is negative, with a
* custom message without '%d' placeholder.
*/
public function testIfNegativeWithValueWithCustomMsgNoPlaceholder(): void
{
$value = -5;
$message = 'Custom error message';

$this->expectException(\PhpThrow\ThrowInvalidArgumentException::class);
$this->expectExceptionMessage(
\sprintf('%s. Provided %d', $message, $value)
);

\PhpThrow\ThrowInvalidArgumentException::ifNegativeWithValue(
$value,
$message
);
}

/**
* Test if an exception is thrown when the value is negative, with a
* custom message containing '%d' placeholder.
*/
public function testIfNegativeWithValueWithCustomMsgWithPlaceholder(): void
{
$value = -5;
$message = 'The value %d is negative.';

$this->expectException(\PhpThrow\ThrowInvalidArgumentException::class);
$this->expectExceptionMessage(
\sprintf($message, $value)
);

\PhpThrow\ThrowInvalidArgumentException::ifNegativeWithValue(
$value,
$message
);
}

/**
* Test if no exception is thrown when the value is positive.
*/
public function testIfNegativeWithValueNotThrowException(): void
{
$value = 5;

\PhpThrow\ThrowInvalidArgumentException::ifNegativeWithValue($value);

$this->expectNotToPerformAssertions();
}
}

0 comments on commit 825bcbd

Please sign in to comment.