feat(tickets): add PDF and Excel export endpoints
This commit is contained in:
138
tests/Unit/Ticket/AdminAppTicketExportServiceTest.php
Normal file
138
tests/Unit/Ticket/AdminAppTicketExportServiceTest.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Ticket;
|
||||
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticket\Services\AdminAppTicketExcelService;
|
||||
use App\Domains\Ticket\Services\AdminAppTicketPdfService;
|
||||
use App\Domains\Ticket\Services\AdminAppTicketReportService;
|
||||
use Barryvdh\DomPDF\ServiceProvider;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Mockery;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AdminAppTicketExportServiceTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->app->register(ServiceProvider::class);
|
||||
Carbon::setTestNow(Carbon::parse('2026-08-24 17:53:00', 'UTC'));
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Carbon::setTestNow();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_it_downloads_the_ticket_report_as_an_excel_file(): void
|
||||
{
|
||||
$response = (new AdminAppTicketExcelService($this->reportService()))
|
||||
->download($this->tenant(), collect(), 'America/La_Paz');
|
||||
|
||||
$this->assertSame(
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
$response->headers->get('content-type'),
|
||||
);
|
||||
$this->assertStringContainsString(
|
||||
'attachment; filename=tickets_acme_20260824_135300.xlsx',
|
||||
(string) $response->headers->get('content-disposition'),
|
||||
);
|
||||
|
||||
$path = $this->spreadsheetPath($response);
|
||||
try {
|
||||
$sheet = IOFactory::load($path)->getActiveSheet();
|
||||
|
||||
$this->assertSame('Tickets', $sheet->getTitle());
|
||||
$this->assertSame('N° de orden', $sheet->getCell('A1')->getValue());
|
||||
$this->assertSame('#15', $sheet->getCell('A2')->getValue());
|
||||
$this->assertSame('Cena', $sheet->getCell('D2')->getValue());
|
||||
$this->assertSame(8000.0, $sheet->getCell('E2')->getValue());
|
||||
$this->assertSame('00000000-0000-0000-0000-000000000001', $sheet->getCell('G2')->getValue());
|
||||
$this->assertSame('Usado', $sheet->getCell('I2')->getValue());
|
||||
} finally {
|
||||
@unlink($path);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_it_downloads_the_ticket_report_as_a_pdf(): void
|
||||
{
|
||||
$response = (new AdminAppTicketPdfService($this->reportService()))
|
||||
->download($this->tenant(), collect(), 'America/La_Paz');
|
||||
|
||||
$this->assertSame('application/pdf', $response->headers->get('content-type'));
|
||||
$this->assertStringContainsString(
|
||||
'attachment; filename=tickets_acme_20260824_135300.pdf',
|
||||
(string) $response->headers->get('content-disposition'),
|
||||
);
|
||||
$this->assertStringStartsWith('%PDF', $response->getContent());
|
||||
}
|
||||
|
||||
public function test_the_pdf_view_contains_the_report_data_and_requested_timezone(): void
|
||||
{
|
||||
$html = view('pdf.adminapp.tickets', [
|
||||
'tenant' => $this->tenant(),
|
||||
'tickets' => collect([$this->row()]),
|
||||
'generatedAt' => now(),
|
||||
'timeZone' => 'America/La_Paz',
|
||||
])->render();
|
||||
|
||||
$this->assertStringContainsString('Generado el 24/08/2026 13:53', $html);
|
||||
$this->assertStringContainsString('00000000-0000-0000-0000-000000000001', $html);
|
||||
$this->assertStringContainsString('Cena', $html);
|
||||
$this->assertStringContainsString('24/08/2026 13:53', $html);
|
||||
}
|
||||
|
||||
private function reportService(): AdminAppTicketReportService
|
||||
{
|
||||
$service = Mockery::mock(AdminAppTicketReportService::class);
|
||||
$service->shouldReceive('rows')->once()->andReturn(collect([$this->row()]));
|
||||
|
||||
return $service;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function row(): array
|
||||
{
|
||||
return [
|
||||
'order_number' => 15,
|
||||
'category' => 'Comida',
|
||||
'product' => '12/10',
|
||||
'type' => 'Cena',
|
||||
'amount' => 8000.0,
|
||||
'client' => 'Cliente Test',
|
||||
'ticket' => '00000000-0000-0000-0000-000000000001',
|
||||
'date' => now(),
|
||||
'status' => 'Usado',
|
||||
'scanned_by' => 'Admin Test',
|
||||
];
|
||||
}
|
||||
|
||||
private function tenant(): Tenant
|
||||
{
|
||||
return (new Tenant)->forceFill([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme Eventos',
|
||||
]);
|
||||
}
|
||||
|
||||
private function spreadsheetPath(StreamedResponse $response): string
|
||||
{
|
||||
ob_start();
|
||||
($response->getCallback())();
|
||||
$contents = ob_get_clean();
|
||||
$this->assertIsString($contents);
|
||||
$this->assertStringStartsWith('PK', $contents);
|
||||
|
||||
$path = tempnam(sys_get_temp_dir(), 'shopit_ticket_excel_');
|
||||
$this->assertNotFalse($path);
|
||||
file_put_contents($path, $contents);
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user