Files
myep-team/tests/Config/EmailTextCatalogTest.php

102 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Config;
use App\Config\EmailTextCatalog;
use App\Config\EmailTextKey;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* The delivered wording is what every mail falls back to, and it is also the text an admin
* starts editing from. A default that references a placeholder nobody declared would print
* a dash forever without anyone noticing, which is what these tests are here to prevent.
*
* Since the wording moved to config/email_texts.yaml, building every definition here is
* also what proves that file is complete: a missing entry or a placeholder without a
* description fails in build() before any assertion runs.
*/
class EmailTextCatalogTest extends KernelTestCase
{
public function testEveryKeyHasADefinition(): void
{
$catalog = $this->catalog();
foreach (EmailTextKey::cases() as $key) {
$definition = $catalog->get($key);
$this->assertSame($key, $definition->key);
$this->assertNotSame('', $definition->label);
$this->assertNotSame('', $definition->defaultSubject);
$this->assertNotSame('', $definition->defaultBody);
}
}
public function testDefaultsOnlyUseDeclaredPlaceholders(): void
{
$catalog = $this->catalog();
foreach (EmailTextKey::cases() as $key) {
$definition = $catalog->get($key);
$declared = $definition->getPlaceholderNames();
preg_match_all(
'/\{(\w+)\}/',
$definition->defaultSubject.' '.$definition->defaultHeadline.' '.$definition->defaultBody,
$matches
);
$used = array_unique($matches[1]);
$this->assertSame(
[],
array_diff($used, $declared),
sprintf('Default wording of "%s" uses undeclared placeholders', $key->value)
);
}
}
/**
* A placeholder nobody prints is either a leftover or a hint that the wording lost a
* detail it used to carry.
*/
public function testEveryDeclaredPlaceholderIsUsedByTheDefaults(): void
{
$catalog = $this->catalog();
foreach (EmailTextKey::cases() as $key) {
$definition = $catalog->get($key);
$text = $definition->defaultSubject.' '.$definition->defaultHeadline.' '.$definition->defaultBody;
foreach ($definition->getPlaceholderNames() as $name) {
$this->assertStringContainsString(
'{'.$name.'}',
$text,
sprintf('Placeholder "%s" of "%s" is declared but never used', $name, $key->value)
);
}
}
}
public function testEveryPlaceholderIsDescribedForTheAdmin(): void
{
$catalog = $this->catalog();
foreach (EmailTextKey::cases() as $key) {
foreach ($catalog->get($key)->placeholders as $name => $description) {
$this->assertNotSame('', trim($description), sprintf('Placeholder "%s" has no description', $name));
}
}
}
private function catalog(): EmailTextCatalog
{
self::bootKernel();
/** @var EmailTextCatalog $catalog */
$catalog = self::getContainer()->get(EmailTextCatalog::class);
return $catalog;
}
}