How in http tests test control which genarate and download pdf file?

14 views Asked by At

In Laravel 10 app I have a report with is generated using barryvdh/laravel-dompdf 2 package, which is run with url in routes/api.php :

Route::post('showTaskDetailsReport/{taskId}', [ReportsController::class, 'showTaskDetailsReport'])->name('reports.showTaskDetailsReport');

And in controller :app/Http/Controllers/Api/ReportsController.php

public function showTaskDetailsReport(int $taskId)
{
    try {
        $task = $this->taskCrudRepository->get(id: $taskId);

        $taskDetailsReport = new TaskDetailsReport;
        $taskDetailsReport->setTask($task);
        ...
        $generatedContent = $taskDetailsReport->generateContent();
        return $taskDetailsReport->loadPdf($generatedContent);
    } catch (\Exception|\Error $e) {
    }
}

and in class app/Library/Reports/TaskDetailsReport.php :

public function loadPdf($pdf): \Symfony\Component\HttpFoundation\Response
{
    $filename = 'task-details-report-' . \Str::slug($this->task->title) . '.pdf';
    return $pdf->download($filename);
}

I run this report from blade report as:

<form method="POST" action="{{ route('reports.showTaskDetailsReport', 9) }}">
    @csrf
    <button type="submit" >
        {{ __('Report') }}
    </button>
</form>

and it work sok, but next I need to make http tests for this action . I made it as :

public function test_1_ShowTaskDetailsReport()
{
    $task = Task::factory()->create();

    $response = $this
        ->actingAs($loggedUser, 'sanctum')
        ->postJson(route('reports.showTaskDetailsReport', [
            'taskId' => $task->id,
        ]));

    $response->assertStatus(200);
}

and again this test works ok and I got 200 code, but I would like in some way to check that file is uploaded? Maybe get name or type of generated file ?

0

There are 0 answers