Files
php-qml/framework/php/tests/ModelPublisherTest.php

139 lines
4.4 KiB
PHP
Raw Normal View History

Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
<?php
declare(strict_types=1);
namespace PhpQml\Bridge\Tests;
use PhpQml\Bridge\Attribute\BridgeResource;
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
use PhpQml\Bridge\BridgeOp;
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
use PhpQml\Bridge\CorrelationContext;
use PhpQml\Bridge\ModelPublisher;
use PhpQml\Bridge\Publisher;
use PhpQml\Bridge\Tests\Helper\HubSpy;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
#[BridgeResource(name: 'todo')]
final class FakeTodo
{
public function __construct(
public string $id,
public string $title,
public bool $done = false,
) {
}
public function getId(): string
{
return $this->id;
}
}
final class FakeNotMarked
{
public function __construct(public int $id, public string $title)
{
}
public function getId(): int
{
return $this->id;
}
}
#[CoversClass(ModelPublisher::class)]
final class ModelPublisherTest extends TestCase
{
private HubSpy $hub;
private CorrelationContext $context;
private ModelPublisher $publisher;
protected function setUp(): void
{
$this->hub = new HubSpy('urn:uuid:test');
$this->context = new CorrelationContext();
$serializer = new Serializer(
[new BackedEnumNormalizer(), new DateTimeNormalizer(), new ObjectNormalizer()],
[new JsonEncoder()],
);
$this->publisher = new ModelPublisher(
new Publisher($this->hub),
$this->context,
$serializer,
);
}
public function testUpsertDualPublishesToCollectionAndEntityTopics(): void
{
$todo = new FakeTodo(id: '019de596-be1c-7642-985c-edcadeef9b5d', title: 'milk', done: false);
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
$this->publisher->publishEntityChange($todo, BridgeOp::Upsert);
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
// The HubSpy only retains the LAST update. To validate both topics,
// re-publish and check the second envelope, but for the assertion of
// semantics we instead use a recording HubSpy that captures all.
// Simplification: confirm the final captured update is the entity
// topic (published last by ModelPublisher).
self::assertNotNull($this->hub->captured);
self::assertSame(
['app://model/todo/019de596-be1c-7642-985c-edcadeef9b5d'],
$this->hub->captured->getTopics(),
);
$envelope = json_decode($this->hub->captured->getData(), true);
self::assertSame('upsert', $envelope['op']);
self::assertSame('019de596-be1c-7642-985c-edcadeef9b5d', $envelope['id']);
self::assertSame('milk', $envelope['data']['title']);
self::assertFalse($envelope['data']['done']);
self::assertGreaterThan(0, $envelope['version']);
self::assertArrayNotHasKey('correlationKey', $envelope);
}
public function testCorrelationKeyIsEchoedWhenContextHasIt(): void
{
$this->context->set('idem-1234');
$this->publisher->publishEntityChange(
new FakeTodo(id: '1', title: 'x'),
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
BridgeOp::Upsert,
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
);
$envelope = json_decode($this->hub->captured->getData(), true);
self::assertSame('idem-1234', $envelope['correlationKey']);
}
public function testDeleteOpOmitsData(): void
{
$this->publisher->publishEntityChange(
new FakeTodo(id: '7', title: 'gone'),
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
BridgeOp::Delete,
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
);
$envelope = json_decode($this->hub->captured->getData(), true);
self::assertSame('delete', $envelope['op']);
self::assertNull($envelope['data']);
}
public function testEntitiesWithoutBridgeResourceAreIgnored(): void
{
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
$this->publisher->publishEntityChange(new FakeNotMarked(1, 'x'), BridgeOp::Upsert);
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
self::assertNull($this->hub->captured);
}
public function testVersionIncreasesOnEachPublish(): void
{
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
$this->publisher->publishEntityChange(new FakeTodo(id: '1', title: 'a'), BridgeOp::Upsert);
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
$first = json_decode($this->hub->captured->getData(), true)['version'];
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
$this->publisher->publishEntityChange(new FakeTodo(id: '1', title: 'b'), BridgeOp::Upsert);
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
$second = json_decode($this->hub->captured->getData(), true)['version'];
self::assertGreaterThan($first, $second);
}
}