Apply contribution guidelines to "StartsWith" rule

After writing the integration tests of the "StartsWith" rule we noticed
that it is generating extra parentheses for the "{{startValue}}"
template placeholder.

This commit will also remove those extra parentheses.

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Danilo Correa 2019-03-03 18:27:45 -03:00 committed by Henrique Moody
parent d2c4912582
commit 18bdd399d7
No known key found for this signature in database
GPG key ID: 221E9281655813A6
5 changed files with 73 additions and 56 deletions

View file

@ -3,8 +3,10 @@
- `StartsWith(mixed $value)`
- `StartsWith(mixed $value, bool $identical)`
This validator is similar to `Contains()`, but validates
only if the value is at the beginning of the input.
Validates whether the input starts with a given value.
This validator is similar to [Contains](Contains.md), but validates only
if the value is at the beginning of the input.
For strings:

View file

@ -24,10 +24,10 @@ final class StartsWithException extends ValidationException
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must start with ({{startValue}})',
self::STANDARD => '{{name}} must start with {{startValue}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not start with ({{startValue}})',
self::STANDARD => '{{name}} must not start with {{startValue}}',
],
];
}

View file

@ -20,6 +20,8 @@ use function mb_strpos;
use function reset;
/**
* Validates whether the input starts with a given value.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Marcelo Araujo <msaraujo@php.net>

View file

@ -0,0 +1,42 @@
--CREDITS--
Danilo Correa <danilosilva87@gmail.com>
--FILE--
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Exceptions\StartsWithException;
use Respect\Validation\Validator as v;
try {
v::startsWith('b')->check(['a', 'b']);
} catch (StartsWithException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::startsWith(1.1))->check([1.1, 2.2]);
} catch (StartsWithException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::startsWith('3.3', true)->assert([3.3, 4.4]);
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::startsWith('c'))->assert(['c', 'd']);
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECT--
`{ "a", "b" }` must start with "b"
`{ 1.1, 2.2 }` must not start with 1.1
- `{ 3.3, 4.4 }` must start with "3.3"
- `{ "c", "d" }` must not start with "c"

View file

@ -13,78 +13,49 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\TestCase;
use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\StartsWithException
* @group rule
*
* @covers \Respect\Validation\Rules\StartsWith
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class StartsWithTest extends TestCase
final class StartsWithTest extends RuleTestCase
{
/**
* @dataProvider providerForStartsWith
*
* @test
*
* @param mixed $input
* {@inheritdoc}
*/
public function startsWith(string $start, $input): void
{
$v = new StartsWith($start);
self::assertTrue($v->__invoke($input));
$v->check($input);
$v->assert($input);
}
/**
* @dataProvider providerForNotStartsWith
* @expectedException \Respect\Validation\Exceptions\StartsWithException
*
* @test
*
* @param mixed $input
*/
public function notStartsWith(string $start, $input, bool $caseSensitive = false): void
{
$v = new StartsWith($start, $caseSensitive);
self::assertFalse($v->__invoke($input));
$v->assert($input);
}
/**
* @return mixed[][]
*/
public function providerForStartsWith(): array
public function providerForValidInput(): array
{
return [
['foo', ['foo', 'bar']],
['foo', 'FOObarbaz'],
['foo', 'foobarbaz'],
['foo', 'foobazfoo'],
['1', [1, 2, 3]],
['1', ['1', 2, 3], true],
[new StartsWith('foo'), ['foo', 'bar']],
[new StartsWith('foo') ,'FOObarbaz'],
[new StartsWith('foo') , 'foobarbaz'],
[new StartsWith('foo') ,'foobazfoo'],
[new StartsWith('1'), [1, 2, 3]],
[new StartsWith('1', true), ['1', 2, 3]],
];
}
/**
* @return mixed[][]
* {@inheritdoc}
*/
public function providerForNotStartsWith(): array
public function providerForInvalidInput(): array
{
return [
['foo', ''],
['bat', ['foo', 'bar']],
['foo', 'barfaabaz'],
['foo', 'FOObarbaz', true],
['foo', 'faabarbaz'],
['foo', 'baabazfaa'],
['foo', 'baafoofaa'],
['1', [1, '1', 3], true],
[new StartsWith('foo'), ''],
[new StartsWith('bat'), ['foo', 'bar']],
[new StartsWith('foo'), 'barfaabaz'],
[new StartsWith('foo', true), 'FOObarbaz'],
[new StartsWith('foo'), 'faabarbaz'],
[new StartsWith('foo'), 'baabazfaa'],
[new StartsWith('foo'), 'baafoofaa'],
[new StartsWith('1', true), [1, '1', 3]],
];
}
}