Files
hub/tests/Feature/Models/OrderItemBatchTest.php
Jon Leopard 5c1863218f fix: optimize test suite performance with DatabaseTransactions
This commit completes the PR #53 integration by optimizing the test suite:

Performance Improvements:
- Migrated 25 test files from RefreshDatabase to DatabaseTransactions
- Tests now run in 12.69s parallel (previously 30s+)
- Increased PostgreSQL max_locks_per_transaction to 256 for parallel testing

Test Infrastructure Changes:
- Disabled broadcasting in tests (set to null) to avoid Reverb connectivity issues
- Reverted 5 integration tests to RefreshDatabase (CheckoutFlowTest + 4 Service tests)
  that require full schema recreation due to complex fixtures

PR #53 Integration Fixes:
- Added Product.inStock() scope for inventory queries
- Fixed ProductFactory to create InventoryItem records instead of using removed columns
- Added Department.products() relationship
- Fixed FulfillmentWorkOrderController view variables
- Fixed orders migration location_id foreign key constraint
- Created seller-layout component wrapper

All 146 tests now pass with optimal performance.
2025-11-17 20:52:50 -07:00

139 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Models;
use App\Models\Order;
use App\Models\OrderItem;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class OrderItemBatchTest extends TestCase
{
use DatabaseTransactions;
protected function setUp(): void
{
parent::setUp();
// Skip all tests if batches table doesn't exist
if (! Schema::hasTable('batches')) {
$this->markTestSkipped('Batches table does not exist. This feature requires the labs-batch-qr-codes worktree.');
}
// Skip all tests if Batch model doesn't exist
if (! class_exists(\App\Models\Batch::class)) {
$this->markTestSkipped('Batch model does not exist. This feature requires the labs-batch-qr-codes worktree.');
}
}
public function test_order_item_has_batch_id_column(): void
{
$this->assertTrue(
Schema::hasColumn('order_items', 'batch_id'),
'order_items table should have batch_id column'
);
}
public function test_order_item_batch_id_is_nullable(): void
{
// Create order item without batch_id
$orderItem = OrderItem::factory()->create([
'batch_id' => null,
]);
$this->assertNull($orderItem->batch_id);
$this->assertNull($orderItem->fresh()->batch_id);
}
public function test_order_item_can_have_batch_id(): void
{
// Create a batch using the Batch model
$batchClass = \App\Models\Batch::class;
$batch = $batchClass::factory()->create();
// Create order item with batch_id
$orderItem = OrderItem::factory()->create([
'batch_id' => $batch->id,
]);
$this->assertEquals($batch->id, $orderItem->batch_id);
$this->assertEquals($batch->id, $orderItem->fresh()->batch_id);
}
public function test_order_item_has_batch_relationship(): void
{
// Create a batch
$batchClass = \App\Models\Batch::class;
$batch = $batchClass::factory()->create([
'batch_number' => 'TEST-BATCH-001',
]);
// Create order item with batch
$orderItem = OrderItem::factory()->create([
'batch_id' => $batch->id,
]);
// Test relationship exists
$this->assertTrue(method_exists($orderItem, 'batch'));
// Test relationship returns correct batch
$this->assertNotNull($orderItem->batch);
$this->assertEquals($batch->id, $orderItem->batch->id);
$this->assertEquals('TEST-BATCH-001', $orderItem->batch->batch_number);
}
public function test_order_item_batch_relationship_returns_null_when_no_batch(): void
{
// Create order item without batch
$orderItem = OrderItem::factory()->create([
'batch_id' => null,
]);
// Test relationship returns null
$this->assertNull($orderItem->batch);
}
public function test_batch_id_foreign_key_sets_null_on_batch_deletion(): void
{
// Create a batch
$batchClass = \App\Models\Batch::class;
$batch = $batchClass::factory()->create();
// Create order item with batch
$orderItem = OrderItem::factory()->create([
'batch_id' => $batch->id,
]);
$this->assertEquals($batch->id, $orderItem->batch_id);
// Delete the batch (soft delete)
$batch->delete();
// Check that batch_id is still set (soft delete doesn't trigger onDelete)
$this->assertEquals($batch->id, $orderItem->fresh()->batch_id);
// Force delete the batch
$batch->forceDelete();
// Now batch_id should be null due to nullOnDelete()
$this->assertNull($orderItem->fresh()->batch_id);
}
public function test_order_item_has_batch_id_index(): void
{
$indexes = Schema::getIndexes('order_items');
$hasBatchIdIndex = collect($indexes)->contains(function ($index) {
return in_array('batch_id', $index['columns']);
});
$this->assertTrue(
$hasBatchIdIndex,
'order_items table should have an index on batch_id column'
);
}
}