From 8104eacc793487e2b49751adaf70b50bb64cb749 Mon Sep 17 00:00:00 2001 From: Nisse Lommerde Date: Sun, 19 Jan 2025 12:51:37 -0500 Subject: [PATCH] Work on tests --- app/Enums/IconEnum.php | 3 +- .../Admin/Resources/OrderResource.php | 4 + app/Models/Invoice.php | 10 +- app/Models/Order.php | 17 +++- database/factories/CustomerFactory.php | 2 +- database/factories/InvoiceFactory.php | 2 +- tests/Feature/InvoiceTest.php | 50 ---------- tests/Pest.php | 19 +++- tests/Unit/InvoiceTest.php | 92 +++++++++++++++++++ tests/{Feature => Unit}/OrderTest.php | 29 +++--- 10 files changed, 146 insertions(+), 82 deletions(-) delete mode 100644 tests/Feature/InvoiceTest.php create mode 100644 tests/Unit/InvoiceTest.php rename tests/{Feature => Unit}/OrderTest.php (94%) diff --git a/app/Enums/IconEnum.php b/app/Enums/IconEnum.php index 562987e..2d65181 100644 --- a/app/Enums/IconEnum.php +++ b/app/Enums/IconEnum.php @@ -20,6 +20,5 @@ enum IconEnum: string case TAB_ALL = 'lucide-layout-grid'; case TAB_OVERDUE = 'lucide-calendar-clock'; - case TAB_UNPRINTED = ' lucide-printer'; - + case TAB_UNPRINTED = 'lucide-printer'; } diff --git a/app/Filament/Admin/Resources/OrderResource.php b/app/Filament/Admin/Resources/OrderResource.php index 8831c13..b8a7a75 100644 --- a/app/Filament/Admin/Resources/OrderResource.php +++ b/app/Filament/Admin/Resources/OrderResource.php @@ -125,6 +125,10 @@ public static function form(Form $form): Form Section::make() ->schema([ + Placeholder::make('ID') + ->label('Order ID') + ->content(fn (Order $record): ?string => $record->internal_po), + Placeholder::make('created_at') ->label('Created') ->content(fn (Order $record): ?string => $record->created_at?->diffForHumans()), diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 0cc0d56..d19a78d 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -39,12 +39,10 @@ class Invoice extends Model ]; protected $casts = [ - 'has_gst' => 'boolean', - 'has_pst' => 'boolean', - 'date' => 'datetime', - 'total' => 'decimal:2', - 'subtotal' => 'decimal:2', - 'status' => InvoiceStatus::class, + 'has_gst' => 'boolean', + 'has_pst' => 'boolean', + 'date' => 'datetime', + 'status' => InvoiceStatus::class, ]; public function calculateTotals(): void diff --git a/app/Models/Order.php b/app/Models/Order.php index 9113375..22e5e55 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -50,8 +50,17 @@ class Order extends Model ]; protected $casts = [ - 'status' => OrderStatus::class, - 'order_type' => OrderType::class, + 'status' => OrderStatus::class, + 'order_type' => OrderType::class, + 'rush' => 'bool', + 'repeat' => 'bool', + 'new_art' => 'bool', + 'event' => 'bool', + 'digitizing' => 'bool', + 'garments' => 'bool', + 'supplied_file' => 'bool', + 'printed' => 'bool', + 'pre_production' => 'bool', ]; public static function boot(): void @@ -109,13 +118,13 @@ public function getTotalProductQuantityAttribute(): int public function getTotalServicePriceAttribute(): float { - $total = 0; + $total = 0.00; foreach ($this->productServices as $service) { $total += $service->amount * $service->amount_price; } - return number_format($total, 2); + return $total; } public function active(): bool diff --git a/database/factories/CustomerFactory.php b/database/factories/CustomerFactory.php index f0c312a..460686d 100644 --- a/database/factories/CustomerFactory.php +++ b/database/factories/CustomerFactory.php @@ -10,7 +10,7 @@ class CustomerFactory extends Factory { protected $model = Customer::class; - public function definition() + public function definition(): array { $company_name = $this->faker->company(); $internal_name = explode(',', $company_name); diff --git a/database/factories/InvoiceFactory.php b/database/factories/InvoiceFactory.php index bfe1149..341cbfa 100644 --- a/database/factories/InvoiceFactory.php +++ b/database/factories/InvoiceFactory.php @@ -15,7 +15,7 @@ class InvoiceFactory extends Factory public function definition(): array { - $customer = Customer::all()->shuffle()->first(); + $customer = Customer::all()->shuffle()->firstOrFail(); return [ 'created_at' => Carbon::now()->subDays(rand(1, 30)), diff --git a/tests/Feature/InvoiceTest.php b/tests/Feature/InvoiceTest.php deleted file mode 100644 index e0677e4..0000000 --- a/tests/Feature/InvoiceTest.php +++ /dev/null @@ -1,50 +0,0 @@ -create(); // Generates a customer - $user = User::factory(['is_admin' => true])->create(); - $pst_rate = TaxRate::where('name', 'PST')->value('value') ?? 0; - $gst_rate = TaxRate::where('name', 'GST')->value('value') ?? 0; - - $this->actingAs($user); - - $formData = [ - 'customer_id' => $customer->id, - 'date' => now()->toDateString(), - 'due_date' => now()->addDays(7)->toDateString(), - 'status' => InvoiceStatus::UNPAID->value, - 'has_gst' => true, - 'has_pst' => true, - ]; - - // Step 3: Submit the form and create an invoice - $this->livewire(CreateInvoice::class) - ->fillForm($formData) // Simulates filling the form - ->call('create') // Submits the form - ->assertHasNoErrors(); // Verifies no validation errors occurred - - // Step 4: Assert the invoice was successfully created in the database - $this->assertDatabaseHas('invoices', [ - 'internal_id' => 'INV400001', - 'customer_id' => $formData['customer_id'], - 'status' => $formData['status'], - 'has_gst' => $formData['has_gst'], - 'has_pst' => $formData['has_pst'], - 'gst_rate' => $gst_rate, - 'pst_rate' => $pst_rate, - ]); -}); - -// it can add orders - -// it correctly calculates tax diff --git a/tests/Pest.php b/tests/Pest.php index 40d096b..96daa30 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -11,9 +11,18 @@ | */ +use Illuminate\Foundation\Testing\RefreshDatabase; + pest()->extend(Tests\TestCase::class) - ->use(Illuminate\Foundation\Testing\RefreshDatabase::class) - ->in('Feature'); +// ->use(Illuminate\Foundation\Testing\DatabaseMigrations::class) +// ->use(RefreshDatabase::class) + ->beforeEach(function () { + DB::beginTransaction(); + }) + ->afterEach(function () { + DB::rollBack(); + }) + ->in('Unit'); /* |-------------------------------------------------------------------------- @@ -26,9 +35,9 @@ | */ -expect()->extend('toBeOne', function () { - return $this->toBe(1); -}); +//expect()->extend('toBeOne', function () { +// return $this->toBe(1); +//}); /* |-------------------------------------------------------------------------- diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php new file mode 100644 index 0000000..ab83493 --- /dev/null +++ b/tests/Unit/InvoiceTest.php @@ -0,0 +1,92 @@ + true])->create(); + $this->actingAs($user); + + $customer = Customer::factory()->create(); // Generates a customer + $pst_rate = TaxRate::where('name', 'PST')->value('value') ?? 0; + $gst_rate = TaxRate::where('name', 'GST')->value('value') ?? 0; + + $formData = [ + 'customer_id' => $customer->id, + 'date' => now()->toDateString(), + 'due_date' => now()->addDays(7)->toDateString(), + 'status' => InvoiceStatus::UNPAID->value, + 'has_gst' => true, + 'has_pst' => true, + ]; + + $this->livewire(CreateInvoice::class) + ->fillForm($formData) + ->call('create') + ->assertHasNoErrors(); + + $this->assertDatabaseHas('invoices', [ + 'internal_id' => 'INV400001', + 'customer_id' => $formData['customer_id'], + 'status' => $formData['status'], + 'has_gst' => $formData['has_gst'], + 'has_pst' => $formData['has_pst'], + 'gst_rate' => $gst_rate, + 'pst_rate' => $pst_rate, + ]); + + $invoice = Invoice::where('internal_id', 'INV400001')->firstOrFail(); + + $this->assertEquals($invoice->orders->isEmpty(), true); +}); + +it('can add orders to an invoice', function () { + $customer = Customer::factory()->create(); + $invoice = Invoice::factory()->create(['customer_id' => $customer->id]); + $orders = Order::factory()->for($customer)->count(3)->create(); + + $invoice->orders()->saveMany($orders); + + $this->assertEquals($invoice->orders->count(), 3); +}); + +it('correctly calculates tax amounts', function () { + $customer = Customer::factory()->create(); + $order = Order::factory()->for($customer)->create(); + $services = ProductService::factory()->count(3)->create(['order_id' => $order->id]); + + $invoice = Invoice::factory(['has_gst' => true, 'has_pst' => true])->create(['customer_id' => $customer->id]); + $invoice->orders()->save($order); + + $invoice->calculateTotals(); //FIXME this shouldn't be necessary + + $subtotal = $order->total_service_price; + $pst_amount = $subtotal * $invoice->pst_rate / 100; + $gst_amount = $subtotal * $invoice->gst_rate / 100; + $total = $subtotal + $pst_amount + $gst_amount; + + $this->assertSame($invoice->subtotal, $subtotal); + $this->assertSame($invoice->gst_amount, $gst_amount); + $this->assertSame($invoice->pst_amount, $pst_amount); + $this->assertSame($invoice->total, $total); +}); +it('correctly re-calculates tax amounts when an order gets added', function () {}); + +it('correctly re-calculates tax amounts when an order gets removed', function () {}); + +it('correctly re-calculates tax amounts when an order\'s product services get added', function () {}); + +it('correctly re-calculates tax amounts when an order\'s product service\'s quantity gets changed', function () {}); + +it('correctly re-calculates tax amounts when an order\'s product service\'s amount gets changed', function () {}); + +it('correctly re-calculates tax amounts when an order\'s product services get removed', function () {}); diff --git a/tests/Feature/OrderTest.php b/tests/Unit/OrderTest.php similarity index 94% rename from tests/Feature/OrderTest.php rename to tests/Unit/OrderTest.php index 0cb9cac..e0fb37a 100644 --- a/tests/Feature/OrderTest.php +++ b/tests/Unit/OrderTest.php @@ -15,14 +15,17 @@ use App\Models\ServiceFile; use App\Models\ServiceType; use App\Models\User; +use Illuminate\Foundation\Testing\DatabaseMigrations; use function Pest\Livewire\livewire; +uses(DatabaseMigrations::class); + it('can create an order and all associated models and save it to the database', function () { - $type = fake()->randomElement(OrderType::cases()); - $status = fake()->randomElement(OrderStatus::cases()); - $customer = Customer::factory()->create(); - $contact = Contact::factory()->for($customer)->create(); + $type = fake()->randomElement(OrderType::cases()); + $status = fake()->randomElement(OrderStatus::cases()); + $customer = Customer::factory()->create(); + // $contact = Contact::factory()->for($customer)->create(); $attributes = array_map(fn ($case) => $case->value, OrderAttributes::cases()); $orderDate = today(); $dueDate = today()->addDays(10); @@ -144,15 +147,15 @@ $status, $dueDate->format('Y-m-d'), 'Notes go here! Here\'s the notes!', - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, + true, + true, + true, + true, + true, + true, + true, + true, + true, ]); // Order Products