2015-01-06 23:14:08 +01:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2015-06-08 17:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2015-01-06 23:14:08 +01:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2019-04-05 20:11:13 +02:00
|
|
|
use Respect\Validation\Test\RuleTestCase;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2019-04-05 20:11:13 +02:00
|
|
|
use function setlocale;
|
2019-03-20 23:44:02 +01:00
|
|
|
use function sprintf;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2019-04-05 20:11:13 +02:00
|
|
|
use const LC_ALL;
|
2017-11-04 11:21:40 +01:00
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Group('rule')]
|
|
|
|
#[CoversClass(No::class)]
|
2019-04-05 20:11:13 +02:00
|
|
|
final class NoTest extends RuleTestCase
|
2015-01-06 23:14:08 +01:00
|
|
|
{
|
2024-01-28 00:29:22 +01:00
|
|
|
private readonly string $locale;
|
2019-04-05 20:11:13 +02:00
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Test]
|
|
|
|
#[DataProvider('providerForValidInputWithLocale')]
|
2023-04-02 16:00:37 +02:00
|
|
|
public function itShouldValidateInputAccordingToTheLocale(string $locale, string $input): void
|
2015-01-06 23:14:08 +01:00
|
|
|
{
|
2023-04-02 16:00:37 +02:00
|
|
|
setlocale(LC_ALL, $locale);
|
2019-04-05 20:11:13 +02:00
|
|
|
|
2023-04-02 16:00:37 +02:00
|
|
|
if ($locale !== setlocale(LC_ALL, '0')) {
|
|
|
|
$this->markTestSkipped(sprintf('Could not set locale information to "%s"', $locale));
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertValidInput(new No(true), $input);
|
2015-01-06 23:14:08 +01:00
|
|
|
}
|
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Test]
|
|
|
|
#[DataProvider('providerForInvalidInputWithLocale')]
|
2023-04-02 16:00:37 +02:00
|
|
|
public function itShouldInvalidateInputAccordingToTheLocale(string $locale, string $input): void
|
2015-01-06 23:14:08 +01:00
|
|
|
{
|
2023-04-02 16:00:37 +02:00
|
|
|
setlocale(LC_ALL, $locale);
|
2015-01-06 23:14:08 +01:00
|
|
|
|
2023-04-02 16:00:37 +02:00
|
|
|
if ($locale !== setlocale(LC_ALL, '0')) {
|
|
|
|
$this->markTestSkipped(sprintf('Could not set locale information to "%s"', $locale));
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertInvalidInput(new No(true), $input);
|
2015-01-06 23:14:08 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @return string[][]
|
|
|
|
*/
|
2023-04-02 16:00:37 +02:00
|
|
|
public static function providerForValidInputWithLocale(): array
|
2015-01-06 23:14:08 +01:00
|
|
|
{
|
2015-10-18 03:44:47 +02:00
|
|
|
return [
|
2019-04-05 20:11:13 +02:00
|
|
|
'nl' => ['nl_NL.UTF-8', 'Nee'],
|
|
|
|
'pt' => ['pt_BR.UTF-8', 'Não'],
|
|
|
|
'ru' => ['ru_RU.UTF-8', 'нет'],
|
2015-10-18 03:44:47 +02:00
|
|
|
];
|
2015-01-06 23:14:08 +01:00
|
|
|
}
|
2019-04-05 20:11:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[][]
|
|
|
|
*/
|
2023-04-02 16:00:37 +02:00
|
|
|
public static function providerForInvalidInputWithLocale(): array
|
2019-04-05 20:11:13 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'nl' => ['nl_NL.UTF-8', 'Ez'],
|
|
|
|
'pt' => ['pt_BR.UTF-8', 'нет'],
|
|
|
|
'ru' => ['pt_BR.UTF-8', 'Οχι'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-02-04 19:23:04 +01:00
|
|
|
/** @return iterable<array{No, mixed}> */
|
|
|
|
public static function providerForValidInput(): iterable
|
2019-04-05 20:11:13 +02:00
|
|
|
{
|
2023-04-02 16:00:37 +02:00
|
|
|
$sut = new No();
|
2019-03-20 23:44:02 +01:00
|
|
|
|
2023-04-02 16:00:37 +02:00
|
|
|
return [
|
|
|
|
[$sut, 'N'],
|
|
|
|
[$sut, 'Nay'],
|
|
|
|
[$sut, 'Nix'],
|
|
|
|
[$sut, 'No'],
|
|
|
|
[$sut, 'Nope'],
|
|
|
|
[$sut, 'Not'],
|
|
|
|
];
|
2019-04-05 20:11:13 +02:00
|
|
|
}
|
|
|
|
|
2024-02-04 19:23:04 +01:00
|
|
|
/** @return iterable<array{No, mixed}> */
|
|
|
|
public static function providerForInvalidInput(): iterable
|
2019-04-05 20:11:13 +02:00
|
|
|
{
|
2023-04-02 16:00:37 +02:00
|
|
|
$sut = new No();
|
2019-03-20 23:44:02 +01:00
|
|
|
|
2023-04-02 16:00:37 +02:00
|
|
|
return [
|
|
|
|
[$sut, 'Donnot'],
|
|
|
|
[$sut, 'Never'],
|
|
|
|
[$sut, 'Niet'],
|
|
|
|
[$sut, 'Noooooooo'],
|
|
|
|
[$sut, 'Não'],
|
|
|
|
];
|
2019-04-05 20:11:13 +02:00
|
|
|
}
|
2020-08-27 15:29:46 +02:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
$this->locale = (string) setlocale(LC_ALL, '0');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
setlocale(LC_ALL, $this->locale);
|
|
|
|
}
|
2015-01-06 23:14:08 +01:00
|
|
|
}
|