feat: decide when a conditional questionnaire question is shown
This commit is contained in:
1 parent
a84275b40e
commit
d06b647dac
4 files changed
+170
No files matched your search
@@ -0,0 +1,70 @@
|
||||
<?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']));
|
||||
}
|
||||
|
||||
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'),
|
||||
),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user