diff --git a/docs/Uppercase.md b/docs/Uppercase.md index 0c7e80de..c1039d59 100644 --- a/docs/Uppercase.md +++ b/docs/Uppercase.md @@ -2,10 +2,20 @@ - `Uppercase()` -Validates if string characters are uppercase in the input: +Validates whether the characters in the input are uppercase. ```php -v::stringType()->uppercase()->validate('W3C'); // true +v::uppercase()->validate('W3C'); // true +``` + +This rule does not validate if the input a numeric value, so `123` and `%` will +be valid. Please add more validations to the chain if you want to refine your +validation. + +```php +v::not(v::numericVal())->uppercase()->validate('42'); // false +v::alnum()->uppercase()->validate('#$%!'); // false +v::not(v::numericVal())->alnum()->uppercase()->validate('W3C'); // true ``` ## Changelog @@ -17,4 +27,6 @@ Version | Description *** See also: +- [Alnum](Alnum.md) - [Lowercase](Lowercase.md) +- [NumericVal](NumericVal.md) diff --git a/library/Exceptions/UppercaseException.php b/library/Exceptions/UppercaseException.php index b1bcddda..3fe1b9a3 100644 --- a/library/Exceptions/UppercaseException.php +++ b/library/Exceptions/UppercaseException.php @@ -13,8 +13,16 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; -class UppercaseException extends ValidationException +/** + * @author Danilo Benevides + * @author Henrique Moody + * @author Jean Pimentel + */ +final class UppercaseException extends ValidationException { + /** + * {@inheritdoc} + */ public static $defaultTemplates = [ self::MODE_DEFAULT => [ self::STANDARD => '{{name}} must be uppercase', diff --git a/library/Rules/Uppercase.php b/library/Rules/Uppercase.php index 6372f6ba..3716292e 100644 --- a/library/Rules/Uppercase.php +++ b/library/Rules/Uppercase.php @@ -13,10 +13,29 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -class Uppercase extends AbstractRule +use function is_string; +use function mb_detect_encoding; +use function mb_strtoupper; + +/** + * Validates whether the characters in the input are uppercase. + * + * @author Alexandre Gomes Gaigalas + * @author Danilo Benevides + * @author Henrique Moody + * @author Jean Pimentel + */ +final class Uppercase extends AbstractRule { + /** + * {@inheritdoc} + */ public function validate($input): bool { + if (!is_string($input)) { + return false; + } + return $input === mb_strtoupper($input, mb_detect_encoding($input)); } } diff --git a/tests/integration/rules/uppercase.phpt b/tests/integration/rules/uppercase.phpt new file mode 100644 index 00000000..cdd17521 --- /dev/null +++ b/tests/integration/rules/uppercase.phpt @@ -0,0 +1,38 @@ +--FILE-- +check('lowercase'); +} catch (UppercaseException $exception) { + echo $exception->getMessage().PHP_EOL; +} + +try { + v::uppercase()->assert('lowercase'); +} catch (NestedValidationException $exception) { + echo $exception->getFullMessage().PHP_EOL; +} + +try { + v::not(v::uppercase())->check('UPPERCASE'); +} catch (UppercaseException $exception) { + echo $exception->getMessage().PHP_EOL; +} + +try { + v::not(v::uppercase())->assert('UPPERCASE'); +} catch (NestedValidationException $exception) { + echo $exception->getFullMessage().PHP_EOL; +} + +?> +--EXPECTF-- +"lowercase" must be uppercase +- "lowercase" must be uppercase +"UPPERCASE" must not be uppercase +- "UPPERCASE" must not be uppercase diff --git a/tests/unit/Rules/UppercaseTest.php b/tests/unit/Rules/UppercaseTest.php index d28ef306..e6b337e4 100644 --- a/tests/unit/Rules/UppercaseTest.php +++ b/tests/unit/Rules/UppercaseTest.php @@ -13,58 +13,58 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use PHPUnit\Framework\TestCase; +use Respect\Validation\Test\RuleTestCase; +use stdClass; /** - * @group rule + * @group rule + * * @covers \Respect\Validation\Rules\Uppercase - * @covers \Respect\Validation\Exceptions\UppercaseException + * + * @author Danilo Benevides + * @author Gabriel Caruso + * @author Henrique Moody + * @author Jean Pimentel */ -class UppercaseTest extends TestCase +final class UppercaseTest extends RuleTestCase { - /** - * @dataProvider providerForValidUppercase - */ - public function testValidUppercaseShouldReturnTrue($input): void + /* + * {@inheritdoc} + */ + public function providerForValidInput(): array { - $uppercase = new Uppercase(); - self::assertTrue($uppercase->validate($input)); - $uppercase->assert($input); - $uppercase->check($input); - } + $rule = new Uppercase(); - /** - * @dataProvider providerForInvalidUppercase - * @expectedException \Respect\Validation\Exceptions\UppercaseException - */ - public function testInvalidUppercaseShouldThrowException($input): void - { - $lowercase = new Uppercase(); - self::assertFalse($lowercase->validate($input)); - $lowercase->assert($input); - } - - public function providerForValidUppercase() - { return [ - [''], - ['UPPERCASE'], - ['UPPERCASE-WITH-DASHES'], - ['UPPERCASE WITH SPACES'], - ['UPPERCASE WITH NUMBERS 123'], - ['UPPERCASE WITH SPECIALS CHARACTERS LIKE Ã Ç Ê'], - ['WITH SPECIALS CHARACTERS LIKE # $ % & * +'], - ['ΤΆΧΙΣΤΗ ΑΛΏΠΗΞ ΒΑΦΉΣ ΨΗΜΈΝΗ ΓΗ, ΔΡΑΣΚΕΛΊΖΕΙ ΥΠΈΡ ΝΩΘΡΟΎ ΚΥΝΌΣ'], + [$rule, ''], + [$rule, 'UPPERCASE'], + [$rule, 'UPPERCASE-WITH-DASHES'], + [$rule, 'UPPERCASE WITH SPACES'], + [$rule, 'UPPERCASE WITH NUMBERS 123'], + [$rule, 'UPPERCASE WITH SPECIALS CHARACTERS LIKE Ã Ç Ê'], + [$rule, 'WITH SPECIALS CHARACTERS LIKE # $ % & * +'], + [$rule, 'ΤΆΧΙΣΤΗ ΑΛΏΠΗΞ ΒΑΦΉΣ ΨΗΜΈΝΗ ΓΗ, ΔΡΑΣΚΕΛΊΖΕΙ ΥΠΈΡ ΝΩΘΡΟΎ ΚΥΝΌΣ'], + // Uppercase should not restrict these + [$rule, '42'], + [$rule, '!@#$%^'], ]; } - public function providerForInvalidUppercase() + /* + * {@inheritdoc} + */ + public function providerForInvalidInput(): array { + $rule = new Uppercase(); + return [ - ['lowercase'], - ['CamelCase'], - ['First Character Uppercase'], - ['With Numbers 1 2 3'], + [$rule, 42], + [$rule, []], + [$rule, new stdClass()], + [$rule, 'lowercase'], + [$rule, 'CamelCase'], + [$rule, 'First Character Uppercase'], + [$rule, 'With Numbers 1 2 3'], ]; } }