First unit tests, misc fixes
This commit is contained in:
		
							
								
								
									
										70
									
								
								tests/Broker/MessageTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								tests/Broker/MessageTest.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,70 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace NoccyLabs\Mercureact\Broker;
 | 
			
		||||
 | 
			
		||||
use PHPUnit\Framework\Attributes\CoversClass;
 | 
			
		||||
 | 
			
		||||
#[CoversClass(Message::class)]
 | 
			
		||||
class MessageTest extends \PHPUnit\Framework\TestCase
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    public function testCreatingMessageFromData()
 | 
			
		||||
    {
 | 
			
		||||
        $message = Message::fromData([
 | 
			
		||||
            'id' => '1a',
 | 
			
		||||
            'topic' => '2b',
 | 
			
		||||
            'data' => '3c',
 | 
			
		||||
            'type' => '4d',
 | 
			
		||||
            'retry' => 42,
 | 
			
		||||
            'private' => true,
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals("1a", $message->id);
 | 
			
		||||
        $this->assertEquals(["2b"], $message->topic);
 | 
			
		||||
        $this->assertEquals("3c", $message->data);
 | 
			
		||||
        $this->assertEquals("4d", $message->type);
 | 
			
		||||
        $this->assertEquals(42, $message->retry);
 | 
			
		||||
        $this->assertEquals(true, $message->private);
 | 
			
		||||
 | 
			
		||||
        $message = Message::fromData([
 | 
			
		||||
            'id' => '1a',
 | 
			
		||||
            'topic' => ['2b'],
 | 
			
		||||
            'data' => '3c',
 | 
			
		||||
            'type' => '4d',
 | 
			
		||||
            'retry' => 42,
 | 
			
		||||
            'private' => true,
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals("1a", $message->id);
 | 
			
		||||
        $this->assertEquals(["2b"], $message->topic);
 | 
			
		||||
        $this->assertEquals("3c", $message->data);
 | 
			
		||||
        $this->assertEquals("4d", $message->type);
 | 
			
		||||
        $this->assertEquals(42, $message->retry);
 | 
			
		||||
        $this->assertEquals(true, $message->private);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testCreatingSseFromMessage()
 | 
			
		||||
    {
 | 
			
		||||
        $message = Message::fromData([
 | 
			
		||||
            'id' => '1a',
 | 
			
		||||
            'topic' => '2b',
 | 
			
		||||
            'data' => '3c',
 | 
			
		||||
            'type' => '4d',
 | 
			
		||||
            'retry' => 42,
 | 
			
		||||
            'private' => true,
 | 
			
		||||
        ]);
 | 
			
		||||
        $sse = $message->toString();
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals(<<<EXPECT
 | 
			
		||||
        event: 4d
 | 
			
		||||
        retry: 42
 | 
			
		||||
        id: 1a
 | 
			
		||||
        data: 3c
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
        EXPECT, $sse);
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										63
									
								
								tests/Broker/TopicTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								tests/Broker/TopicTest.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,63 @@
 | 
			
		||||
<?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; }
 | 
			
		||||
        };
 | 
			
		||||
        $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; }
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        $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; }
 | 
			
		||||
        };
 | 
			
		||||
        $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; }
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        $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));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user