Apply contribution guidelines to "Lowercase" rule

Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Danilo Benevides 2018-06-03 15:07:02 -03:00 committed by Henrique Moody
parent 36be04c520
commit b29bdbd111
No known key found for this signature in database
GPG key ID: 221E9281655813A6
5 changed files with 106 additions and 42 deletions

View file

@ -2,7 +2,7 @@
- `Lowercase()`
Validates if string characters are lowercase in the input:
Validates whether the characters in the input are lowercase.
```php
v::stringType()->lowercase()->validate('xkcd'); // true

View file

@ -13,8 +13,16 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class LowercaseException extends ValidationException
/**
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jean Pimentel <jeanfap@gmail.com>
*/
final class LowercaseException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be lowercase',

View file

@ -13,10 +13,29 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
class Lowercase extends AbstractRule
use function is_string;
use function mb_detect_encoding;
use function mb_strtolower;
/**
* Validates whether the characters in the input are lowercase.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jean Pimentel <jeanfap@gmail.com>
*/
final class Lowercase extends AbstractRule
{
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
if (!is_string($input)) {
return false;
}
return $input === mb_strtolower($input, mb_detect_encoding($input));
}
}

View file

@ -0,0 +1,38 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\LowercaseException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::lowercase()->check('UPPERCASE');
} catch (LowercaseException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::lowercase())->check('lowercase');
} catch (LowercaseException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::lowercase()->assert('UPPERCASE');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::lowercase())->assert('lowercase');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"UPPERCASE" must be lowercase
"lowercase" must not be lowercase
- "UPPERCASE" must be lowercase
- "lowercase" must not be lowercase

View file

@ -13,58 +13,57 @@ 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\Lowercase
* @covers \Respect\Validation\Exceptions\LowercaseException
*
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jean Pimentel <jeanfap@gmail.com>
*/
class LowercaseTest extends TestCase
final class LowercaseTest extends RuleTestCase
{
/**
* @dataProvider providerForValidLowercase
*/
public function testValidLowercaseShouldReturnTrue($input): void
/*
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
$lowercase = new Lowercase();
self::assertTrue($lowercase->__invoke($input));
$lowercase->assert($input);
$lowercase->check($input);
}
$rule = new Lowercase();
/**
* @dataProvider providerForInvalidLowercase
* @expectedException \Respect\Validation\Exceptions\LowercaseException
*/
public function testInvalidLowercaseShouldThrowException($input): void
{
$lowercase = new Lowercase();
self::assertFalse($lowercase->__invoke($input));
$lowercase->assert($input);
}
public function providerForValidLowercase()
{
return [
[''],
['lowercase'],
['lowercase-with-dashes'],
['lowercase with spaces'],
['lowercase with numbers 123'],
['lowercase with specials characters like ã ç ê'],
['with specials characters like # $ % & * +'],
['τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός'],
[$rule, ''],
[$rule, 'lowercase'],
[$rule, 'lowercase-with-dashes'],
[$rule, 'lowercase with spaces'],
[$rule, 'lowercase with numbers 123'],
[$rule, 'lowercase with specials characters like ã ç ê'],
[$rule, 'with specials characters like # $ % & * +'],
[$rule, 'τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός'],
[$rule, '42'],
[$rule, '!@#$%^'],
];
}
public function providerForInvalidLowercase()
/*
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
$rule = new Lowercase();
return [
['UPPERCASE'],
['CamelCase'],
['First Character Uppercase'],
['With Numbers 1 2 3'],
[$rule, 42],
[$rule, []],
[$rule, new stdClass()],
[$rule, 'UPPERCASE'],
[$rule, 'CamelCase'],
[$rule, 'First Character Uppercase'],
[$rule, 'With Numbers 1 2 3'],
];
}
}