mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
Merge pull request #906 from JonPurvis/extra-expectations
add expectations for uppercase, lowercase, alpha and alphanumeric
This commit is contained in:
@ -961,4 +961,52 @@ final class Expectation
|
|||||||
|
|
||||||
return $this->exporter->shortenedExport($value);
|
return $this->exporter->shortenedExport($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the value is uppercase.
|
||||||
|
*
|
||||||
|
* @return self<TValue>
|
||||||
|
*/
|
||||||
|
public function toBeUppercase(string $message = ''): self
|
||||||
|
{
|
||||||
|
Assert::assertTrue(ctype_upper((string) $this->value), $message);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the value is lowercase.
|
||||||
|
*
|
||||||
|
* @return self<TValue>
|
||||||
|
*/
|
||||||
|
public function toBeLowercase(string $message = ''): self
|
||||||
|
{
|
||||||
|
Assert::assertTrue(ctype_lower((string) $this->value), $message);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the value is alphanumeric.
|
||||||
|
*
|
||||||
|
* @return self<TValue>
|
||||||
|
*/
|
||||||
|
public function toBeAlphaNumeric(string $message = ''): self
|
||||||
|
{
|
||||||
|
Assert::assertTrue(ctype_alnum((string) $this->value), $message);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the value is alpha.
|
||||||
|
*
|
||||||
|
* @return self<TValue>
|
||||||
|
*/
|
||||||
|
public function toBeAlpha(string $message = ''): self
|
||||||
|
{
|
||||||
|
Assert::assertTrue(ctype_alpha((string) $this->value), $message);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
20
tests/Features/Expect/toBeAlpha.php
Normal file
20
tests/Features/Expect/toBeAlpha.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect('abc')->toBeAlpha();
|
||||||
|
expect('123')->not->toBeAlpha();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect('123')->toBeAlpha();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('failures with custom message', function () {
|
||||||
|
expect('123')->toBeAlpha('oh no!');
|
||||||
|
})->throws(ExpectationFailedException::class, 'oh no!');
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect('abc')->not->toBeAlpha();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
20
tests/Features/Expect/toBeAlphaNumeric.php
Normal file
20
tests/Features/Expect/toBeAlphaNumeric.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect('abc123')->toBeAlphaNumeric();
|
||||||
|
expect('-')->not->toBeAlphaNumeric();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect('-')->toBeAlphaNumeric();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('failures with custom message', function () {
|
||||||
|
expect('-')->toBeAlphaNumeric('oh no!');
|
||||||
|
})->throws(ExpectationFailedException::class, 'oh no!');
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect('abc123')->not->toBeAlphaNumeric();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
20
tests/Features/Expect/toBeLowercase.php
Normal file
20
tests/Features/Expect/toBeLowercase.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect('lowercase')->toBeLowercase();
|
||||||
|
expect('UPPERCASE')->not->toBeLowercase();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect('UPPERCASE')->toBeLowercase();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('failures with custom message', function () {
|
||||||
|
expect('UPPERCASE')->toBeLowercase('oh no!');
|
||||||
|
})->throws(ExpectationFailedException::class, 'oh no!');
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect('lowercase')->not->toBeLowercase();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
20
tests/Features/Expect/toBeUppercase.php
Normal file
20
tests/Features/Expect/toBeUppercase.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect('UPPERCASE')->toBeUppercase();
|
||||||
|
expect('lowercase')->not->toBeUppercase();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect('lowercase')->toBeUppercase();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('failures with custom message', function () {
|
||||||
|
expect('lowercase')->toBeUppercase('oh no!');
|
||||||
|
})->throws(ExpectationFailedException::class, 'oh no!');
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect('UPPERCASE')->not->toBeUppercase();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
Reference in New Issue
Block a user