99 lines
2.7 KiB
PHP
99 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace com\fwdekker\deathnotifier\validator;
|
|
|
|
use Monolog\Test\TestCase;
|
|
|
|
|
|
/**
|
|
* Inheritable unit tests for extensions of `Rule`.
|
|
*/
|
|
abstract class RuleTest extends TestCase
|
|
{
|
|
/**
|
|
* Constructs a new `Rule` of the class under test, using `override`.
|
|
*
|
|
* The returned `Rule` will only be tested under `get_valid_input` and `get_invalid_input`.
|
|
*
|
|
* @param string|null $override the override message
|
|
* @return Rule a new `Rule` of the class under test, using `override`
|
|
*/
|
|
abstract function get_rule(?string $override = null): Rule;
|
|
|
|
/**
|
|
* @return string|null a valid input to give to `get_rule`
|
|
*/
|
|
abstract function get_valid_input(): ?string;
|
|
|
|
/**
|
|
* @return string|null an invalid input to give to `get_rule`
|
|
*/
|
|
abstract function get_invalid_input(): ?string;
|
|
|
|
|
|
public function test_returns_null_if_input_is_valid(): void
|
|
{
|
|
$rule = $this->get_rule();
|
|
|
|
$is_valid = $rule->check(["input" => $this->get_valid_input()], "input");
|
|
|
|
$this->assertNull($is_valid);
|
|
}
|
|
|
|
public function test_returns_not_null_if_input_is_invalid(): void
|
|
{
|
|
$rule = $this->get_rule();
|
|
|
|
$is_valid = $rule->check(["input" => $this->get_invalid_input()], "input");
|
|
|
|
$this->assertNotNull($is_valid);
|
|
}
|
|
|
|
public function test_returns_not_null_if_input_is_not_set(): void
|
|
{
|
|
$rule = $this->get_rule();
|
|
|
|
$is_valid = $rule->check(["input" => $this->get_valid_input()], "does_not_exist");
|
|
|
|
$this->assertNotNull($is_valid);
|
|
}
|
|
|
|
public function test_returns_unsatisfied_payload_if_input_is_invalid(): void
|
|
{
|
|
$rule = $this->get_rule();
|
|
|
|
$is_valid = $rule->check(["input" => $this->get_invalid_input()], "input");
|
|
|
|
$this->assertNotNull($is_valid);
|
|
$this->assertFalse($is_valid->satisfied);
|
|
}
|
|
|
|
public function test_returns_payload_with_overridden_message_if_input_is_invalid(): void
|
|
{
|
|
$override = "Override message.";
|
|
$rule = $this->get_rule($override);
|
|
|
|
$is_valid = $rule->check(["input" => $this->get_invalid_input()], "input");
|
|
|
|
$this->assertNotNull($is_valid);
|
|
$this->assertEquals($override, $is_valid->payload["message"]);
|
|
}
|
|
|
|
public function test_returns_payload_with_key_of_invalid_input_if_input_is_invalid(): void
|
|
{
|
|
$rule = $this->get_rule();
|
|
|
|
$is_valid = $rule->check(
|
|
[
|
|
"valid1" => $this->get_valid_input(),
|
|
"invalid" => $this->get_invalid_input(),
|
|
"valid2" => $this->get_valid_input(),
|
|
],
|
|
"invalid"
|
|
);
|
|
|
|
$this->assertNotNull($is_valid);
|
|
$this->assertEquals("invalid", $is_valid->payload["target"]);
|
|
}
|
|
}
|