|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PubNubTests\unit; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use PubNub\Builders\DTO\SubscribeOperation; |
| 7 | +use PubNub\Managers\StateManager; |
| 8 | +use PubNub\PNConfiguration; |
| 9 | +use PubNub\PubNub; |
| 10 | + |
| 11 | +class StateManagerTest extends TestCase |
| 12 | +{ |
| 13 | + private PubNub $pubnub; |
| 14 | + private StateManager $stateManager; |
| 15 | + |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + $config = new PNConfiguration(); |
| 19 | + $config->setSubscribeKey('demo'); |
| 20 | + $config->setUserId('test-user'); |
| 21 | + |
| 22 | + $this->pubnub = new PubNub($config); |
| 23 | + $this->stateManager = new StateManager($this->pubnub); |
| 24 | + } |
| 25 | + |
| 26 | + public function testStateManagerDeduplicatesChannelListWithDuplicates(): void |
| 27 | + { |
| 28 | + // Subscribe with duplicate channels in a single operation |
| 29 | + $operation = new SubscribeOperation(['ch1', 'ch1', 'ch2'], [], false, null); |
| 30 | + |
| 31 | + $this->stateManager->adaptSubscribeBuilder($operation); |
| 32 | + |
| 33 | + // Get the prepared channel list |
| 34 | + $channels = $this->stateManager->prepareChannelList(false); |
| 35 | + |
| 36 | + // Should be deduplicated - only unique channels |
| 37 | + $this->assertCount(2, $channels, 'StateManager should deduplicate channels'); |
| 38 | + $this->assertContains('ch1', $channels); |
| 39 | + $this->assertContains('ch2', $channels); |
| 40 | + |
| 41 | + // Verify ch1 appears only once |
| 42 | + $ch1Count = count(array_filter($channels, fn($ch) => $ch === 'ch1')); |
| 43 | + $this->assertEquals(1, $ch1Count, 'Channel ch1 should appear only once'); |
| 44 | + } |
| 45 | + |
| 46 | + public function testStateManagerDeduplicatesWhenSubscribingToSameChannelMultipleTimes(): void |
| 47 | + { |
| 48 | + // First subscription to ch1 |
| 49 | + $operation1 = new SubscribeOperation(['ch1'], [], false, null); |
| 50 | + $this->stateManager->adaptSubscribeBuilder($operation1); |
| 51 | + |
| 52 | + // Second subscription to ch1 and ch2 |
| 53 | + $operation2 = new SubscribeOperation(['ch1', 'ch2'], [], false, null); |
| 54 | + $this->stateManager->adaptSubscribeBuilder($operation2); |
| 55 | + |
| 56 | + // Get the prepared channel list |
| 57 | + $channels = $this->stateManager->prepareChannelList(false); |
| 58 | + |
| 59 | + // Should be deduplicated - only unique channels |
| 60 | + $this->assertCount(2, $channels, 'StateManager should deduplicate channels across multiple subscriptions'); |
| 61 | + $this->assertContains('ch1', $channels); |
| 62 | + $this->assertContains('ch2', $channels); |
| 63 | + |
| 64 | + // Verify ch1 appears only once |
| 65 | + $ch1Count = count(array_filter($channels, fn($ch) => $ch === 'ch1')); |
| 66 | + $this->assertEquals(1, $ch1Count, 'Channel ch1 should appear only once even after multiple subscriptions'); |
| 67 | + } |
| 68 | + |
| 69 | + public function testStateManagerDeduplicatesChannelGroups(): void |
| 70 | + { |
| 71 | + // Subscribe with duplicate channel groups |
| 72 | + $operation = new SubscribeOperation([], ['cg1', 'cg1', 'cg2'], false, null); |
| 73 | + |
| 74 | + $this->stateManager->adaptSubscribeBuilder($operation); |
| 75 | + |
| 76 | + // Get the prepared channel group list |
| 77 | + $channelGroups = $this->stateManager->prepareChannelGroupList(false); |
| 78 | + |
| 79 | + // Should be deduplicated |
| 80 | + $this->assertCount(2, $channelGroups, 'StateManager should deduplicate channel groups'); |
| 81 | + $this->assertContains('cg1', $channelGroups); |
| 82 | + $this->assertContains('cg2', $channelGroups); |
| 83 | + |
| 84 | + // Verify cg1 appears only once |
| 85 | + $cg1Count = count(array_filter($channelGroups, fn($cg) => $cg === 'cg1')); |
| 86 | + $this->assertEquals(1, $cg1Count, 'Channel group cg1 should appear only once'); |
| 87 | + } |
| 88 | +} |
0 commit comments