introducing toBeDigits

This commit is contained in:
AJ Meireles
2023-08-14 17:10:58 -03:00
parent 03648f580c
commit 398e3ff3b5
2 changed files with 32 additions and 0 deletions

View File

@ -502,6 +502,18 @@ final class Expectation
return $this; return $this;
} }
/**
* Asserts that the value contains only digits.
*
* @return self<TValue>
*/
public function toBeDigits(string $message = ''): self
{
Assert::assertTrue(ctype_digit((string) $this->value), $message);
return $this;
}
/** /**
* Asserts that the value is of type object. * Asserts that the value is of type object.
* *

View File

@ -0,0 +1,20 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect('123')->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);