Skip to content

Commit

Permalink
Merge pull request #906 from JonPurvis/extra-expectations
Browse files Browse the repository at this point in the history
add expectations for uppercase, lowercase, alpha and alphanumeric
  • Loading branch information
nunomaduro authored Aug 13, 2023
2 parents f1414a0 + f996a48 commit 62267df
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/Mixins/Expectation.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 62267df

Please sign in to comment.