shopit-back/tests/Unit/Ticket/AdminAppTicketExportService...

155 lines
5.4 KiB
PHP

<?php
namespace Tests\Unit\Ticket;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Ticket\Services\AdminAppTicketColumnService;
use App\Domains\Ticket\Services\AdminAppTicketExcelService;
use App\Domains\Ticket\Services\AdminAppTicketPdfService;
use App\Domains\Ticket\Services\AdminAppTicketReportService;
use App\Domains\Ticket\Services\AdminAppTicketRowService;
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(), $this->columnService()))
->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_fiesta_futbol_infantil_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('Vianda', $sheet->getCell('D2')->getValue());
$this->assertSame('12/10', $sheet->getCell('E2')->getValue());
$this->assertSame(8000.0, $sheet->getCell('G2')->getValue());
$this->assertSame('25', $sheet->getCell('I2')->getValue());
$this->assertSame('Usado', $sheet->getCell('J2')->getValue());
} finally {
@unlink($path);
}
}
public function test_it_downloads_the_ticket_report_as_a_pdf(): void
{
$response = (new AdminAppTicketPdfService($this->reportService(), $this->columnService()))
->download($this->tenant(), collect(), 'America/La_Paz');
$this->assertSame('application/pdf', $response->headers->get('content-type'));
$this->assertStringContainsString(
'attachment; filename=tickets_fiesta_futbol_infantil_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
{
$columns = $this->columnService()->columns($this->tenant());
$html = view('pdf.adminapp.tickets', [
'tenant' => $this->tenant(),
'columns' => $columns,
'tickets' => (new AdminAppTicketRowService)->displayRows(
collect([$this->row()]),
$columns,
'America/La_Paz',
),
'generatedAt' => now(),
'timeZone' => 'America/La_Paz',
])->render();
$this->assertStringContainsString('Generado el 24/08/2026 13:53', $html);
$this->assertStringContainsString('25', $html);
$this->assertStringNotContainsString('00000000-0000-0000-0000-000000000001', $html);
$this->assertStringContainsString('Almuerzo', $html);
$this->assertStringContainsString('Vianda', $html);
}
private function reportService(): AdminAppTicketReportService
{
$rowService = Mockery::mock(AdminAppTicketRowService::class)->makePartial();
$rowService->shouldReceive('rows')->once()->andReturn(collect([$this->row()]));
return new AdminAppTicketReportService($rowService);
}
/** @return array<string, mixed> */
private function row(): array
{
return [
'order_number' => 15,
'category' => 'Comida',
'product' => 'Almuerzo',
'type' => 'Vianda',
'date' => '12/10',
'size' => '-',
'amount' => 8000.0,
'client' => 'Cliente Test',
'id' => 25,
'status' => 'used',
'scanned_by' => 'Admin Test',
];
}
private function tenant(): Tenant
{
return (new Tenant)->forceFill([
'codigo' => 'fiesta_futbol_infantil',
'nombre' => 'Fiesta del Fútbol Infantil',
]);
}
private function columnService(): AdminAppTicketColumnService
{
return new AdminAppTicketColumnService;
}
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;
}
}