From 08133c68ae647e6677077653797591fb14b02e08 Mon Sep 17 00:00:00 2001 From: SUM2046 Date: Mon, 6 Jul 2026 16:44:24 +0530 Subject: [PATCH] export test --- .../DailySalePaymentReportExportTest.php | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 Tests/Feature/DailySalePaymentReportExportTest.php diff --git a/Tests/Feature/DailySalePaymentReportExportTest.php b/Tests/Feature/DailySalePaymentReportExportTest.php new file mode 100644 index 0000000..1c8fa13 --- /dev/null +++ b/Tests/Feature/DailySalePaymentReportExportTest.php @@ -0,0 +1,138 @@ +environment() !== 'local') { + putenv('APP_ENV=local'); + $_ENV['APP_ENV'] = 'local'; + $_SERVER['APP_ENV'] = 'local'; + } + + DB::beginTransaction(); + + $this->reportDate = Carbon::today()->toDateString(); + + $this->cashier = User::factory()->create([ + 'name' => fake()->name(), + 'is_active' => 1, + 'is_deleted' => 0, + ]); + + $this->customer = Customer::factory()->create([ + 'name' => fake()->name(), + 'customer_group_id' => 1, + ]); + + $cashOptionId = DB::table('payment_options')->insertGetId([ + 'name' => 'Cash', + 'is_active' => 1, + ]); + + $saleId = DB::table('sales')->insertGetId([ + 'reference_no' => 'INV-EXPORT-' . fake()->unique()->numerify('####'), + 'user_id' => $this->cashier->id, + 'customer_id' => $this->customer->id, + 'warehouse_id' => 1, + 'item' => 1, + 'total_qty' => 1, + 'total_discount' => 0, + 'total_tax' => 0, + 'total_price' => 4000, + 'grand_total' => 4000, + 'sale_status' => 1, + 'payment_status' => 1, + 'due_amount' => 0, + 'paid_amount' => 4000, + 'created_at' => $this->reportDate . ' 09:00:00', + ]); + + DB::table('payments')->insert([ + 'payment_reference' => 'PAY-EXPORT-1', + 'user_id' => $this->cashier->id, + 'sale_id' => $saleId, + 'amount' => 4000, + 'change' => 0, + 'paying_method' => 'cash', + 'payment_option_id' => $cashOptionId, + 'created_at' => $this->reportDate . ' 09:00:00', + ]); + } + + protected function tearDown(): void + { + DB::rollBack(); + parent::tearDown(); + } + + public function test_pdf_export_downloads_successfully_with_correct_headers() + { + $response = $this->actingAs($this->cashier) + ->get('/dailysalepaymentreportmodule/export_pdf?date=' . $this->reportDate . '&type=pdf'); + + $response->assertStatus(200); + $response->assertHeader('content-type', 'application/pdf'); + + $contentDisposition = $response->headers->get('content-disposition'); + $this->assertStringContainsString('daily-sale-payment-report-' . $this->reportDate, $contentDisposition); + $this->assertStringContainsString('.pdf', $contentDisposition); + } + + public function test_excel_export_downloads_successfully_with_correct_headers() + { + $response = $this->actingAs($this->cashier) + ->get('/dailysalepaymentreportmodule/export?date=' . $this->reportDate . '&type=excel'); + + $response->assertStatus(200); + + $contentDisposition = $response->headers->get('content-disposition'); + $this->assertStringContainsString('Daily_Sale_Payment_Report_' . $this->reportDate, $contentDisposition); + $this->assertStringContainsString('.xlsx', $contentDisposition); + } + + public function test_csv_export_downloads_successfully_with_correct_headers() + { + $response = $this->actingAs($this->cashier) + ->get('/dailysalepaymentreportmodule/export?date=' . $this->reportDate . '&type=csv'); + + $response->assertStatus(200); + + $contentDisposition = $response->headers->get('content-disposition'); + $this->assertStringContainsString('Daily_Sale_Payment_Report_' . $this->reportDate, $contentDisposition); + $this->assertStringContainsString('.csv', $contentDisposition); + } + + public function export_still_works_when_no_sales_exist_for_selected_date() + { + $emptyDate = Carbon::today()->subYears(5)->toDateString(); + + $pdfResponse = $this->actingAs($this->cashier) + ->get('/dailysalepaymentreportmodule/export_pdf?date=' . $emptyDate . '&type=pdf'); + $pdfResponse->assertStatus(200); + + $excelResponse = $this->actingAs($this->cashier) + ->get('/dailysalepaymentreportmodule/export?date=' . $emptyDate . '&type=excel'); + $excelResponse->assertStatus(200); + + $csvResponse = $this->actingAs($this->cashier) + ->get('/dailysalepaymentreportmodule/export?date=' . $emptyDate . '&type=csv'); + $csvResponse->assertStatus(200); + } +}