From 7ca9c853690deacdfdf4d06b3cf9c8438e60caa9 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 +-- app/Models/Order.php | 13 ++++++++-- database/factories/CustomerFactory.php | 2 +- database/factories/InvoiceFactory.php | 2 +- tests/Feature/InvoiceTest.php | 33 +++++++++++++++++--------- tests/Feature/OrderTest.php | 32 +++++++++++++++---------- tests/Pest.php | 2 +- 7 files changed, 56 insertions(+), 31 deletions(-) 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/Models/Order.php b/app/Models/Order.php index 9113375..66515d7 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 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 index e0677e4..66d357b 100644 --- a/tests/Feature/InvoiceTest.php +++ b/tests/Feature/InvoiceTest.php @@ -4,20 +4,21 @@ use App\Filament\Admin\Resources\InvoiceResource\Pages\CreateInvoice; use App\Models\Customer; use App\Models\Invoice; +use App\Models\Order; use App\Models\TaxRate; use App\Models\User; -use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Foundation\Testing\DatabaseMigrations; -uses(RefreshDatabase::class); +uses(DatabaseMigrations::class); it('can create an invoice', function () { + $user = User::factory(['is_admin' => true])->create(); + $this->actingAs($user); + $customer = Customer::factory()->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(), @@ -27,13 +28,11 @@ '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 + ->fillForm($formData) + ->call('create') + ->assertHasNoErrors(); - // Step 4: Assert the invoice was successfully created in the database $this->assertDatabaseHas('invoices', [ 'internal_id' => 'INV400001', 'customer_id' => $formData['customer_id'], @@ -43,8 +42,20 @@ '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 +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 diff --git a/tests/Feature/OrderTest.php b/tests/Feature/OrderTest.php index 0cb9cac..68c0c77 100644 --- a/tests/Feature/OrderTest.php +++ b/tests/Feature/OrderTest.php @@ -15,14 +15,20 @@ use App\Models\ServiceFile; use App\Models\ServiceType; use App\Models\User; +use Illuminate\Foundation\Testing\DatabaseMigrations; +use Illuminate\Foundation\Testing\RefreshDatabase; use function Pest\Livewire\livewire; +//uses(RefreshDatabase::class); +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 +150,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 diff --git a/tests/Pest.php b/tests/Pest.php index 40d096b..24b7c5e 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -12,7 +12,7 @@ */ pest()->extend(Tests\TestCase::class) - ->use(Illuminate\Foundation\Testing\RefreshDatabase::class) +// ->use(Illuminate\Foundation\Testing\RefreshDatabase::class) ->in('Feature'); /*