Skip to content

Commit

Permalink
feat: add comprehensive tests for invoice function
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] committed Mar 11, 2024
1 parent 45b6808 commit 56c8ca1
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions tests/Feature/InvoiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use App\Models\Order;
use App\Models\Invoice;
use App\Mail\InvoiceMail;

class InvoiceTest extends TestCase
{
use RefreshDatabase;

public function testAutomaticInvoiceGeneration()
{
// Arrange: Simulate order completion
$order = Order::factory()->create();

// Act: Trigger invoice generation
$response = $this->postJson('/api/orders/'.$order->id.'/complete');

// Assert: Invoice is automatically generated with correct details
$this->assertDatabaseHas('invoices', [
'order_id' => $order->id,
]);
}

public function testInvoiceManagementInterfaceAccessibility()
{
// Act: Access the invoice management interface
$response = $this->get('/invoices');

// Assert: Interface is accessible
$response->assertStatus(200);
}

public function testCorrectnessOfDetailedInvoiceView()
{
// Arrange: Create an invoice
$invoice = Invoice::factory()->create();

// Act: View the invoice
$response = $this->get('/invoices/'.$invoice->id);

// Assert: Response contains correct invoice details
$response->assertSee($invoice->total_amount);
}

public function testSuccessfulPDFGeneration()
{
// Arrange: Create an invoice
$invoice = Invoice::factory()->create();

// Act: Request PDF generation
$response = $this->get('/invoices/'.$invoice->id.'/pdf');

// Assert: PDF is successfully generated
$response->assertHeader('Content-Type', 'application/pdf');
}

public function testEmailSendingWithInvoiceAttached()
{
// Arrange: Mock the mailer
Mail::fake();
$invoice = Invoice::factory()->create();

// Act: Trigger email sending
$this->post('/invoices/'.$invoice->id.'/send');

// Assert: Email is sent with the correct attachment
Mail::assertSent(InvoiceMail::class, function ($mail) use ($invoice) {
return $mail->hasTo($invoice->customer->email) &&
$mail->attachments->contains(function ($attachment) use ($invoice) {
return $attachment->as == "invoice-{$invoice->id}.pdf";
});
});
}
}

0 comments on commit 56c8ca1

Please sign in to comment.