death-notifier/src/test/php/com/fwdekker/deathnotifier/validator/EqualsCliPasswordRuleTest.php

69 lines
2.7 KiB
PHP

<?php
namespace com\fwdekker\deathnotifier\validator;
use com\fwdekker\deathnotifier\Config;
use com\fwdekker\deathnotifier\IllegalArgumentError;
use PHPUnit\Framework\TestCase;
use Throwable;
/**
* Unit tests for {@see EqualsCliPasswordRule}.
*/
class EqualsCliPasswordRuleTest extends TestCase
{
/**
* Tests the output of {@see EqualsCliPasswordRule::check()}.
*
* @param string $name the name to give to the test case
* @param string|null $password the password that is set in the configuration
* @param string|null $input the user's input
* @param class-string<Throwable>|null $exception the exception that is asserted to be thrown
* @param string|null $exception_message the exception message that is asserted
* @return void
* @throws InvalidInputException if {@see $exception} is `null` but an exception is thrown
* @dataProvider check_provider
*/
public function test_check(string $name, ?string $password, ?string $input, ?string $exception,
?string $exception_message): void
{
self::setName($name);
if ($exception !== null)
self::expectException($exception);
if ($exception_message !== null)
self::expectExceptionMessage($exception_message);
Config::_set(EqualsCliPasswordRule::CONFIG_KEY, $password);
(new EqualsCliPasswordRule())->check(["key" => $input], "key");
Config::_reset();
if ($exception === null && $exception_message === null)
self::assertTrue(true);
}
/**
* Returns the test cases.
*
* @return array<array{string, string|null, string|null, class-string<Throwable>|null, string|null}> the test cases
* @see RuleTest::test_check()
*/
public function check_provider(): array
{
$hash = "\$2y\$04\$fwXTw7Rjzw0EpU094u4agOBaBNqtCHGc4TMoxfbPrxuqO5tpYyRka"; # Hash of "password"
$error = IllegalArgumentError::class;
$exception = InvalidInputException::class;
return [
["error if password is not set", null, "input", $error, "The CLI is disabled because the CLI password is not set."],
["error if password is blank", " ", "input", $error, "The CLI is disabled because the CLI password is not set."],
["error if password is default", EqualsCliPasswordRule::DEFAULT, "input", $error, "The CLI is disabled because the CLI password is set to the default."],
["exception if input is not set", $hash, null, $exception, "This operation requires the CLI password."],
["exception if input is incorrect", $hash, "incorrect", $exception, "Incorrect CLI password."],
["no exception if input is correct", $hash, "password", null, null],
];
}
}