feca-mailshots-plugin/tests/unit/test_dsl_pipeline.php

65 lines
1.8 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([
'contacts' => ['ID', 'Accountid', 'Last', 'Email', 'FENContact1'],
'accounts' => ['ID', 'Name', 'Type'],
'renewals' => ['id', 'account_id', 'status', 'selected'],
'members.ExcludedAccounts' => ['ExcludedAccount'],
]);
$parser = new DslParser();
$validator = new DslValidator($metadata);
$compiler = new DslCompiler($metadata);
$cases = [
[
'dsl' => "contacts and accounts where contacts.Last contains 'smith' and contacts.Accountid = accounts.ID",
'expectValid' => true,
],
[
'dsl' => 'accounts where pending-renewal',
'expectValid' => false,
],
[
'dsl' => 'contacts and members.ExcludedAccounts',
'expectValid' => false,
],
];
$failures = [];
foreach ($cases as $case) {
$ast = $parser->parse($case['dsl']);
$validation = $validator->validate($ast);
$isValid = $validation['errors'] === [];
if ($isValid !== $case['expectValid']) {
$failures[] = ['dsl' => $case['dsl'], 'errors' => $validation['errors']];
continue;
}
if ($isValid) {
$compiled = $compiler->compile($ast);
if (strpos($compiled['sql'], 'SELECT ') !== 0) {
$failures[] = ['dsl' => $case['dsl'], 'errors' => ['Compilation did not produce SELECT']];
}
}
}
if ($failures !== []) {
fwrite(STDERR, "DSL pipeline test failures:\n" . json_encode($failures, JSON_PRETTY_PRINT) . "\n");
exit(1);
}
echo "DSL pipeline tests passed\n";