export test

This commit is contained in:
SUM2046
2026-07-06 16:44:24 +05:30
parent a4a7b09c10
commit 08133c68ae

View File

@@ -0,0 +1,138 @@
<?php
namespace Modules\DailySalePaymentReportModule\Tests\Feature;
use App\Models\User;
use Carbon\Carbon;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Modules\DailySalePaymentReportModule\Entities\Customer;
class DailySalePaymentReportExportTest extends TestCase
{
protected $reportDate;
protected $cashier;
protected $customer;
protected function setUp(): void
{
parent::setUp();
if (app()->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);
}
}