|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\PurchaseOrder; |
| 4 | + |
| 5 | +use App\Filament\Resources\PurchaseOrderResource; |
| 6 | +use App\Models\Product; |
| 7 | +use App\Models\PurchaseOrder; |
| 8 | +use App\Models\PurchaseOrderItem; |
| 9 | +use Carbon\Carbon; |
| 10 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 11 | +use Livewire\Livewire; |
| 12 | +use Tests\TestCase; |
| 13 | + |
| 14 | +class CreatePurchaseOrderTest extends TestCase |
| 15 | +{ |
| 16 | + use RefreshDatabase; |
| 17 | + |
| 18 | + /** |
| 19 | + * @return void |
| 20 | + */ |
| 21 | + public function test_can_access_create_purchase_order(): void |
| 22 | + { |
| 23 | + $this->login(['create_purchase::order', 'view_any_purchase::order']); |
| 24 | + |
| 25 | + $this->get(PurchaseOrderResource::getUrl('create')) |
| 26 | + ->assertSuccessful(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function test_cant_access_create_purchase_order_with_wrong_permission(): void |
| 33 | + { |
| 34 | + $this->login(['create_something']); |
| 35 | + |
| 36 | + $this->get(PurchaseOrderResource::getUrl('create')) |
| 37 | + ->assertForbidden(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + public function test_can_create_purchase_order(): void |
| 44 | + { |
| 45 | + $user = $this->login(['create_purchase::order', 'view_any_purchase::order']); |
| 46 | + |
| 47 | + $purchaseOrder = PurchaseOrder::factory()->make([ |
| 48 | + 'company_id' => $this->company->id, |
| 49 | + 'user_id' => $user->id, |
| 50 | + 'status' => 'pending', |
| 51 | + ])->toArray(); |
| 52 | + $product = Product::factory()->create(['purchase_price' => 100]); |
| 53 | + |
| 54 | + $items = [ |
| 55 | + [ |
| 56 | + 'product_id' => $product->id, |
| 57 | + 'quantity' => 1, |
| 58 | + ], |
| 59 | + ]; |
| 60 | + |
| 61 | + Livewire::test(PurchaseOrderResource\Pages\CreatePurchaseOrder::class) |
| 62 | + ->set('data.purchaseOrderItems', null) |
| 63 | + ->fillForm(array_merge($purchaseOrder, ['purchaseOrderItems' => $items, 'paid_amount' => 1])) |
| 64 | + ->call('create') |
| 65 | + ->assertHasNoFormErrors(); |
| 66 | + |
| 67 | + $this->assertDatabaseHas('purchase_orders', [ |
| 68 | + 'company_id' => $purchaseOrder['company_id'], |
| 69 | + 'order_date' => (new Carbon($purchaseOrder['order_date']))->startOfDay()->toDateTimeString(), |
| 70 | + 'total_amount' => $product->purchase_price, |
| 71 | + 'paid_amount' => 1, |
| 72 | + ]); |
| 73 | + |
| 74 | + $purchaseOrder = PurchaseOrder::first(); |
| 75 | + $this->assertDatabaseHas('purchase_order_items', [ |
| 76 | + 'purchase_order_id' => $purchaseOrder->id, |
| 77 | + 'sku' => $product->sku, |
| 78 | + 'product_id' => $product->id, |
| 79 | + ]); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + public function test_cant_create_purchase_order_with_wrong_permission(): void |
| 86 | + { |
| 87 | + $this->login(['create_something']); |
| 88 | + |
| 89 | + Livewire::test(PurchaseOrderResource\Pages\CreatePurchaseOrder::class) |
| 90 | + ->assertForbidden(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + public function test_can_create_goods_receipt_when_purchase_order_is_pending(): void |
| 97 | + { |
| 98 | + $user = $this->login(['create_purchase::order', 'view_any_purchase::order']); |
| 99 | + |
| 100 | + $purchaseOrder = PurchaseOrder::factory()->create([ |
| 101 | + 'company_id' => $this->company->id, |
| 102 | + 'user_id' => $user->id, |
| 103 | + 'status' => 'pending', |
| 104 | + ]); |
| 105 | + |
| 106 | + $product = Product::factory()->create(['purchase_price' => 100]); |
| 107 | + PurchaseOrderItem::factory()->create([ |
| 108 | + 'company_id' => $product->id, |
| 109 | + 'purchase_order_id' => $purchaseOrder->id, |
| 110 | + 'product_id' => $product->id, |
| 111 | + 'quantity' => 1, |
| 112 | + 'unit_cost' => 1, |
| 113 | + 'sku' => $product->sku, |
| 114 | + 'name' => $product->name, |
| 115 | + ]); |
| 116 | + |
| 117 | + Livewire::test(PurchaseOrderResource\RelationManagers\GoodsReceiptsRelationManager::class, [ |
| 118 | + 'ownerRecord' => $purchaseOrder, |
| 119 | + 'pageClass' => PurchaseOrderResource\Pages\ViewPurchaseOrder::class, |
| 120 | + ])->callTableAction('create', null, [ |
| 121 | + 'received_date' => now(), |
| 122 | + 'product_id' => $product->id, |
| 123 | + 'quantity' => 1, |
| 124 | + ]) |
| 125 | + ->assertHasNoTableActionErrors(); |
| 126 | + |
| 127 | + $this->assertDatabaseHas('goods_receipts', [ |
| 128 | + 'purchase_order_id' => $purchaseOrder->id, |
| 129 | + 'user_id' => $purchaseOrder->user_id, |
| 130 | + 'product_id' => $product->id, |
| 131 | + 'sku' => $product->sku, |
| 132 | + 'name' => $product->name, |
| 133 | + 'quantity' => 1, |
| 134 | + 'unit_cost' => $product->purchase_price, |
| 135 | + ]); |
| 136 | + |
| 137 | + $this->assertDatabaseHas('purchase_orders', [ |
| 138 | + 'id' => $purchaseOrder->id, |
| 139 | + 'status' => 'partially_received', |
| 140 | + ]); |
| 141 | + } |
| 142 | +} |
0 commit comments