add more controllers and fix tests
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\PurchaseOrders;
|
||||
|
||||
use App\Services\PurchaseOrders\PurchaseOrderCreateService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PurchaseOrderCreateServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_create_persists_purchase_order_and_items(): void
|
||||
{
|
||||
$supplierId = DB::table('suppliers')->insertGetId(['name' => 'Alpha Supplies']);
|
||||
$supplyId = DB::table('supplies')->insertGetId(['name' => 'Paper', 'unit' => 'box', 'qty_on_hand' => 0]);
|
||||
|
||||
$service = new PurchaseOrderCreateService();
|
||||
$po = $service->create([
|
||||
'po_number' => 'PO-500',
|
||||
'supplier_id' => $supplierId,
|
||||
'order_date' => '2026-03-10',
|
||||
'expected_date' => '2026-03-15',
|
||||
'notes' => 'Initial order',
|
||||
'items' => [
|
||||
[
|
||||
'supply_id' => $supplyId,
|
||||
'description' => 'Paper for class',
|
||||
'quantity' => 3,
|
||||
'unit_cost' => 4,
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertNotNull($po->id);
|
||||
$this->assertDatabaseHas('purchase_orders', [
|
||||
'id' => $po->id,
|
||||
'po_number' => 'PO-500',
|
||||
'subtotal' => 12,
|
||||
'total' => 12,
|
||||
]);
|
||||
$this->assertDatabaseHas('purchase_order_items', [
|
||||
'purchase_order_id' => $po->id,
|
||||
'supply_id' => $supplyId,
|
||||
'quantity' => 3,
|
||||
'unit_cost' => 4,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\PurchaseOrders;
|
||||
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Models\PurchaseOrderItem;
|
||||
use App\Models\Supply;
|
||||
use App\Services\PurchaseOrders\PurchaseOrderReceiveService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PurchaseOrderReceiveServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_receive_updates_items_and_inventory(): void
|
||||
{
|
||||
$supply = Supply::query()->create([
|
||||
'name' => 'Paper',
|
||||
'unit' => 'box',
|
||||
'qty_on_hand' => 2,
|
||||
]);
|
||||
|
||||
$po = PurchaseOrder::query()->create([
|
||||
'po_number' => 'PO-600',
|
||||
'supplier_id' => 1,
|
||||
'status' => PurchaseOrder::STATUS_ORDERED,
|
||||
'subtotal' => 10,
|
||||
'tax' => 0,
|
||||
'total' => 10,
|
||||
]);
|
||||
|
||||
$item = PurchaseOrderItem::query()->create([
|
||||
'purchase_order_id' => $po->id,
|
||||
'supply_id' => $supply->id,
|
||||
'description' => 'Paper for class',
|
||||
'quantity' => 3,
|
||||
'received_qty' => 0,
|
||||
'unit_cost' => 3,
|
||||
]);
|
||||
|
||||
$service = new PurchaseOrderReceiveService();
|
||||
$result = $service->receive($po->id, [
|
||||
['id' => $item->id, 'quantity' => 3],
|
||||
], 'tester@example.com');
|
||||
|
||||
$this->assertTrue($result['completed']);
|
||||
$this->assertDatabaseHas('purchase_order_items', [
|
||||
'id' => $item->id,
|
||||
'received_qty' => 3,
|
||||
]);
|
||||
$this->assertDatabaseHas('supplies', [
|
||||
'id' => $supply->id,
|
||||
'qty_on_hand' => 5,
|
||||
]);
|
||||
$this->assertDatabaseHas('supply_transactions', [
|
||||
'supply_id' => $supply->id,
|
||||
'type' => 'in',
|
||||
'quantity' => 3,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\PurchaseOrders;
|
||||
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Services\PurchaseOrders\PurchaseOrderStatusService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PurchaseOrderStatusServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_cancel_marks_purchase_order_canceled(): void
|
||||
{
|
||||
$po = PurchaseOrder::query()->create([
|
||||
'po_number' => 'PO-700',
|
||||
'supplier_id' => 1,
|
||||
'status' => PurchaseOrder::STATUS_ORDERED,
|
||||
'subtotal' => 0,
|
||||
'tax' => 0,
|
||||
'total' => 0,
|
||||
]);
|
||||
|
||||
$service = new PurchaseOrderStatusService();
|
||||
$updated = $service->cancel($po->id);
|
||||
|
||||
$this->assertSame(PurchaseOrder::STATUS_CANCELED, $updated->status);
|
||||
$this->assertDatabaseHas('purchase_orders', [
|
||||
'id' => $po->id,
|
||||
'status' => PurchaseOrder::STATUS_CANCELED,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user