Files

39 lines
1.8 KiB
PHP

<?php
use App\Models\User;
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
use Illuminate\Contracts\Http\Kernel as HttpKernel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
require __DIR__.'/../../vendor/autoload.php';
$database = $argv[1] ?? '';
if (! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/', $database)) {
fwrite(STDERR, 'Base de testes inválida.');
exit(2);
}
$app = require __DIR__.'/../../bootstrap/app.php';
$app->make(ConsoleKernel::class)->bootstrap();
if (! $app->environment('testing')) {
throw new LogicException('O worker só pode executar em APP_ENV=testing.');
}
$config = config('database.connections.pgsql');
$config['database'] = $database;
$config['url'] = null;
config(['database.default' => 'pgsql', 'database.connections.pgsql' => $config]);
DB::purge('pgsql');
if (DB::selectOne('select current_database() as name')->name !== $database) {
throw new LogicException('A ligação não corresponde à base de testes.');
}
DB::select('select set_config(?, ?, false)', ['application_name', 'procura_receipt_'.$argv[4]]);
$pid = DB::selectOne('select pg_backend_pid() as pid')->pid;
auth()->guard()->setUser(User::query()->findOrFail($argv[2]));
$payload = json_decode(stream_get_contents(STDIN), true, 512, JSON_THROW_ON_ERROR);
$request = Request::create('/ordens/'.$argv[3].'/recepcoes', 'POST', $payload, server: ['HTTP_ACCEPT' => 'application/json']);
$kernel = $app->make(HttpKernel::class);
$response = $kernel->handle($request);
$body = json_decode($response->getContent(), true);
$kernel->terminate($request, $response);
echo json_encode(['status' => $response->getStatusCode(), 'id' => $body['id'] ?? null, 'created' => $body['created'] ?? null, 'error_fields' => array_keys($body['errors'] ?? []), 'backend_pid' => $pid], JSON_THROW_ON_ERROR);