Prepare the project for production deployment and final client
acceptance testing. Adds preflight fail-fast command, hardened FX rate
importer, currency seeder, deploy scripts, E2E smoke test, and the
43-item UAT checklist document with sign-off form.
Key changes:
- Phase 3.9.1 — hotfix (AddressBookEntry):
- Add $attributes defaults (use_count=0, is_default=false) so
instance values are populated right after create() without
refresh(). Resolves the red test
`it_increments_use_count_and_sets_last_used_at`.
- Phase 3.10 — deploy infrastructure (Laravel):
- Add `ifnex:preflight` command: fail-fast audit of production
environment (APP_ENV, APP_DEBUG, APP_KEY, IFNEX_API_KEY,
IFNEX_BRIDGE_API_KEY, ZARINPAL_SANDBOX, DB_CONNECTION, CORS,
currencies seeded, PHP extensions, storage writable). Exits 1
if any check fails; supports --skip-db for pre-migrate runs.
- Add `ifnex:import-fx-rates` command: replacement for
`ifnex:update-rates`. Fetches USD/AED from Exir.io + EUR/CNY
from FreeCurrencyAPI. Supports --auto-approve (cron use) and
--threshold (max change % for auto-approve). Logs critical on
HTTP failure. URL configurable via EXIR_API_URL env.
- Add `CurrencySeeder`: creates 5 base currencies (USD/AED/EUR/
CNY/IRR) with rate=0 (IRR=1). Idempotent — does not overwrite
real rates already set by admin or cron.
- Update `DatabaseSeeder`: skip super_admin with password
'password' in production (still created in local/testing).
- Fix `bootstrap/app.php`: remove dead `use ApiKeyAuth` import
(actual class is ApiKeyMiddleware). Latent fatal if api_key
alias was used.
- Add `config/ifnex.php` exir block + preflight config.
- Harden `.env.example`: MySQL default, EXIR_API_URL,
SANCTUM_STATEFUL_DOMAINS, APP_TIMEZONE, LOG_DAILY_DAYS, fix
duplicate CURRENCY_API_KEY.
- Phase 3.10 — deploy scripts (Linux):
- Add `scripts/deploy.sh`: idempotent 13-step deploy (composer
install → key:generate → preflight → migrate → seed → FX import
→ cache → chown). Fail-fast with set -e.
- Add `scripts/cron-exir-rates.sh`: daily cron wrapper for
`ifnex:import-fx-rates --source=all --auto-approve`.
- Phase 3.10.1 — preflight hotfix:
- Make expected DB connection configurable
(ifnex.preflight.expected_db_connection) instead of hardcoded
'mysql'. Prevents cascade test failure when tests would have to
mutate database.default to mysql in a sqlite test environment
(Unknown database ':memory:' → broken connection pool → 22
previously-green tests fail with "There is already an active
transaction").
- Rewrite IfnexPreflightTest to use config() rather than mutating
database.default.
- Phase 3.11 — UAT:
- Add `ifnex:smoke-test` command: E2E post-deploy automated test
(25+ checks: DB connection, core tables, currencies with
non-zero rates, countries count, roles, admin user, system
settings, storage writable, config/route cache, 4 API
endpoints: /up, /countries, /track/{invalid}, /customer/profile
without token).
- Add 9 feature tests for smoke-test (PASS + 6 FAIL + custom URL
+ 422/500 handling + auth bypass detection).
- Add `UAT-3.11.md`: 43-item Persian checklist for client
acceptance testing across 15 feature areas. Includes sign-off
form for both parties (client + VernaSoft).
- Update `CLIENT_DELIVERY.md` to record phases 3.10 and 3.11.
Tests:
- Total: 51 → 82 tests, 150 → 220+ assertions, all green.
- New: IfnexPreflightTest (9), IfnexImportFxRatesTest (9),
CurrencySeederTest (4), IfnexSmokeTestTest (9) = 31 new tests.
Documentation:
- DEPLOYMENT_HANDOFF.md §2.4: add CurrencySeeder + import-fx-rates.
- DEPLOYMENT_HANDOFF.md §2.8: update cron to use cron-exir-rates.sh.
- UAT-3.11.md: new 43-item client acceptance checklist + sign-off.
- CLIENT_DELIVERY.md: record phases 3.10 and 3.11 completion.
162 lines
5.5 KiB
PHP
162 lines
5.5 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Console;
|
||
|
||
use App\Models\Currency;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use PHPUnit\Framework\Attributes\Test;
|
||
use Tests\TestCase;
|
||
|
||
/**
|
||
* تستهای دستور ifnex:preflight (فاز ۳.۱۰)
|
||
*
|
||
* نکتهٔ مهم: این تستها هرگز database.default را mutate نمیکنند، چون
|
||
* تغییر آن به 'mysql' در محیط تست (که DB_DATABASE=:memory: است) باعث
|
||
* میشود PDO سعی کند به MySQL با database name ':memory:' وصل شود که
|
||
* با خطای "Unknown database ':memory:'" fail میشود و connection pool
|
||
* را در حالت ناقص باقی میگذارد (cascade failure به تستهای بعدی).
|
||
*
|
||
* بهجای آن، از ifnex.preflight.expected_db_connection استفاده میکنیم
|
||
* تا به preflight بگوییم sqlite را بهعنوان DB مورد انتظار بپذیرد.
|
||
*/
|
||
class IfnexPreflightTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
private function runPreflight(array $args = []): \Illuminate\Testing\PendingCommand
|
||
{
|
||
return $this->artisan('ifnex:preflight', $args);
|
||
}
|
||
|
||
/**
|
||
* تنظیم configهای Production-hardened بهجز DB_CONNECTION.
|
||
* برای تستهایی که میخواهیم preflight PASS شود، expected_db_connection
|
||
* را روی sqlite میگذاریم تا با محیط تست همخوانی داشته باشد.
|
||
*/
|
||
private function hardenedConfig(array $overrides = []): void
|
||
{
|
||
config(array_merge([
|
||
'app.env' => 'production',
|
||
'app.debug' => false,
|
||
'app.key' => 'base64:'.str_repeat('A', 43),
|
||
'ifnex.api_key' => 'real-rotated-key-1234567890',
|
||
'ifnex.bridge_api_key' => 'real-rotated-bridge-key',
|
||
'ifnex.zarinpal.sandbox' => false,
|
||
'ifnex.zarinpal.merchant_id' => '00000000-0000-0000-0000-000000000000',
|
||
'ifnex.cors_allowed_origins' => ['https://ifnex.ir'],
|
||
// ← database.default را هرگز mutate نکنیم؛ اگه Need be، expected را تغییر میدهیم.
|
||
'ifnex.preflight.expected_db_connection' => 'sqlite',
|
||
], $overrides));
|
||
}
|
||
|
||
// ═══════════════════ FAIL scenarios ═══════════════════
|
||
|
||
#[Test]
|
||
public function it_fails_when_app_env_is_not_production()
|
||
{
|
||
// محیط تست testing است → باید fail شود
|
||
$this->runPreflight(['--skip-db' => true])
|
||
->assertFailed();
|
||
}
|
||
|
||
#[Test]
|
||
public function it_fails_when_debug_is_true_in_production()
|
||
{
|
||
$this->hardenedConfig([
|
||
'app.debug' => true,
|
||
]);
|
||
|
||
$this->runPreflight(['--skip-db' => true])
|
||
->assertFailed();
|
||
}
|
||
|
||
#[Test]
|
||
public function it_fails_when_api_keys_are_default()
|
||
{
|
||
$this->hardenedConfig([
|
||
'ifnex.api_key' => 'change-me',
|
||
'ifnex.bridge_api_key' => 'change-this-secret-key',
|
||
]);
|
||
|
||
$this->runPreflight(['--skip-db' => true])
|
||
->assertFailed();
|
||
}
|
||
|
||
#[Test]
|
||
public function it_fails_when_zarinpal_sandbox_is_true_in_production()
|
||
{
|
||
$this->hardenedConfig([
|
||
'ifnex.zarinpal.sandbox' => true,
|
||
]);
|
||
|
||
$this->runPreflight(['--skip-db' => true])
|
||
->assertFailed();
|
||
}
|
||
|
||
#[Test]
|
||
public function it_fails_when_db_connection_is_not_expected()
|
||
{
|
||
// تست میگوید: اگر expected=mysql ولی actual=sqlite، باید fail شود.
|
||
// در محیط تست، database.default = sqlite است.
|
||
$this->hardenedConfig([
|
||
'ifnex.preflight.expected_db_connection' => 'mysql', // انتظار mysql ولی واقعیت sqlite
|
||
]);
|
||
|
||
$this->runPreflight(['--skip-db' => true])
|
||
->assertFailed();
|
||
}
|
||
|
||
#[Test]
|
||
public function it_fails_when_cors_has_localhost()
|
||
{
|
||
$this->hardenedConfig([
|
||
'ifnex.cors_allowed_origins' => ['https://ifnex.ir', 'http://localhost'],
|
||
]);
|
||
|
||
$this->runPreflight(['--skip-db' => true])
|
||
->assertFailed();
|
||
}
|
||
|
||
#[Test]
|
||
public function it_fails_when_currencies_table_is_empty()
|
||
{
|
||
$this->hardenedConfig();
|
||
// جدول currencies خالی است (RefreshDatabase آن را خالی ایجاد کرده)
|
||
|
||
$this->runPreflight()
|
||
->assertFailed();
|
||
}
|
||
|
||
// ═══════════════════ PASS scenarios ═══════════════════
|
||
|
||
#[Test]
|
||
public function it_passes_when_everything_is_hardened_and_currencies_seeded()
|
||
{
|
||
$this->hardenedConfig();
|
||
|
||
// ایجاد ۵ ارز پایه (با اتصال sqlite که محیط تست پیشفرض است)
|
||
foreach (['USD', 'AED', 'EUR', 'CNY', 'IRR'] as $code) {
|
||
Currency::create([
|
||
'code' => $code,
|
||
'name' => $code,
|
||
'symbol' => $code,
|
||
'rate_to_rial' => $code === 'IRR' ? 1 : 100000,
|
||
'is_active' => true,
|
||
]);
|
||
}
|
||
|
||
$this->runPreflight()
|
||
->assertSuccessful();
|
||
}
|
||
|
||
#[Test]
|
||
public function it_can_skip_db_check_with_option()
|
||
{
|
||
$this->hardenedConfig();
|
||
|
||
// با --skip-db نباید currencies را چک کند
|
||
$this->runPreflight(['--skip-db' => true])
|
||
->assertSuccessful();
|
||
}
|
||
}
|