From 253d0e345e44b5c2a9a74a0116c9d68a25974eed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Wis=CC=81niewski?= Date: Thu, 7 Nov 2024 08:51:44 +0100 Subject: [PATCH] Fix test --- src/Services/QuestionnaireModelService.php | 21 +++++++++++++++++++++ tests/Api/QuestionAnswerListTest.php | 4 +++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Services/QuestionnaireModelService.php b/src/Services/QuestionnaireModelService.php index 6c5952e..1d013e6 100644 --- a/src/Services/QuestionnaireModelService.php +++ b/src/Services/QuestionnaireModelService.php @@ -133,6 +133,7 @@ public function getQuestionnaireDataToExport(QuestionnaireModelDto $dto, Questio ]; return match ($questionnaireModelType->model_class) { + // @phpstan-ignore-next-line Consultation::class => $this->forConsultations($result, $groupBy, $dto), default => $this->default($result, $groupBy, $dto), }; @@ -145,15 +146,22 @@ private function default(Builder $result, array $groupBy, QuestionnaireModelDto ->get(); foreach ($result as $value) { + // @phpstan-ignore-next-line $rates = explode(';', $value->rates); + // @phpstan-ignore-next-line $notes = explode(';', $value->notes); + // @phpstan-ignore-next-line $dates = explode(';', $value->answer_dates); for ($i = 0; $i < count($dates); $i++) { + // @phpstan-ignore-next-line $value->{$value->question_title . ' - rate #' . $i} = $rates[$i]; + // @phpstan-ignore-next-line $value->{$value->question_title . ' - note #' . $i} = $notes[$i]; + // @phpstan-ignore-next-line $value->{$value->question_title . ' - date #' . $i} = $dates[$i]; $answerTime = Carbon::make($dates[$i]); + // @phpstan-ignore-next-line $value->{$value->question_title . ' - timestamp #' . $i} = $answerTime->timestamp; } } @@ -187,26 +195,39 @@ private function forConsultations(Builder $result, array $groupBy, Questionnaire $previous = $i > 0 ? $result->get($i - 1) : null; $next = $i === $result->count() - 1 ? null : $result->get($i + 1); + // @phpstan-ignore-next-line $datesGraterThan = $value->consultation_start; + // @phpstan-ignore-next-line if (!$previous || $previous->user_id !== $value->user_id) { $lastAnswerIndex = 0; } + // @phpstan-ignore-next-line $datesLessThan = $next && $next->user_id === $value->user_id ? $next->consultation_start : null; + // @phpstan-ignore-next-line $rates = explode(';', $value->rates); + // @phpstan-ignore-next-line $notes = explode(';', $value->notes); + // @phpstan-ignore-next-line $dates = explode(';', $value->answer_dates); while ($lastAnswerIndex < count($dates) && ($datesLessThan === null || Carbon::make($datesLessThan)->isAfter($dates[$lastAnswerIndex]))) { + // @phpstan-ignore-next-line $consultationStartTimestamp = Carbon::make($value->consultation_start)->timestamp; + // @phpstan-ignore-next-line $value->{$consultationStartTimestamp . ' - rate #' . $lastAnswerIndex . ' - ' . $value->question_title} = $rates[$lastAnswerIndex]; + // @phpstan-ignore-next-line $value->{$consultationStartTimestamp . ' - note #' . $lastAnswerIndex . ' - ' . $value->question_title} = $notes[$lastAnswerIndex]; + // @phpstan-ignore-next-line $value->{$consultationStartTimestamp . ' - date #' . $lastAnswerIndex . ' - ' . $value->question_title} = $dates[$lastAnswerIndex]; $answerTime = Carbon::make($dates[$lastAnswerIndex]); + // @phpstan-ignore-next-line $value->{$consultationStartTimestamp . ' - timestamp #' . $lastAnswerIndex . ' - ' . $value->question_title} = $answerTime->timestamp; + // @phpstan-ignore-next-line $value->{$consultationStartTimestamp . ' - seconds after start #' . $lastAnswerIndex . ' - ' . $value->question_title} = $answerTime->diffInSeconds(Carbon::make($value->consultation_start)); + // @phpstan-ignore-next-line $value->{$consultationStartTimestamp . ' - answered after consultation #' . $lastAnswerIndex . ' - ' . $value->question_title} = $answerTime->isAfter(Carbon::make($value->consultation_end)) ? 'Tak' : 'Nie' ; $lastAnswerIndex++; } diff --git a/tests/Api/QuestionAnswerListTest.php b/tests/Api/QuestionAnswerListTest.php index 8d91f61..158ad7f 100644 --- a/tests/Api/QuestionAnswerListTest.php +++ b/tests/Api/QuestionAnswerListTest.php @@ -13,6 +13,7 @@ use EscolaLms\Questionnaire\Models\QuestionnaireModelType; use EscolaLms\Questionnaire\Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Maatwebsite\Excel\Facades\Excel; class QuestionAnswerListTest extends TestCase { @@ -345,7 +346,8 @@ public function testExportReport(): void 'user_id' => $user4->getKey(), ]); - $response = $this->actingAs($this->user, 'api')->json( + Excel::fake(); + $this->actingAs($this->user, 'api')->json( 'get', '/api/admin/questionnaire/' . $questionnaireModel->modelableType->title . '/' . $questionnaireModel->model_id . '/' . $questionnaire->getKey() . '/export', )->assertOk();