56 lines
2.1 KiB
PHP
56 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Model\Questionnaire;
|
|
|
|
use App\Enum\ConditionOperator;
|
|
use App\Model\Questionnaire\Condition;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ConditionTest extends TestCase
|
|
{
|
|
#[DataProvider('provideAnswers')]
|
|
public function testIsMet(ConditionOperator $operator, string|array|int $value, mixed $answer, bool $expected): void
|
|
{
|
|
$condition = new Condition('controlling', $operator, $value);
|
|
|
|
$this->assertSame($expected, $condition->isMet(['controlling' => $answer]));
|
|
}
|
|
|
|
public static function provideAnswers(): iterable
|
|
{
|
|
yield 'equals, matching' => [ConditionOperator::EQUALS, 'yes', 'yes', true];
|
|
yield 'equals, other option' => [ConditionOperator::EQUALS, 'yes', 'no', false];
|
|
yield 'in, listed' => [ConditionOperator::IN, ['bad', 'very_bad'], 'bad', true];
|
|
yield 'in, not listed' => [ConditionOperator::IN, ['bad', 'very_bad'], 'good', false];
|
|
yield 'lte, below' => [ConditionOperator::LTE, 3, 1, true];
|
|
yield 'lte, at the limit' => [ConditionOperator::LTE, 3, 3, true];
|
|
yield 'lte, above' => [ConditionOperator::LTE, 3, 4, false];
|
|
// A string "2" is not what the form or the stored JSON hands over for a rating, so it
|
|
// must not open a follow-up by accident.
|
|
yield 'lte, not an int' => [ConditionOperator::LTE, 3, '2', false];
|
|
}
|
|
|
|
/**
|
|
* An unanswered rating is not "3 or less": the low-rating follow-up must not demand a reason
|
|
* before a rating was given.
|
|
*/
|
|
#[DataProvider('provideOperators')]
|
|
public function testUnansweredIsNeverMet(ConditionOperator $operator, string|array|int $value): void
|
|
{
|
|
$condition = new Condition('controlling', $operator, $value);
|
|
|
|
$this->assertFalse($condition->isMet([]));
|
|
$this->assertFalse($condition->isMet(['controlling' => null]));
|
|
}
|
|
|
|
public static function provideOperators(): iterable
|
|
{
|
|
yield 'equals' => [ConditionOperator::EQUALS, 'yes'];
|
|
yield 'in' => [ConditionOperator::IN, ['yes']];
|
|
yield 'lte' => [ConditionOperator::LTE, 3];
|
|
}
|
|
}
|