diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 56e1da09..cb3ec31d 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -502,6 +502,18 @@ final class Expectation return $this; } + /** + * Asserts that the value contains only digits. + * + * @return self + */ + public function toBeDigits(string $message = ''): self + { + Assert::assertTrue(ctype_digit((string) $this->value), $message); + + return $this; + } + /** * Asserts that the value is of type object. * diff --git a/tests/Features/Expect/toBeDigits.php b/tests/Features/Expect/toBeDigits.php new file mode 100644 index 00000000..7b8d6d98 --- /dev/null +++ b/tests/Features/Expect/toBeDigits.php @@ -0,0 +1,20 @@ +toBeDigits(); + expect('123.14')->not->toBeDigits(); +}); + +test('failures', function () { + expect('123.14')->toBeDigits(); +})->throws(ExpectationFailedException::class); + +test('failures with custom message', function () { + expect('123.14')->toBeDigits('oh no!'); +})->throws(ExpectationFailedException::class, 'oh no!'); + +test('not failures', function () { + expect('445')->not->toBeDigits(); +})->throws(ExpectationFailedException::class);