death-notifier/src/test/php/com/fwdekker/deathnotifier/validation/HasStringLengthRuleTest.php

48 lines
2.2 KiB
PHP

<?php
namespace com\fwdekker\deathnotifier\validation;
use InvalidArgumentException;
/**
* Unit tests for {@see HasStringLengthRule}.
*/
class HasStringLengthRuleTest extends RuleTestTemplate
{
public static function check_provider(): array
{
$type = InvalidTypeException::class;
$value = InvalidValueException::class;
return [
"exception if input is not set" => [new HasStringLengthRule(2, 3), null, $type, "Required input 'key' not set."],
"exception if input is not a string" => [new HasStringLengthRule(2, 3), 221, $type, "Input 'key' should be string, but is integer."],
"exception if input is too short" => [new HasStringLengthRule(3, 4), "a", $value, "Use at least 3 characters."],
"exception if input is too long" => [new HasStringLengthRule(4, 6), "long-input", $value, "Use at most 6 characters."],
"exception if input is too short and there is no maximum" => [new HasStringLengthRule(3, null), "a", $value, "Use at least 3 characters."],
"exception if input is too long and there is no minimum" => [new HasStringLengthRule(null, 6), "long-input", $value, "Use at most 6 characters."],
"no exception if input is minimum length" => [new HasStringLengthRule(2, 5), "je", null, null],
"no exception if input is maximum length" => [new HasStringLengthRule(3, 4), "word", null, null],
"no exception if input is within boundaries" => [new HasStringLengthRule(4, 7), "string", null, null],
"no exception if input is empty and there is no minimum" => [new HasStringLengthRule(null, 4), "te", null, null],
"no exception if input is long and there is no maximum" => [new HasStringLengthRule(2, null), "a-very-long-string", null, null],
];
}
public function test_constructor_throws_error_if_both_limits_are_null(): void
{
self::expectException(InvalidArgumentException::class);
new HasStringLengthRule();
}
public function test_constructor_throws_error_if_minimum_exceeds_maximum(): void
{
self::expectException(InvalidArgumentException::class);
new HasStringLengthRule(4, 2);
}
}