Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
daVitekPL committed Nov 7, 2024
1 parent d6b9ee9 commit 253d0e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/Services/QuestionnaireModelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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++;
}
Expand Down
4 changes: 3 additions & 1 deletion tests/Api/QuestionAnswerListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 253d0e3

Please sign in to comment.