mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 16:52:16 +02:00
Apply contribution guidelines to "Slug" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
540c0a4a17
commit
d63983e2d2
|
@ -2,7 +2,7 @@
|
|||
|
||||
- `Slug()`
|
||||
|
||||
Validates slug-like strings:
|
||||
Validates whether the input is a valid slug.
|
||||
|
||||
```php
|
||||
v::slug()->validate('my-wordpress-title'); // true
|
||||
|
|
|
@ -15,9 +15,10 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
/**
|
||||
* @author Carlos André Ferrari <caferrari@gmail.com>
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class SlugException extends ValidationException
|
||||
final class SlugException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
|
|
@ -13,19 +13,26 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use function is_string;
|
||||
use function mb_strstr;
|
||||
use function preg_match;
|
||||
|
||||
/**
|
||||
* Validates whether the input is a valid slug.
|
||||
*
|
||||
* @author Carlos André Ferrari <caferrari@gmail.com>
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Nick Lombard <github@jigsoft.co.za>
|
||||
*/
|
||||
class Slug extends AbstractRule
|
||||
final class Slug extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (mb_strstr($input, '--')) {
|
||||
if (!is_string($input) || mb_strstr($input, '--')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
43
tests/integration/rules/slug.phpt
Normal file
43
tests/integration/rules/slug.phpt
Normal file
|
@ -0,0 +1,43 @@
|
|||
--CREDITS--
|
||||
Danilo Correa <danilosilva87@gmail.com>
|
||||
Marcel dos Santos <marcelgsantos@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\SlugException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::slug()->check('my-Slug');
|
||||
} catch (SlugException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::slug())->check('my-slug');
|
||||
} catch (SlugException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::slug()->assert('my-Slug');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::slug())->assert('my-slug');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
"my-Slug" must be a valid slug
|
||||
"my-slug" must not be a valid slug
|
||||
- "my-Slug" must be a valid slug
|
||||
- "my-slug" must not be a valid slug
|
|
@ -1,15 +0,0 @@
|
|||
--CREDITS--
|
||||
Marcel dos Santos <marcelgsantos@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::slug()->check('getting-started-to-respect-validation');
|
||||
v::slug()->assert('welcome-to-php-7');
|
||||
?>
|
||||
--EXPECT--
|
|
@ -1,22 +0,0 @@
|
|||
--CREDITS--
|
||||
Henrique Moody <henriquemoody@gmail.com>
|
||||
Marcel dos Santos <marcelgsantos@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\SlugException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::slug()->check('wrong slug');
|
||||
} catch (SlugException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
"wrong slug" must be a valid slug
|
|
@ -1,21 +0,0 @@
|
|||
--CREDITS--
|
||||
Henrique Moody <henriquemoody@gmail.com>
|
||||
Marcel dos Santos <marcelgsantos@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::slug()->assert('wrong slug');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
- "wrong slug" must be a valid slug
|
|
@ -1,21 +0,0 @@
|
|||
--CREDITS--
|
||||
Henrique Moody <henriquemoody@gmail.com>
|
||||
Marcel dos Santos <marcelgsantos@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\SlugException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::slug())->check('good-and-valid-slug');
|
||||
} catch (SlugException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
"good-and-valid-slug" must not be a valid slug
|
|
@ -1,21 +0,0 @@
|
|||
--CREDITS--
|
||||
Henrique Moody <henriquemoody@gmail.com>
|
||||
Marcel dos Santos <marcelgsantos@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::slug())->assert('good-and-valid-slug');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
- "good-and-valid-slug" must not be a valid slug
|
|
@ -13,72 +13,62 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Test\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\Slug
|
||||
*
|
||||
* @author Carlos André Ferrari <caferrari@gmail.com>
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Marcel dos Santos <marcelgsantos@gmail.com>
|
||||
*/
|
||||
final class SlugTest extends TestCase
|
||||
final class SlugTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerValidSlug
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validSlug(string $input): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$rule = new Slug();
|
||||
$sut = new Slug();
|
||||
|
||||
self::assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerInvalidSlug
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function invalidSlug(string $input): void
|
||||
{
|
||||
$rule = new Slug();
|
||||
|
||||
self::assertFalse($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function providerValidSlug(): array
|
||||
{
|
||||
return [
|
||||
['o-rato-roeu-o-rei-de-roma'],
|
||||
['o-alganet-e-um-feio'],
|
||||
['a-e-i-o-u'],
|
||||
['anticonstitucionalissimamente'],
|
||||
[$sut, 'o-rato-roeu-o-rei-de-roma'],
|
||||
[$sut, 'o-alganet-e-um-feio'],
|
||||
[$sut, 'a-e-i-o-u'],
|
||||
[$sut, 'anticonstitucionalissimamente'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerInvalidSlug(): array
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$sut = new Slug();
|
||||
|
||||
return [
|
||||
[''],
|
||||
['o-alganet-é-um-feio'],
|
||||
['á-é-í-ó-ú'],
|
||||
['-assim-nao-pode'],
|
||||
['assim-tambem-nao-'],
|
||||
['nem--assim'],
|
||||
['--nem-assim'],
|
||||
['Nem mesmo Assim'],
|
||||
['Ou-ate-assim'],
|
||||
['-Se juntar-tudo-Então-'],
|
||||
[$sut, ''],
|
||||
[$sut, 'o-alganet-é-um-feio'],
|
||||
[$sut, 'á-é-í-ó-ú'],
|
||||
[$sut, '-assim-nao-pode'],
|
||||
[$sut, 'assim-tambem-nao-'],
|
||||
[$sut, 'nem--assim'],
|
||||
[$sut, '--nem-assim'],
|
||||
[$sut, 'Nem mesmo Assim'],
|
||||
[$sut, 'Ou-ate-assim'],
|
||||
[$sut, '-Se juntar-tudo-Então-'],
|
||||
[$sut, 'eAssim-vai'],
|
||||
[$sut, '@-!teste-teste'],
|
||||
[$sut, '*teste-teste'],
|
||||
[$sut, 123],
|
||||
[$sut, []],
|
||||
[$sut, 123.321],
|
||||
[$sut, new stdClass()],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue