respect-validation/tests/unit/Validators/LanguageCodeTest.php
Henrique Moody 7c681fec66
Fix SPDX headers in all files
I ran the `bin/console spdx --fix` with different strategies for
different files. For most of the core classes, since they've been
drastically rebuilt, I've run it with the `git-blame` strategy, for for
the `src/Validators`, in which the API changed completely but the logic
remains the same, I use the `git-log` strategy.
2026-02-03 15:23:23 +01:00

96 lines
3.4 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-FileContributor: Danilo Benevides <danilobenevides01@gmail.com>
* SPDX-FileContributor: Emmerson Siqueira <emmersonsiqueira@gmail.com>
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Validation\Validators;
use DI;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\ContainerRegistry;
use Respect\Validation\Exceptions\InvalidValidatorException;
use Respect\Validation\Exceptions\MissingComposerDependencyException;
use Respect\Validation\Test\RuleTestCase;
#[Group('validator')]
#[CoversClass(LanguageCode::class)]
final class LanguageCodeTest extends RuleTestCase
{
#[Test]
public function itShouldThrowAnExceptionWhenSetIsInvalid(): void
{
$this->expectException(InvalidValidatorException::class);
$this->expectExceptionMessage(
'"whatever" is not a valid set for ISO 639-3 (Available: "alpha-2" and "alpha-3")',
);
// @phpstan-ignore-next-line
new LanguageCode('whatever');
}
#[Test]
public function shouldThrowWhenMissingComponent(): void
{
$mainContainer = ContainerRegistry::getContainer();
ContainerRegistry::setContainer((new DI\ContainerBuilder())->useAutowiring(false)->build());
try {
new LanguageCode('alpha-3');
$this->fail('Expected MissingComposerDependencyException was not thrown.');
} catch (MissingComposerDependencyException $e) {
$this->assertStringContainsString(
'LanguageCode rule requires PHP ISO Codes',
$e->getMessage(),
);
} finally {
ContainerRegistry::setContainer($mainContainer);
}
}
/** @return iterable<array{LanguageCode, mixed}> */
public static function providerForValidInput(): iterable
{
$sutAlpha2 = new LanguageCode('alpha-2');
$sutAlpha3 = new LanguageCode('alpha-3');
return [
'alpha-2: en' => [$sutAlpha2, 'en'],
'alpha-2: it' => [$sutAlpha2, 'it'],
'alpha-2: la' => [$sutAlpha2, 'la'],
'alpha-2: pt' => [$sutAlpha2, 'pt'],
'alpha-3: eng' => [$sutAlpha3, 'eng'],
'alpha-3: ita' => [$sutAlpha3, 'ita'],
'alpha-3: lat' => [$sutAlpha3, 'lat'],
'alpha-3: por' => [$sutAlpha3, 'por'],
];
}
/** @return iterable<array{LanguageCode, mixed}> */
public static function providerForInvalidInput(): iterable
{
$sutAlpha2 = new LanguageCode('alpha-2');
$sutAlpha3 = new LanguageCode('alpha-3');
return [
'alpha-2: alpha-3 code' => [$sutAlpha2, 'por'],
'alpha-2: boolean' => [$sutAlpha2, false],
'alpha-2: empty array' => [$sutAlpha2, []],
'alpha-2: empty' => [$sutAlpha2, ''],
'alpha-2: null' => [$sutAlpha2, null],
'alpha-3: alpha-2 code' => [$sutAlpha3, 'pt'],
'alpha-3: boolean' => [$sutAlpha3, true],
'alpha-3: empty array' => [$sutAlpha3, []],
'alpha-3: empty' => [$sutAlpha3, ''],
'alpha-3: null' => [$sutAlpha3, ''],
];
}
}