respect-validation/tests/unit/Validators/PostalCodeTest.php
Henrique Moody 7db3bea8a6
Enhance LintSpdxCommand with contributor tracking and header normalization
Improves SPDX header linting to ensure consistent license metadata across
the codebase.

Key changes:

- Enforce deterministic tag ordering (License-Identifier, FileCopyrightText,
  FileContributor) to ensure consistency, prevent merge conflicts, and
  simplify code reviews

- Add contributor alias mapping to consolidate contributors with multiple
  emails or name variations (e.g., "nickl-" → "Nick Lombard")

- Add --contributions-strategy option with "blame" (current code authors)
  and "log" (all historical contributors) to support different attribution
  philosophies

- Add optional path argument to lint specific files or directories

- Add --fix option to automatically correct header issues

Assisted-by: Claude Code (claude-opus-4-5-20251101)
2026-02-03 15:23:20 +01:00

121 lines
4.4 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-FileContributor: Axel Wargnier <axel@axessweb.fr>
* SPDX-FileContributor: Bogus <g.predl@edis.at>
* SPDX-FileContributor: Brian Johnson
* SPDX-FileContributor: Daniel Alt <julien.altenburg@gmail.com>
* SPDX-FileContributor: Daniel Altenburg
* SPDX-FileContributor: Danilo Correa <danilosilva87@gmail.com>
* SPDX-FileContributor: Gabriel Caruso <carusogabriel34@gmail.com>
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
* SPDX-FileContributor: Markus.Lauer <markus@kfm-motorraeder.de>
* SPDX-FileContributor: Mateusz Burzyński <matburzy@gmail.com>
* SPDX-FileContributor: Sebastian <me@sebastianpontow.de>
* SPDX-FileContributor: William Espindola <oi@williamespindola.com.br>
* SPDX-FileContributor: ong-ar <reclusis@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Validation\Validators;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\InvalidValidatorException;
use Respect\Validation\Test\RuleTestCase;
#[Group('validator')]
#[CoversClass(PostalCode::class)]
final class PostalCodeTest extends RuleTestCase
{
#[Test]
public function shouldValidateEmptyStringsWhenUsingDefaultPattern(): void
{
$validator = new PostalCode('ZW');
self::assertValidInput($validator, '');
}
#[Test]
public function shouldNotValidateNonEmptyStringsWhenUsingDefaultPattern(): void
{
$validator = new PostalCode('ZW');
self::assertInvalidInput($validator, ' ');
}
#[Test]
public function shouldThrowsExceptionWhenCountryCodeIsNotValid(): void
{
$this->expectException(InvalidValidatorException::class);
$this->expectExceptionMessage('Cannot validate postal code from "Whatever" country');
new PostalCode('Whatever');
}
/** @return iterable<array{PostalCode, mixed}> */
public static function providerForValidInput(): iterable
{
return [
[new PostalCode('BR'), '02179-000'],
[new PostalCode('BR'), '02179000'],
[new PostalCode('CA'), 'A1A 2B2'],
[new PostalCode('GB'), 'GIR 0AA'],
[new PostalCode('GB'), 'PR1 9LY'],
[new PostalCode('US'), '02179'],
[new PostalCode('YE'), ''],
[new PostalCode('PL'), '99-300'],
[new PostalCode('NL'), '1012 GX'],
[new PostalCode('NL'), '1012GX'],
[new PostalCode('PT'), '3660-606'],
[new PostalCode('PT'), '3660606'],
[new PostalCode('CO'), '110231'],
[new PostalCode('KR'), '03187'],
[new PostalCode('IE'), 'D14 YD91'],
[new PostalCode('IE'), 'D6W 3333'],
[new PostalCode('EC'), '170515'],
[new PostalCode('IL'), '7019900'],
[new PostalCode('IL'), '94142'],
[new PostalCode('KY'), 'KY1-1102'],
[new PostalCode('KY'), 'KY2-2001'],
[new PostalCode('KY'), 'KY2-2001'],
[new PostalCode('KY'), 'KY3-2500'],
[new PostalCode('AM'), '0010'],
[new PostalCode('RS'), '24430'],
[new PostalCode('RS'), '244300'],
[new PostalCode('GR'), '24430'],
[new PostalCode('GR'), '244 30'],
[new PostalCode('KH'), '12080'],
[new PostalCode('KH'), '120802'],
[new PostalCode('CZ', true), '120 80'],
];
}
/** @return iterable<array{PostalCode, mixed}> */
public static function providerForInvalidInput(): iterable
{
return [
[new PostalCode('BR'), '02179'],
[new PostalCode('BR'), '02179.000'],
[new PostalCode('CA'), '1A1B2B'],
[new PostalCode('GB'), 'GIR 00A'],
[new PostalCode('GB', true), 'GIR0AA'],
[new PostalCode('GB', true), 'PR19LY'],
[new PostalCode('US'), '021 79'],
[new PostalCode('YE'), '02179'],
[new PostalCode('PL'), '99300'],
[new PostalCode('KR'), '548940'],
[new PostalCode('KR'), '548-940'],
[new PostalCode('EC'), 'A1234B'],
[new PostalCode('KY'), 'KY4-2500'],
[new PostalCode('AM'), '375010'],
[new PostalCode('KH'), '1208'],
[new PostalCode('CZ', true), '12080'],
];
}
}