Two profiles, one contract: testing the boundary of reuse
A journal and catalogue in one Symfony project: different adapters, routes and branding, one title-reading contract and composition checks.
A shared package can work perfectly in its first site while still depending on that site's routes, service names or familiar data structure. With only one consumer, these assumptions are easy to miss. A second site is useful for more than copying configuration: it tests whether the same promise can be fulfilled in another composition.
This exercise continues the single Symfony project, article_demo. The journal and catalogue are two synthetic profiles in one Kernel, with different adapters, routes and branding. We check their behavior and composition; independent installations, separate releases and deployment of two sites require other experiments.
Consider a narrow capability: reading a title by key and exact locale. A learning journal and an equipment catalogue will have different data, adapters, routes and branding. They will share the query type, result rules and one contract test suite. This separates behavioural portability from two pages that merely look similar.
The two consumers must differ meaningfully
Replacing the logo above the same data set tests very little. The second profile should force us to abandon at least some accidental assumptions of the first. A small but deliberate difference is sufficient.
| Property | Journal | Catalogue |
|---|---|---|
| Brand | Field Notebook | Workshop Shelf |
| Known key | journal-hinge |
catalogue-vice |
| Ukrainian title | Нотатка про петлю | Настільні лещата |
| Source structure | Dictionary of notes indexed by key | List of items with a ref field |
| Route | /notebook/{locale}/{key} |
/equipment/{key}/{locale} |
| Presentation | Article inside a journal block | Section with a specification list |
The chosen learning profile is Symfony 8.1 and PHP 8.4 or later with the required extensions. The official Symfony installation guide describes the skeleton and environment requirements. Exact installed dependency versions must come from a real installation; a version constraint in a command is not a substitute for a lock file.
Start the contract with the meaning of absence
The title reader accepts a key and a locale. In this example, a key contains only lowercase Latin letters, digits and hyphens, and the locale is one of three agreed codes. These are deliberately narrow teaching rules. They do not describe every possible identifier or language tag format.
<?php
declare(strict_types=1);
namespace App\TitleContract;
final readonly class TitleQuery
{
public function __construct(public string $key, public string $locale)
{
if (preg_match('/\A[a-z0-9][a-z0-9-]*\z/', $key) !== 1) {
throw new \InvalidArgumentException('Invalid title key.');
}
if (!in_array($locale, ['uk', 'ru', 'en'], true)) {
throw new \InvalidArgumentException('Unsupported locale.');
}
}
}
The source promises to return the title in the requested locale. If the record or translation is unavailable, the result is null. Substituting another language breaks that promise even if the user sees non-empty text. A source error must not become null either: absence and failure call for different actions.
<?php
declare(strict_types=1);
namespace App\TitleContract;
interface LocalizedTitleSource
{
/**
* Return the exact locale's plain-text title, or null when unavailable.
* Never fall back to another locale or turn a source failure into null.
*/
public function find(TitleQuery $query): ?string;
}
A small reader sits above the source. It checks one further shared condition: a title cannot consist solely of the ordinary whitespace characters removed by PHP trim. The PHP documentation defines them. Actual text is returned without trimming or adding presentation. This is neither a universal check of every Unicode space nor an HTML sanitiser.
<?php
declare(strict_types=1);
namespace App\TitleContract;
final readonly class TitleReader
{
public function __construct(private LocalizedTitleSource $source)
{
}
public function read(TitleQuery $query): ?string
{
$title = $this->source->find($query);
if ($title !== null && trim($title) === '') {
throw new \UnexpectedValueException('A title cannot be blank.');
}
return $title;
}
}
The contract contains no route, colour, site name or Entity. It also grants no permission to display arbitrary records. The site decides which data its adapter can expose, while the presentation is responsible for safely outputting the text.
Different adapters, one promise
The journal adapter looks up a note in a dictionary. The known record has UK/RU titles but no EN translation. A separate hidden note helps check that storing text does not automatically make it available through the reader.
<?php
declare(strict_types=1);
namespace App\TitleConsumer;
use App\TitleContract\LocalizedTitleSource;
use App\TitleContract\TitleQuery;
final class JournalTitles implements LocalizedTitleSource
{
private const NOTES = [
'journal-hinge' => [
'public' => true,
'translations' => ['uk' => 'Нотатка про петлю', 'ru' => 'Заметка о петле'],
],
'journal-draft' => [
'public' => false,
'translations' => ['uk' => 'Чернетка нотатки'],
],
];
public function find(TitleQuery $query): ?string
{
$note = self::NOTES[$query->key] ?? null;
if ($note === null || !$note['public']) {
return null;
}
return $note['translations'][$query->locale] ?? null;
}
}
The catalogue uses a different data shape. It iterates over items, matches the key to the ref field and reads its own title dictionary. The second adapter needs no journal entity, translations key or first-site name.
<?php
declare(strict_types=1);
namespace App\TitleConsumer;
use App\TitleContract\LocalizedTitleSource;
use App\TitleContract\TitleQuery;
final class CatalogueTitles implements LocalizedTitleSource
{
private const ITEMS = [
[
'ref' => 'catalogue-vice',
'visible' => true,
'titles' => ['uk' => 'Настільні лещата', 'ru' => 'Настольные тиски'],
],
[
'ref' => 'catalogue-hidden',
'visible' => false,
'titles' => ['uk' => 'Прихований інструмент'],
],
];
public function find(TitleQuery $query): ?string
{
foreach (self::ITEMS as $row) {
if ($row['ref'] === $query->key && $row['visible']) {
return $row['titles'][$query->locale] ?? null;
}
}
return null;
}
}
The public and visible flags belong to synthetic fixtures. They illustrate the boundary of an available source, but do not implement authentication or a complete permissions system. Likewise, the absence of a foreign key in an array does not establish the isolation of real databases, caches or accounts.
The deliberate difference between adapters matters more than their size. If the second site had to copy the journal's data structure to fulfil the contract, it would be checking compatibility with the first model instead of independence from it.
Two explicit compositions in one application
Shared types live in src/TitleContract and adapters in src/TitleConsumer. The journal uses src/Controller/JournalNoteController.php, while the catalogue uses src/Controller/EquipmentController.php. These are new controllers; preserve the existing NoteController and /notes routes. One Composer autoloader loads App\ and App\Tests\ from the standard project directories.
Merge the definitions below into the existing services block in config/services.yaml. Preserve earlier imports, _defaults, services and routes. Two named TitleReader instances receive different source adapters; one global alias would not select the correct source for both controllers.
services:
App\TitleConsumer\JournalTitles: ~
App\TitleConsumer\CatalogueTitles: ~
app.title_reader.journal:
class: App\TitleContract\TitleReader
arguments:
$source: '@App\TitleConsumer\JournalTitles'
app.title_reader.catalogue:
class: App\TitleContract\TitleReader
arguments:
$source: '@App\TitleConsumer\CatalogueTitles'
App\Controller\JournalNoteController:
arguments:
$reader: '@app.title_reader.journal'
$brand: 'Field Notebook'
App\Controller\EquipmentController:
arguments:
$reader: '@app.title_reader.catalogue'
$brand: 'Workshop Shelf'
JournalNoteController receives app.title_reader.journal and the Field Notebook brand; EquipmentController receives app.title_reader.catalogue and Workshop Shelf. An invalid TitleQuery produces 400, an absent title 404, and a source failure does not become a missing record. Separate Twig templates, templates/title_consumers/journal.html.twig and catalogue.html.twig, escape the displayed text. Controllers pass data to templates rather than concatenate HTML.
One suite, two sets of facts
tests/TitleContract/TitleSourceContractTestCase.php defines ten shared methods: exact UK/RU titles; a missing translation; unknown, hidden and foreign keys; an unsupported locale; an empty key; a whitespace-only title; and propagation of a source exception. The final two use small substitute sources. They do not exercise failure of a real external service.
JournalContractTest and CatalogueContractTest inherit the same assertions and provide separate literal facts. Expected titles are not calculated through the adapter being tested, which could repeat its own error. All tests live in the root tests/TitleContract directory; there is no installed package or reliance on a dependency’s autoload-dev.
The contract alone cannot detect a wrong service binding, route or brand. tests/Http/JournalPageTest.php and CataloguePageTest.php therefore have four checks each: their own page, absent EN, a foreign key in their own profile, and the second profile’s page with its own branding. The final case returns 200 because both routes legitimately coexist in one Kernel. A foreign key absent from one adapter does not establish isolation of real databases or accounts.
Check the complete added surface
php bin/console lint:container --env=test
php bin/console debug:router --env=test
php bin/phpunit tests/TitleContract tests/Http
Run the commands once from the article_demo root. The shared contract gives 20 executions across two adapters, and the HTTP files add 8. This describes the suite rather than a successful execution.
After your own baseline run, change one rule at a time. Substitute UK when catalogue EN is absent: the contract check should detect that even when the UK page works. Then temporarily change only the catalogue route: adapter contracts remain unchanged, while the HTTP check should detect the new address. Restore the code after each probe.
What the second profile establishes
This composition can check one promise against two different data structures, explicit adapter wiring and separate presentation. It does not test a clean installation of an independent Composer package, two separate containers, rollout or production recovery. Those promises require separate boundaries and observations.
If an adapter fails, inspect lookup and facts. If the contract holds but the page fails, inspect the container, route and template. Clarify a new shared rule as a contract first; leave product differences in the profile. The number of copied files cannot settle that decision.