respect-validation/tests/unit/Validators/StartsWithTest.php
Alexandre Gomes Gaigalas 2600db3c74 Introduce Trimmed validator
This commit introduces the `Trimmed` validator that ensures a string
cannot start or end with a list of specific values.

The default values used are a selected list of Unicode invisible
characters.

To support this change, the StartsWith and EndsWith validators were
modified so they can also support multiple values to check for.

While StartsWith and EndsWith are more generic, and also perform
start-of-array and end-of-array kinds of checks, Trimmed is more
focused on string inputs, which tailors to a more specific use
case.
2026-02-26 10:34:53 +00:00

55 lines
1.9 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-FileContributor: Danilo Correa <danilosilva87@gmail.com>
* SPDX-FileContributor: Gabriel Caruso <carusogabriel34@gmail.com>
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Validation\Validators;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Respect\Validation\Test\RuleTestCase;
#[Group('validator')]
#[CoversClass(StartsWith::class)]
final class StartsWithTest extends RuleTestCase
{
/** @return iterable<array{StartsWith, mixed}> */
public static function providerForValidInput(): iterable
{
return [
[new StartsWith('foo'), ['foo', 'bar']],
[new StartsWith('foo'), 'foobarbaz'],
[new StartsWith('foo'), 'foobazfoo'],
[new StartsWith('foo', 'bar'), 'barbaz'],
[new StartsWith('foo', 'bar'), ['bar', 'baz']],
[new StartsWith('1'), ['1', 2, 3]],
];
}
/** @return iterable<array{StartsWith, mixed}> */
public static function providerForInvalidInput(): iterable
{
return [
[new StartsWith(123), 123],
[new StartsWith('foo'), ''],
[new StartsWith('bat'), ['foo', 'bar']],
[new StartsWith('foo'), 'barfaabaz'],
[new StartsWith('foo'), 'FOObarbaz'],
[new StartsWith('foo'), 'faabarbaz'],
[new StartsWith('foo'), 'baabazfaa'],
[new StartsWith('foo'), 'baafoofaa'],
[new StartsWith('foo', 'bar'), 'bazfoo'],
[new StartsWith('foo', 'bar'), ['baz', 'foo']],
[new StartsWith('1'), [1, '1', 3]],
[new StartsWith('1'), [1, 2, 3]],
];
}
}