Files
compras/tests/Support/InboxFixture.php
T
2026-11-02 20:27:11 +02:00

67 lines
3.0 KiB
PHP

<?php
use Tempo\Integration\WarehouseFeed;
use Tempo\Integration\WarehouseInbox;
require_once __DIR__.'/../../packages/integration/bootstrap.php';
/** Only disposable test stores; never installs the receiver in an application. */
function inboxConnection(): PDO
{
if (getenv('INBOX_TEST_PG') === '1') {
$name = getenv('DB_DATABASE');
if (getenv('DB_HOST') !== '127.0.0.1' || ! is_string($name) || ! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', $name)) {
throw new LogicException('Inbox PG exige uma base descartável identificada.');
}
$db = new PDO('pgsql:host=127.0.0.1;port='.getenv('DB_PORT').';dbname='.$name, getenv('DB_USERNAME'), getenv('DB_PASSWORD'), [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
if ($db->query('SELECT current_database()')->fetchColumn() !== $name) {
throw new LogicException('Destino de teste divergente.');
}
} else {
$db = new PDO('sqlite::memory:', options: [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
}
return $db;
}
function inboxDatabase(): PDO
{
$db = inboxConnection();
if (getenv('INBOX_TEST_PG') === '1') {
$db->exec('DROP TABLE IF EXISTS sgc_exchange_facts');
$db->exec('DROP TABLE IF EXISTS sgc_exchange_streams');
}
foreach (WarehouseInbox::schema() as $sql) {
$db->exec($sql);
}
return $db;
}
function inboxEvent(int $id = 1, string $action = 'deliver', ?string $delivery = null): array
{
$event = ['schema' => 'sgc.warehouse.fulfilment-event.v1', 'idempotency_key' => 'sgc:7:warehouse-event:'.$id,
'tenant_id' => 7, 'event_id' => $id, 'event_version' => $id + 2, 'action' => $action,
'occurred_at' => '2026-11-02T10:00:00+00:00', 'actor' => ['id' => 3, 'name' => 'Operador sintético'],
'request' => ['id' => 19, 'reference' => 'RA-19', 'document_type' => 'internal_warehouse_request',
'work_context_key' => 'procurement.internal', 'requester' => ['id' => 5, 'name' => 'Requisitante sintético', 'department_id' => 2],
'warehouse' => ['id' => 1, 'code' => 'ARM', 'name' => 'Armazém sintético'], 'cost_center' => ['id' => 2, 'code' => 'CC', 'name' => 'Centro sintético'], 'destination' => 'Oficina sintética'],
'lines' => $action === 'deliver' ? [['request_line_index' => 0, 'item' => ['id' => 4, 'code' => 'ART', 'name' => 'Artigo sintético', 'unit' => 'un'],
'quantity_milli' => 1500, 'use' => 'internal', 'destination' => 'Oficina sintética', 'equipment' => null, 'stock_operation_id' => $id + 10]] : [],
'delivery_event_key' => $delivery];
return inboxSign($event);
}
function inboxSign(array $event): array
{
unset($event['payload_sha256']);
return [...$event, 'payload_sha256' => hash('sha256', WarehouseFeed::json($event))];
}
function inboxPage(array $events, string $cursor = 'synthetic-opaque-cursor', bool $more = false): array
{
return ['schema' => 'sgc.warehouse.fulfilment-feed.v1', 'items' => $events, 'next_cursor' => $cursor, 'has_more' => $more];
}