87 lines
3.1 KiB
PHP
87 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Model\Questionnaire;
|
|
|
|
use App\Enum\ConditionOperator;
|
|
use App\Enum\QuestionType;
|
|
use App\Model\Questionnaire\Condition;
|
|
use App\Model\Questionnaire\Definition;
|
|
use App\Model\Questionnaire\Option;
|
|
use App\Model\Questionnaire\Question;
|
|
use App\Model\Questionnaire\Section;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class DefinitionTest extends TestCase
|
|
{
|
|
public function testQuestionWithoutConditionIsAlwaysVisible(): void
|
|
{
|
|
$definition = $this->definition();
|
|
|
|
$this->assertTrue($definition->isVisible($definition->getQuestion('damage'), []));
|
|
}
|
|
|
|
public function testFollowUpIsVisibleOnlyForItsAnswer(): void
|
|
{
|
|
$definition = $this->definition();
|
|
$followUp = $definition->getQuestion('claim');
|
|
|
|
$this->assertTrue($definition->isVisible($followUp, ['damage' => 'yes']));
|
|
$this->assertFalse($definition->isVisible($followUp, ['damage' => 'no']));
|
|
}
|
|
|
|
/**
|
|
* "details" depends on "claim", which depends on "damage". Switching "damage" to "no" hides
|
|
* "claim"; its stale answer must not keep "details" open.
|
|
*/
|
|
public function testFollowUpOfAHiddenQuestionIsHidden(): void
|
|
{
|
|
$definition = $this->definition();
|
|
$details = $definition->getQuestion('details');
|
|
|
|
$this->assertTrue($definition->isVisible($details, ['damage' => 'yes', 'claim' => 'expected']));
|
|
$this->assertFalse($definition->isVisible($details, ['damage' => 'no', 'claim' => 'expected']));
|
|
}
|
|
|
|
public function testWithoutHiddenAnswersDropsHiddenEmptyAndUnknownAnswers(): void
|
|
{
|
|
$answers = [
|
|
'damage' => 'no',
|
|
'claim' => 'expected', // hidden now that damage is "no"
|
|
'details' => 'Spiegel ab', // hidden in turn
|
|
'unknown' => 'x', // not a question of this definition
|
|
];
|
|
|
|
$this->assertSame(['damage' => 'no'], $this->definition()->withoutHiddenAnswers($answers));
|
|
$this->assertSame(
|
|
['damage' => 'yes', 'claim' => 'settled'],
|
|
$this->definition()->withoutHiddenAnswers(['damage' => 'yes', 'claim' => 'settled', 'details' => '']),
|
|
);
|
|
}
|
|
|
|
private function definition(): Definition
|
|
{
|
|
$yesNo = [new Option('yes', 'Ja'), new Option('no', 'Nein')];
|
|
|
|
return new Definition('sample', 1, 'Beispiel', null, [
|
|
new Section('main', null, [
|
|
new Question('damage', 'Schäden?', QuestionType::CHOICE, options: $yesNo),
|
|
new Question(
|
|
'claim',
|
|
'Kommt noch was?',
|
|
QuestionType::CHOICE,
|
|
options: [new Option('expected', 'Ja'), new Option('settled', 'Nein')],
|
|
showIf: new Condition('damage', ConditionOperator::EQUALS, 'yes'),
|
|
),
|
|
new Question(
|
|
'details',
|
|
'Was ist passiert?',
|
|
QuestionType::TEXTAREA,
|
|
showIf: new Condition('claim', ConditionOperator::EQUALS, 'expected'),
|
|
),
|
|
]),
|
|
]);
|
|
}
|
|
}
|