Skip to content

Commit 742235e

Browse files
committed
fix: conditional branch test mock and psalm types
- update test to mock getActiveBranches (plural) - type getActiveBranches/findActiveBranches as non-nullable array - add triggerType and branches to extra settings type - simplify branch guard in question cloning Signed-off-by: Micke Nordin <kano@sunet.se>
1 parent 94aefa7 commit 742235e

5 files changed

Lines changed: 20 additions & 6 deletions

File tree

lib/Controller/ApiController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ public function newForm(?int $fromId = null): DataResponse {
244244
$this->optionMapper->insert($newOption);
245245
$optionIdMapper[$oldOptionID] = $newOption->getId();
246246
}
247-
$branches = $questionData['extraSettings']['branches'] ?? null;
248-
if (isset($branches) && \is_array($branches) > 0) {
247+
$branches = $questionData['extraSettings']['branches'] ?? [];
248+
if (!empty($branches)) {
249249
foreach ($branches as $branchKey => $branch) {
250250
if (isset($branch['conditions']) && \is_array($branch['conditions'])) {
251251
foreach ($branch['conditions'] as $conditionKey => $cond) {

lib/ResponseDefinitions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
* validationRegex?: string,
4343
* validationType?: string,
4444
* questionType?: string,
45+
* triggerType?: string,
46+
* branches?: list<array<string, mixed>>,
4547
* }
4648
*
4749
* @psalm-type FormsQuestionType = "dropdown"|"multiple"|"multiple_unique"|"date"|"time"|"short"|"long"|"file"|"datetime"|"grid"|"color"|"conditional"

lib/Service/SubmissionService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ private function validateConditionalQuestion(array $question, array $answerData,
797797
* @param array $triggerAnswer The trigger answer values
798798
* @return array The active branches or empty array if none matches
799799
*/
800-
public function getActiveBranches(array $question, array $triggerAnswer): ?array {
800+
public function getActiveBranches(array $question, array $triggerAnswer): array {
801801
$extraSettings = $question['extraSettings'] ?? [];
802802
return $this->findActiveBranches(
803803
$extraSettings['triggerType'] ?? '',
@@ -816,7 +816,7 @@ public function getActiveBranches(array $question, array $triggerAnswer): ?array
816816
* @param array $options The options for the trigger question
817817
* @return array The active branches or empty array if none matches
818818
*/
819-
private function findActiveBranches(string $triggerType, array $triggerAnswer, array $branches, array $options): ?array {
819+
private function findActiveBranches(string $triggerType, array $triggerAnswer, array $branches, array $options): array {
820820
return array_filter($branches, function ($branch) use ($triggerType, $triggerAnswer) {
821821
$conditions = $branch['conditions'] ?? [];
822822
if (empty($conditions)) {

openapi.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,18 @@
582582
},
583583
"questionType": {
584584
"type": "string"
585+
},
586+
"triggerType": {
587+
"type": "string"
588+
},
589+
"branches": {
590+
"type": "array",
591+
"items": {
592+
"type": "object",
593+
"additionalProperties": {
594+
"type": "object"
595+
}
596+
}
585597
}
586598
}
587599
},

tests/Unit/Controller/ApiControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,9 +937,9 @@ public function testNewSubmission_conditionalQuestion() {
937937
}));
938938

939939
$this->submissionService->expects($this->once())
940-
->method('getActiveBranch')
940+
->method('getActiveBranches')
941941
->with($questions[0], $answers[100]['trigger'])
942-
->willReturn($questions[0]['extraSettings']['branches'][0]);
942+
->willReturn([$questions[0]['extraSettings']['branches'][0]]);
943943

944944
// Trigger answer + subquestion answer = 2 inserts
945945
$this->answerMapper->expects($this->exactly(2))

0 commit comments

Comments
 (0)