101 lines
3.7 KiB
PHP
101 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Entity;
|
|
|
|
use App\Entity\Application;
|
|
use App\Entity\Assignment;
|
|
use App\Entity\Disposition;
|
|
use App\Entity\QuestionnaireResponse;
|
|
use App\Entity\Teamer;
|
|
use App\Enum\ConditionOperator;
|
|
use App\Enum\QuestionnaireResponseStatus;
|
|
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 QuestionnaireResponseTest extends TestCase
|
|
{
|
|
public function testNewResponseIsADraftOfTheCurrentVersion(): void
|
|
{
|
|
$response = $this->response();
|
|
|
|
$this->assertSame('sample', $response->getQuestionnaire());
|
|
$this->assertSame(1, $response->getVersion());
|
|
$this->assertSame(QuestionnaireResponseStatus::DRAFT, $response->getStatus());
|
|
$this->assertNull($response->getSubmittedAt());
|
|
}
|
|
|
|
/**
|
|
* The respondent switched "damage" back to "no" after writing a description; switching it to
|
|
* "yes" again must bring the description back.
|
|
*/
|
|
public function testDraftKeepsAnswersToHiddenQuestions(): void
|
|
{
|
|
$response = $this->response()->saveDraft(['damage' => 'no', 'details' => 'Spiegel ab'], ['teamerName' => 'Kim']);
|
|
|
|
$this->assertSame(['damage' => 'no', 'details' => 'Spiegel ab'], $response->getAnswers());
|
|
$this->assertSame(['teamerName' => 'Kim'], $response->getContext());
|
|
$this->assertFalse($response->isSubmitted());
|
|
}
|
|
|
|
public function testSubmitDropsAnswersToHiddenQuestions(): void
|
|
{
|
|
$now = new \DateTimeImmutable('2026-03-01 12:00');
|
|
$response = $this->response()->submit($this->definition(), ['damage' => 'no', 'details' => 'Spiegel ab'], [], $now);
|
|
|
|
$this->assertSame(['damage' => 'no'], $response->getAnswers());
|
|
$this->assertTrue($response->isSubmitted());
|
|
$this->assertSame($now, $response->getSubmittedAt());
|
|
}
|
|
|
|
/**
|
|
* A draft begun before the definition was bumped is submitted, and therefore rendered, under
|
|
* the version it was completed in.
|
|
*/
|
|
public function testSubmitRecordsTheVersionAnsweredUnder(): void
|
|
{
|
|
$response = $this->response()->submit($this->definition(version: 2), ['damage' => 'no'], [], new \DateTimeImmutable());
|
|
|
|
$this->assertSame(2, $response->getVersion());
|
|
}
|
|
|
|
public function testSubmittedResponseCannotBeChanged(): void
|
|
{
|
|
$response = $this->response()->submit($this->definition(), ['damage' => 'no'], [], new \DateTimeImmutable());
|
|
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$response->saveDraft(['damage' => 'yes'], []);
|
|
}
|
|
|
|
public function testResponseCannotBeSubmittedWithAnotherQuestionnaire(): void
|
|
{
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$this->response()->submit($this->definition(key: 'other'), [], [], new \DateTimeImmutable());
|
|
}
|
|
|
|
private function response(): QuestionnaireResponse
|
|
{
|
|
$disposition = new Disposition(new Application(new Assignment(), new Teamer()));
|
|
|
|
return QuestionnaireResponse::forDisposition($this->definition(), $disposition);
|
|
}
|
|
|
|
private function definition(string $key = 'sample', int $version = 1): Definition
|
|
{
|
|
return new Definition($key, $version, 'Beispiel', null, [
|
|
new Section('main', null, [
|
|
new Question('damage', 'Schäden?', QuestionType::CHOICE, options: [new Option('yes', 'Ja'), new Option('no', 'Nein')]),
|
|
new Question('details', 'Was ist passiert?', QuestionType::TEXTAREA, showIf: new Condition('damage', ConditionOperator::EQUALS, 'yes')),
|
|
]),
|
|
]);
|
|
}
|
|
}
|