Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add expectations for uppercase, lowercase, alpha and alphanumeric #906

Merged
merged 2 commits into from Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Mixins/Expectation.php
Expand Up @@ -961,4 +961,52 @@ private function export(mixed $value): string

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 changes: 20 additions & 0 deletions tests/Features/Expect/toBeAlpha.php
@@ -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 changes: 20 additions & 0 deletions tests/Features/Expect/toBeAlphaNumeric.php
@@ -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 changes: 20 additions & 0 deletions tests/Features/Expect/toBeLowercase.php
@@ -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 changes: 20 additions & 0 deletions tests/Features/Expect/toBeUppercase.php
@@ -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);