|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Derafu: Data Processor - Four-Phase Data Processing Library. |
| 7 | + * |
| 8 | + * Copyright (c) 2025 Esteban De La Fuente Rubio / Derafu <https://www.derafu.dev> |
| 9 | + * Licensed under the MIT License. |
| 10 | + * See LICENSE file for more details. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Derafu\TestsDataProcessor\Rule\Validator\String; |
| 14 | + |
| 15 | +use Derafu\DataProcessor\Exception\ValidationException; |
| 16 | +use Derafu\DataProcessor\Rule\Validator\String\LengthRule; |
| 17 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 18 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +#[CoversClass(LengthRule::class)] |
| 22 | +final class LengthRuleTest extends TestCase |
| 23 | +{ |
| 24 | + private LengthRule $rule; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->rule = new LengthRule(); |
| 29 | + } |
| 30 | + |
| 31 | + #[DataProvider('lengthDataProvider')] |
| 32 | + public function testLength(mixed $value, array $parameters, bool $shouldPass): void |
| 33 | + { |
| 34 | + if (!$shouldPass) { |
| 35 | + $this->expectException(ValidationException::class); |
| 36 | + } |
| 37 | + |
| 38 | + $this->rule->validate($value, $parameters); |
| 39 | + |
| 40 | + if ($shouldPass) { |
| 41 | + $this->assertTrue(true); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public static function lengthDataProvider(): array |
| 46 | + { |
| 47 | + return [ |
| 48 | + 'exact_length' => ['hello', ['5'], true], |
| 49 | + 'too_short' => ['hi', ['5'], false], |
| 50 | + 'too_long' => ['hello world', ['5'], false], |
| 51 | + 'unicode_exact' => ['héllo', ['5'], true], |
| 52 | + 'unicode_too_short' => ['hé', ['5'], false], |
| 53 | + 'non_string' => [123, ['3'], false], |
| 54 | + 'missing_parameter' => ['hello', [], false], |
| 55 | + 'zero_length_treated_as_missing' => ['', ['0'], false], |
| 56 | + ]; |
| 57 | + } |
| 58 | +} |
0 commit comments