Christopher Vagnetoft
e6c85b81e5
* Use the PHP context options to configure tls rather than reinventing the wheel. * Properly setup the SocketServer for ssl * Added generic getter for config values
67 lines
2.7 KiB
PHP
67 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace NoccyLabs\Mercureact\Broker;
|
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
|
|
#[CoversClass(Topic::class)]
|
|
class TopicTest extends \PHPUnit\Framework\TestCase
|
|
{
|
|
|
|
public function testPublicMessagesAreDeliveredToAllSubscribers()
|
|
{
|
|
$authorizedSubscriber = new class implements SubscriberInterface {
|
|
public array $messages = [];
|
|
public function isAuthorized():bool { return true; }
|
|
public function deliver(Message $message):void { $this->messages[] = $message; }
|
|
public function getPayload(): ?array { return null; }
|
|
public function getId(): string { return ""; }
|
|
};
|
|
$unauthorizedSubscriber = new class implements SubscriberInterface {
|
|
public array $messages = [];
|
|
public function isAuthorized():bool { return false; }
|
|
public function deliver(Message $message):void { $this->messages[] = $message; }
|
|
public function getPayload(): ?array { return null; }
|
|
public function getId(): string { return ""; }
|
|
};
|
|
|
|
$topic = new Topic("foo");
|
|
$topic->addSubscriber($authorizedSubscriber);
|
|
$topic->addSubscriber($unauthorizedSubscriber);
|
|
|
|
$message = new Message(topic:["foo"], data:"test", private:false);
|
|
$topic->publish($message);
|
|
|
|
$this->assertEquals(1, count($authorizedSubscriber->messages));
|
|
$this->assertEquals(1, count($unauthorizedSubscriber->messages));
|
|
}
|
|
|
|
public function testPrivateMessagesAreNotDeliveredToUnauthorizedSubscribers()
|
|
{
|
|
$authorizedSubscriber = new class implements SubscriberInterface {
|
|
public array $messages = [];
|
|
public function isAuthorized():bool { return true; }
|
|
public function deliver(Message $message):void { $this->messages[] = $message; }
|
|
public function getPayload(): ?array { return null; }
|
|
public function getId(): string { return ""; }
|
|
};
|
|
$unauthorizedSubscriber = new class implements SubscriberInterface {
|
|
public array $messages = [];
|
|
public function isAuthorized():bool { return false; }
|
|
public function deliver(Message $message):void { $this->messages[] = $message; }
|
|
public function getPayload(): ?array { return null; }
|
|
public function getId(): string { return ""; }
|
|
};
|
|
|
|
$topic = new Topic("foo");
|
|
$topic->addSubscriber($authorizedSubscriber);
|
|
$topic->addSubscriber($unauthorizedSubscriber);
|
|
|
|
$message = new Message(topic:["foo"], data:"test", private:true);
|
|
$topic->publish($message);
|
|
|
|
$this->assertEquals(1, count($authorizedSubscriber->messages));
|
|
$this->assertEquals(0, count($unauthorizedSubscriber->messages));
|
|
}
|
|
|
|
} |