Tests: Fix string length validation tests

This commit is contained in:
Igor Scheller 2023-09-25 17:16:12 +02:00 committed by Michael Weimann
parent 599fff26d4
commit fddae62669
5 changed files with 27 additions and 3 deletions

View File

@ -29,10 +29,14 @@ trait StringInputLength
protected function isDateTime(mixed $input): bool
{
try {
$input = new DateTime($input);
$inputDateTime = new DateTime($input);
$now = new DateTime();
// Min 1s diff to exclude any not auto-detected dates / times like ...
return abs((int) (new DateTime())->diff($input)->format('%s')) > 1;
} catch (Exception $e) {
return abs($inputDateTime->getTimestamp() - $now->getTimestamp()) > 1
// Different timezone to prevent interpreting the value as a timezone which happens with H
&& $inputDateTime->getTimezone()->getName() != $input;
} catch (Exception) {
// Ignore it
}

View File

@ -16,8 +16,11 @@ class BetweenTest extends TestCase
{
$rule = new Between(3, 10);
$this->assertFalse($rule->validate(1));
$this->assertFalse($rule->validate(2));
$this->assertFalse($rule->validate('11'));
$this->assertTrue($rule->validate(3));
$this->assertTrue($rule->validate(5));
$this->assertTrue($rule->validate(10));
$this->assertFalse($rule->validate('AS'));
$this->assertFalse($rule->validate('TestContentThatCounts'));
$this->assertTrue($rule->validate('TESTING'));
@ -25,6 +28,12 @@ class BetweenTest extends TestCase
$rule = new Between('2042-01-01', '2042-10-10');
$this->assertFalse($rule->validate('2000-01-01'));
$this->assertFalse($rule->validate('3000-01-01'));
$this->assertFalse($rule->validate('2041-12-31'));
$this->assertFalse($rule->validate('2042-10-11'));
$this->assertTrue($rule->validate('2042-05-11'));
$this->assertTrue($rule->validate('2042-01-01'));
$this->assertTrue($rule->validate('2042-01-02'));
$this->assertTrue($rule->validate('2042-10-09'));
$this->assertTrue($rule->validate('2042-10-10'));
}
}

View File

@ -18,11 +18,16 @@ class MaxTest extends TestCase
$this->assertFalse($rule->validate(10));
$this->assertFalse($rule->validate('22'));
$this->assertTrue($rule->validate(3));
$this->assertTrue($rule->validate(2));
$this->assertTrue($rule->validate(-10));
$this->assertFalse($rule->validate('TEST'));
$this->assertTrue($rule->validate('AS'));
$rule = new Max('2042-01-01');
$this->assertFalse($rule->validate('2100-01-01'));
$this->assertFalse($rule->validate('2042-01-02'));
$this->assertTrue($rule->validate('2000-01-01'));
$this->assertTrue($rule->validate('2041-12-31'));
$this->assertTrue($rule->validate('2042-01-01'));
}
}

View File

@ -15,14 +15,19 @@ class MinTest extends TestCase
public function testValidate(): void
{
$rule = new Min(3);
$this->assertFalse($rule->validate(-10));
$this->assertFalse($rule->validate(1));
$this->assertFalse($rule->validate('2'));
$this->assertTrue($rule->validate(3));
$this->assertTrue($rule->validate(4));
$this->assertFalse($rule->validate('AS'));
$this->assertTrue($rule->validate('TEST'));
$rule = new Min('2042-01-01');
$this->assertFalse($rule->validate('2000-01-01'));
$this->assertFalse($rule->validate('2041-12-31'));
$this->assertTrue($rule->validate('2042-01-01'));
$this->assertTrue($rule->validate('2042-01-02'));
$this->assertTrue($rule->validate('2345-01-01'));
}
}

View File

@ -40,6 +40,7 @@ class StringInputLengthTest extends TestCase
['com', 3],
['Test', 4],
['H', 1],
['Europe/Berlin', 13],
['3', 3],
[42, 42],
[99.3, 99.3],