feca-mailshots-plugin/tests/unit/test_dsl_accounts_picklists...

70 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
require_once dirname(__DIR__, 2) . '/feca_mailshots_plugin/src/autoload.php';
require_once __DIR__ . '/FakeMetadataProvider.php';
use FecaMailshots\Application\DslCompiler;
use FecaMailshots\Application\DslValidator;
use FecaMailshots\Domain\DslParser;
use FecaMailshots\Tests\Unit\FakeMetadataProvider;
$metadata = new FakeMetadataProvider([
'accounts' => ['id', 'name', 'account_type_id', 'sector_id', 'public_location_id'],
'contacts' => ['id', 'account_id', 'contact_email_1'],
]);
$parser = new DslParser();
$compiler = new DslCompiler($metadata);
$validator = new DslValidator($metadata);
$ast = $parser->parse("accounts and contacts where accounts.account_type_id = 1");
$compiled = $compiler->compile($ast);
$sql = (string) ($compiled['sql'] ?? '');
if (strpos($sql, 'picklist_') !== false) {
fwrite(STDERR, "Did not expect implicit picklist joins in physical-schema projection\n");
exit(1);
}
if (strpos($sql, 's_accounts.`account_type_id` AS `accounts.account_type_id`') === false) {
fwrite(STDERR, "Expected physical accounts.account_type_id projection\n");
exit(1);
}
if (strpos($sql, "COALESCE(s_accounts.`account_type_id`, '') = ?") === false) {
fwrite(STDERR, "Expected predicate accounts.account_type_id to compile against physical column\n");
exit(1);
}
$memberAst = $parser->parse('accounts where member-account');
$memberValidation = $validator->validate($memberAst);
if (($memberValidation['errors'] ?? []) !== []) {
fwrite(STDERR, "Expected member-account to validate on accounts: " . json_encode($memberValidation['errors']) . "\n");
exit(1);
}
$memberSql = (string) ($compiler->compile($memberAst)['sql'] ?? '');
foreach ([
's_accounts.`account_type_id` IN (SELECT id FROM `picklist_account_type`',
"LOWER(TRIM(COALESCE(`slug`, ''))) = 'member'",
] as $needle) {
if (strpos($memberSql, $needle) === false) {
fwrite(STDERR, "Expected member-account SQL to contain {$needle}\n{$memberSql}\n");
exit(1);
}
}
$affiliateAst = $parser->parse('accounts where affiliate-account');
$affiliateSql = (string) ($compiler->compile($affiliateAst)['sql'] ?? '');
if (strpos($affiliateSql, "LOWER(TRIM(COALESCE(`slug`, ''))) = 'affiliate'") === false) {
fwrite(STDERR, "Expected affiliate-account SQL to filter by affiliate slug\n{$affiliateSql}\n");
exit(1);
}
$invalidValidation = $validator->validate($parser->parse('contacts where member-account'));
if (!in_array('Filter member-account requires source accounts', $invalidValidation['errors'] ?? [], true)) {
fwrite(STDERR, "Expected member-account to require accounts source: " . json_encode($invalidValidation['errors'] ?? []) . "\n");
exit(1);
}
echo "DSL accounts physical-schema regression test passed\n";