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

45 lines
1.1 KiB
PHP

<?php /** @noinspection PhpUnhandledExceptionInspection */
namespace com\fwdekker\deathnotifier\validation;
/**
* Unit tests for {@see IsValidCsrfTokenRule}.
*/
class IsValidCsrfTokenRuleTest extends RuleTest
{
public function check_provider(): array
{
$type = InvalidTypeException::class;
return [
"exception if input is not set" => [new IsValidCsrfTokenRule(), null, $type, "Missing CSRF token."],
"exception if input is not a string" => [new IsValidCsrfTokenRule(), 170, $type, "CSRF token should be string, but is integer."],
];
}
protected function setUp(): void
{
$_SESSION = [];
}
public function test_exception_if_input_is_incorrect_token(): void
{
$_SESSION["token"] = "valid";
self::expectException(InvalidValueException::class);
(new IsValidCsrfTokenRule())->check(["key" => "invalid"], "key");
}
public function test_no_exception_if_input_is_correct_token(): void
{
$_SESSION["token"] = "valid";
(new IsValidCsrfTokenRule())->check(["key" => "valid"], "key");
self::assertTrue(true);
}
}