Files
WMS/tests/Browser/Concerns/InteractsWithWmsData.php
T
2026-09-21 10:14:57 +01:00

273 lines
8.8 KiB
PHP
Executable File

<?php
namespace Tests\Browser\Concerns;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Schema;
use Modules\Company\Models\Company;
use Modules\Platform\app\Models\LicenseKey;
use Modules\Platform\app\Models\Payment;
use Modules\Platform\app\Models\PlatformPrice;
use Modules\Warehouse\app\Models\Warehouse;
use Modules\Warehouse\app\Models\WarehouseValidationRule;
trait InteractsWithWmsData
{
protected string $browserPassword = '987400Kmh';
/**
* Create or update the MPBA platform administrator used by browser tests.
*
* @param array<string, mixed> $overrides
*/
protected function platformAdminUser(array $overrides = []): User
{
$company = Company::query()->updateOrCreate(
['slug' => 'mpba-limited'],
[
'name' => 'MPBA Limited',
'status' => 'active',
'timezone' => 'Europe/London',
'is_system_company' => true,
'billing_email' => 'mark.gregory@mpba.co.uk',
],
);
$email = $overrides['email'] ?? 'platform.admin@example.test';
unset($overrides['email']);
return User::query()->updateOrCreate(
['email' => $email],
[
'company_id' => $company->id,
'name' => $overrides['name'] ?? 'Platform Admin',
'password' => Hash::make($this->browserPassword),
'email_verified_at' => now(),
'is_platform_admin' => true,
'is_company_admin' => true,
'status' => 'active',
...$overrides,
],
);
}
/**
* @param array<string, mixed> $overrides
*/
protected function tenantCompany(array $overrides = []): Company
{
$slug = $overrides['slug'] ?? 'test-company';
unset($overrides['slug']);
$company = Company::query()->updateOrCreate(
['slug' => $slug],
[
'name' => $overrides['name'] ?? 'Test Company',
'status' => $overrides['status'] ?? 'active',
'timezone' => 'Europe/London',
'is_system_company' => false,
'billing_email' => $overrides['billing_email'] ?? 'billing@test-company.example',
...$overrides,
],
);
LicenseKey::query()->where('company_id', $company->id)->delete();
LicenseKey::issueFor($company, 'Browser Test Licence', 'enterprise', 250);
return $company->refresh();
}
/**
* @param array<string, mixed> $overrides
*/
protected function tenantUser(array $overrides = []): User
{
$company = $overrides['company'] ?? $this->tenantCompany();
unset($overrides['company']);
$email = $overrides['email'] ?? 'tenant.user@example.test';
unset($overrides['email']);
return User::query()->updateOrCreate(
['email' => $email],
[
'company_id' => $company->id,
'name' => $overrides['name'] ?? 'Tenant User',
'password' => Hash::make($this->browserPassword),
'email_verified_at' => now(),
'is_platform_admin' => false,
'is_company_admin' => true,
'status' => 'active',
...$overrides,
],
);
}
protected function seedPlatformPrices(): void
{
foreach ([
['basic', 'Basic', 'Starter WMS access', 'Core warehouse management for small teams.', 4900, 49000, 10, false, true, ['2 warehouses', '10 users', 'Standard support']],
['advanced', 'Advanced', 'Growing WMS operations', 'More users, warehouses and automation controls.', 14900, 149000, 20, true, true, ['5 warehouses', '100 users', 'Advanced support']],
['pro', 'Pro', 'Professional WMS command centre', 'Full operational visibility for larger teams.', 29900, 299000, 30, false, true, ['15 warehouses', '1000 users', 'Priority support']],
] as [$code, $name, $strapline, $description, $monthly, $annual, $sortOrder, $featured, $active, $features]) {
PlatformPrice::query()->updateOrCreate(
['code' => $code],
$this->platformPriceAttributes($name, $strapline, $description, $monthly, $annual, $sortOrder, $featured, $active, $features),
);
}
}
protected function seedWarehouse(?Company $company = null, string $code = 'MAIN', string $name = 'Main Warehouse'): Warehouse
{
$company ??= $this->tenantCompany();
$warehouse = Warehouse::query()->updateOrCreate(
[
'company_id' => $company->id,
'code' => $code,
],
$this->warehouseAttributes($name),
);
$this->seedWarehouseValidationRule($warehouse, 'Require batch number', 'batch-required');
return $warehouse->refresh();
}
protected function seedPlatformDashboardData(): void
{
$company = $this->tenantCompany([
'name' => 'Customer One',
'slug' => 'customer-one',
]);
$this->seedWarehouse($company);
$this->seedPlatformPrices();
Payment::query()->updateOrCreate(
['stripe_payment_intent_id' => 'pi_browser_paid'],
[
'company_id' => $company->id,
'amount' => 12345,
'currency' => 'gbp',
'status' => 'paid',
'description' => 'Browser test payment',
'paid_at' => now(),
'metadata' => ['source' => 'dusk'],
],
);
}
/**
* @param array<int, string> $features
*
* @return array<string, mixed>
*/
private function platformPriceAttributes(
string $name,
string $strapline,
string $description,
int $monthly,
int $annual,
int $sortOrder,
bool $featured,
bool $active,
array $features,
): array {
$attributes = [
'name' => $name,
'strapline' => $strapline,
'description' => $description,
'monthly_amount' => $monthly,
'currency' => 'gbp',
'badge_label' => $featured ? 'Popular' : null,
'features' => $features,
'sort_order' => $sortOrder,
'is_featured' => $featured,
];
if (Schema::hasColumn('platform_prices', 'annual_amount')) {
$attributes['annual_amount'] = $annual;
}
if (Schema::hasColumn('platform_prices', 'yearly_amount')) {
$attributes['yearly_amount'] = $annual;
}
if (Schema::hasColumn('platform_prices', 'is_active')) {
$attributes['is_active'] = $active;
}
if (Schema::hasColumn('platform_prices', 'active')) {
$attributes['active'] = $active;
}
return $attributes;
}
/**
* @return array<string, mixed>
*/
private function warehouseAttributes(string $name): array
{
$attributes = [
'name' => $name,
'timezone' => 'Europe/London',
];
if (Schema::hasColumn('warehouses', 'address')) {
$attributes['address'] = 'Main Road';
}
if (Schema::hasColumn('warehouses', 'status')) {
$attributes['status'] = 'active';
}
return $attributes;
}
private function seedWarehouseValidationRule(Warehouse $warehouse, string $name, string $key): void
{
$attributes = [
'warehouse_id' => $warehouse->id,
];
if (Schema::hasColumn('warehouse_validation_rules', 'company_id')) {
$attributes['company_id'] = $warehouse->company_id;
}
if (Schema::hasColumn('warehouse_validation_rules', 'rule_key')) {
$attributes['rule_key'] = $key;
}
if (Schema::hasColumn('warehouse_validation_rules', 'key')) {
$attributes['key'] = $key;
}
$values = ['name' => $name];
if (Schema::hasColumn('warehouse_validation_rules', 'severity')) {
$values['severity'] = 'error';
}
if (Schema::hasColumn('warehouse_validation_rules', 'rule_type')) {
$values['rule_type'] = 'validation';
}
if (Schema::hasColumn('warehouse_validation_rules', 'value')) {
$values['value'] = 'required';
}
if (Schema::hasColumn('warehouse_validation_rules', 'configuration')) {
$values['configuration'] = ['field' => 'batch_number'];
}
if (Schema::hasColumn('warehouse_validation_rules', 'is_active')) {
$values['is_active'] = true;
}
WarehouseValidationRule::query()->updateOrCreate($attributes, $values);
}
}