Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/Http/Controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,11 @@ public function update(Request $request, Order $order)
$totalCommission = 0;

// 4. Process NEW Items
$variantIds = collect($validated['items'])->pluck('id');
$variants = ProductVariant::with('product')->whereIn('id', $variantIds)->get()->keyBy('id');

foreach ($validated['items'] as $itemData) {
$variant = ProductVariant::with('product')->find($itemData['id']);
$variant = $variants->get($itemData['id']);

// Stock Check
if ($variant->quantity < $itemData['quantity']) {
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</source>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:5ySMp2mTack7W9o1mEJ6d3GgGkTmKQF+EsWOruzXp40="/>
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="BROADCAST_CONNECTION" value="null"/>
Expand Down
120 changes: 120 additions & 0 deletions tests/Feature/OrderUpdatePerformanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Models\User;
use App\Models\Category;
use App\Models\Product;
use App\Models\ProductVariant;
use App\Models\Order;
use App\Models\Unit;
use App\Models\Customer;
use Illuminate\Support\Facades\DB;

class OrderUpdatePerformanceTest extends TestCase
{
use RefreshDatabase;

public function test_order_update_performance()
{
// 1. Setup Data
$user = User::factory()->create();

$category = Category::create(['name' => 'Test Category']);
$unit = Unit::create(['name' => 'Test Unit', 'short_name' => 'tu']);

$product = Product::create([
'name' => 'Test Product',
'sku' => 'TP-001',
'category_id' => $category->id,
'unit_id' => $unit->id,
'selling_price' => 100,
'quantity' => 1000,
'alert_quantity' => 10,
]);

$variants = [];
for ($i = 0; $i < 10; $i++) {
$variants[] = ProductVariant::create([
'product_id' => $product->id,
'unit_id' => $unit->id,
'sku' => 'TP-001-V' . $i,
'selling_price' => 100,
'limit_price' => 80,
'quantity' => 100, // Initial quantity
'alert_quantity' => 10,
]);
}

$customer = Customer::create([
'name' => 'Test Customer',
'mobile' => '1234567890',
'address' => 'Test Address',
'city' => 'Test City',
'country' => 'Sri Lanka',
]);

$order = Order::forceCreate([
'order_number' => 'ORD-001',
'user_id' => $user->id,
'customer_id' => $customer->id,
'order_date' => now(),
'customer_name' => 'Test Customer',
'customer_phone' => '1234567890',
'customer_address' => 'Test Address',
'status' => 'pending',
'order_type' => 'direct',
'total_amount' => 0,
]);

// 2. Prepare Payload
$items = [];
foreach ($variants as $variant) {
$items[] = [
'id' => $variant->id,
'quantity' => 2,
'selling_price' => 100,
];
}

$payload = [
'order_type' => 'direct',
'order_date' => now()->format('Y-m-d'),
'customer' => [
'name' => 'Test Customer',
'mobile' => '1234567890',
'address' => 'Test Address',
'city' => 'Test City',
],
'items' => $items,
'payment_method' => 'cod',
'call_status' => 'pending',
];

// 3. Measure Queries
DB::flushQueryLog();
DB::enableQueryLog();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Disable query logging after performance assertion

DB::enableQueryLog() is turned on for this test but never turned off, so the same connection can keep logging queries for later tests in the same PHPUnit process. That can silently accumulate large query logs in memory and slow or destabilize unrelated tests; this test should disable logging after reading DB::getQueryLog() (or use a scoped listener) to avoid cross-test side effects.

Useful? React with πŸ‘Β / πŸ‘Ž.


$response = $this->actingAs($user)->putJson(route('orders.update', $order->id), $payload);

$response->assertStatus(200);

$queryLog = DB::getQueryLog();
$queryCount = count($queryLog);


// Assert query count is significantly reduced (e.g. < 45)
// Baseline was 66.
$this->assertLessThan(45, $queryCount, "Query count should be optimized (was 66, expected < 45)");

// Assert stock decrement
foreach ($variants as $variant) {
$this->assertDatabaseHas('product_variants', [
'id' => $variant->id,
'quantity' => 98, // 100 - 2
]);
}
}
}