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

73 lines
2.6 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace PhpQml\Bridge\Tests;
use PhpQml\Bridge\Publisher;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
#[CoversClass(Publisher::class)]
final class PublisherTest extends TestCase
{
public function testPublishWritesEnvelopeAsJsonOnTheGivenTopic(): void
{
$captured = null;
$hub = new class($captured) implements HubInterface {
public function __construct(private mixed &$captured) {}
public function getUrl(): string { return 'http://localhost/.well-known/mercure'; }
public function getPublicUrl(): string { return $this->getUrl(); }
public function getProvider(): \Symfony\Component\Mercure\Jwt\TokenProviderInterface
{
throw new \LogicException('not used in test');
}
public function getFactory(): ?\Symfony\Component\Mercure\Jwt\TokenFactoryInterface
{
return null;
}
public function publish(Update $update): string
{
$this->captured = $update;
return 'urn:uuid:test';
}
};
$publisher = new Publisher($hub);
$id = $publisher->publish('app://model/todo', ['op' => 'upsert', 'id' => '1', 'data' => ['done' => true], 'version' => 7]);
self::assertSame('urn:uuid:test', $id);
self::assertInstanceOf(Update::class, $captured);
self::assertSame(['app://model/todo'], $captured->getTopics());
self::assertJsonStringEqualsJsonString(
'{"op":"upsert","id":"1","data":{"done":true},"version":7}',
$captured->getData(),
);
self::assertFalse($captured->isPrivate());
}
public function testPrivateFlagIsForwarded(): void
{
$captured = null;
$hub = new class($captured) implements HubInterface {
public function __construct(private mixed &$captured) {}
public function getUrl(): string { return ''; }
public function getPublicUrl(): string { return ''; }
public function getProvider(): \Symfony\Component\Mercure\Jwt\TokenProviderInterface { throw new \LogicException(); }
public function getFactory(): ?\Symfony\Component\Mercure\Jwt\TokenFactoryInterface { return null; }
public function publish(Update $update): string { $this->captured = $update; return ''; }
};
(new Publisher($hub))->publish('app://event/internal', ['data' => 'x'], private: true);
self::assertTrue($captured->isPrivate());
}
}