69 lines
2.3 KiB
PHP
69 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once dirname(__DIR__, 2) . '/feca_mailshots_plugin/src/autoload.php';
|
|
|
|
use FecaMailshots\Application\AttachmentService;
|
|
use FecaMailshots\Infrastructure\Env;
|
|
use FecaMailshots\Infrastructure\PdoDatabaseRouter;
|
|
use FecaMailshots\Repository\AttachmentRepository;
|
|
|
|
Env::load(dirname(__DIR__, 2) . '/credentials/.env');
|
|
|
|
$dbConfig = [
|
|
'MYSQL_HOST' => '127.0.0.1',
|
|
'MYSQL_PORT' => (string) (getenv('MYSQL_TUNNEL_LOCAL_PORT') ?: '13306'),
|
|
'MYSQL_USER' => Env::require('REMOTE_MYSQL_USER'),
|
|
'MYSQL_PASSWORD' => Env::require('REMOTE_MYSQL_PASSWORD'),
|
|
'MAILSHOTS_REMOTE_MYSQL_DB' => Env::require('MAILSHOTS_REMOTE_MYSQL_DB'),
|
|
'MEMBERS_REMOTE_MYSQL_DB' => Env::require('MEMBERS_REMOTE_MYSQL_DB'),
|
|
'FEN_REMOTE_MYSQL_DB' => Env::require('FEN_REMOTE_MYSQL_DB'),
|
|
];
|
|
|
|
$router = new PdoDatabaseRouter($dbConfig);
|
|
$repo = new AttachmentRepository($router);
|
|
$service = new AttachmentService($repo);
|
|
$name = 'blob_roundtrip_' . gmdate('Ymd_His') . '_' . bin2hex(random_bytes(3));
|
|
$id = null;
|
|
|
|
$payload = "%PDF-1.7\n" . random_bytes(1536 * 1024) . "\n%%EOF";
|
|
|
|
try {
|
|
$saved = $service->save(null, [
|
|
'name' => $name,
|
|
'file_name' => 'roundtrip.pdf',
|
|
'mime_type' => 'application/pdf',
|
|
'file_bytes_base64' => base64_encode($payload),
|
|
]);
|
|
if (($saved['ok'] ?? false) !== true) {
|
|
fwrite(STDERR, 'Failed to save large attachment: ' . json_encode($saved) . "\n");
|
|
exit(1);
|
|
}
|
|
$id = (int) ($saved['id'] ?? 0);
|
|
|
|
$reloaded = $repo->findByName($name);
|
|
if (!is_array($reloaded)) {
|
|
fwrite(STDERR, "Large attachment was not found after save\n");
|
|
exit(1);
|
|
}
|
|
$bytes = (string) ($reloaded['file_bytes'] ?? '');
|
|
if (strlen($bytes) !== strlen($payload)) {
|
|
fwrite(STDERR, 'Large attachment byte length changed: expected ' . strlen($payload) . ', got ' . strlen($bytes) . "\n");
|
|
exit(1);
|
|
}
|
|
if (hash('sha256', $bytes) !== hash('sha256', $payload)) {
|
|
fwrite(STDERR, "Large attachment bytes changed after DB round-trip\n");
|
|
exit(1);
|
|
}
|
|
} finally {
|
|
if ($id !== null && $id > 0) {
|
|
try {
|
|
$repo->delete($id);
|
|
} catch (Throwable $e) {
|
|
}
|
|
}
|
|
}
|
|
|
|
echo "Attachment blob round-trip regression test passed\n";
|