|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PubNubTests\unit\objects\member; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use PubNub\PNConfiguration; |
| 7 | +use PubNub\PubNub; |
| 8 | +use PubNub\Endpoints\Objects\Member\ManageMembers; |
| 9 | +use PubNub\Exceptions\PubNubValidationException; |
| 10 | +use PubNub\Models\Consumer\Objects\Member\PNChannelMember; |
| 11 | + |
| 12 | +/** |
| 13 | + * Regression tests for the members/uuids validation in ManageMembers. |
| 14 | + * |
| 15 | + * These pin the behaviour of the `||` fix. Previously the endpoint used `or`, |
| 16 | + * which binds looser than `=`, so `$members`/`$uuids` were assigned only the |
| 17 | + * `set*` operand and the `remove*` operand was silently discarded. As a result |
| 18 | + * remove-only operations wrongly failed validation, and mixed members+uuids |
| 19 | + * calls wrongly passed. See src/PubNub/Endpoints/Objects/Member/ManageMembers.php. |
| 20 | + */ |
| 21 | +class ManageMembersValidationTest extends TestCase |
| 22 | +{ |
| 23 | + private PubNub $pubnub; |
| 24 | + |
| 25 | + public function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + $config = new PNConfiguration(); |
| 29 | + $config->setSubscribeKey('demo'); |
| 30 | + $config->setPublishKey('demo'); |
| 31 | + $config->setUuid('validation-test-uuid'); |
| 32 | + $this->pubnub = new PubNub($config); |
| 33 | + } |
| 34 | + |
| 35 | + private function invokeValidateParams(ManageMembers $endpoint): void |
| 36 | + { |
| 37 | + $method = new \ReflectionMethod($endpoint, 'validateParams'); |
| 38 | + $method->invoke($endpoint); |
| 39 | + } |
| 40 | + |
| 41 | + public function testSetMembersOnlyPassesValidation(): void |
| 42 | + { |
| 43 | + $endpoint = $this->pubnub->manageMembers() |
| 44 | + ->channel('ch') |
| 45 | + ->setMembers([new PNChannelMember('u1')]); |
| 46 | + |
| 47 | + $this->invokeValidateParams($endpoint); |
| 48 | + $this->assertTrue(true, 'set-only members must pass validation'); |
| 49 | + } |
| 50 | + |
| 51 | + public function testRemoveMembersOnlyPassesValidation(): void |
| 52 | + { |
| 53 | + // Regression: with the old `or` bug this threw "Members or a list of uuids missing". |
| 54 | + $endpoint = $this->pubnub->manageMembers() |
| 55 | + ->channel('ch') |
| 56 | + ->removeMembers([new PNChannelMember('u1')]); |
| 57 | + |
| 58 | + $this->invokeValidateParams($endpoint); |
| 59 | + $this->assertTrue(true, 'remove-only members must pass validation'); |
| 60 | + } |
| 61 | + |
| 62 | + public function testRemoveUuidsOnlyPassesValidation(): void |
| 63 | + { |
| 64 | + // Regression: with the old `or` bug this threw "Members or a list of uuids missing". |
| 65 | + $endpoint = $this->pubnub->manageMembers() |
| 66 | + ->channel('ch') |
| 67 | + ->removeUuids(['u1']); |
| 68 | + |
| 69 | + $this->invokeValidateParams($endpoint); |
| 70 | + $this->assertTrue(true, 'remove-only uuids must pass validation'); |
| 71 | + } |
| 72 | + |
| 73 | + public function testSetAndRemoveMembersTogetherPassesValidation(): void |
| 74 | + { |
| 75 | + $endpoint = $this->pubnub->manageMembers() |
| 76 | + ->channel('ch') |
| 77 | + ->setMembers([new PNChannelMember('u1')]) |
| 78 | + ->removeMembers([new PNChannelMember('u2')]); |
| 79 | + |
| 80 | + $this->invokeValidateParams($endpoint); |
| 81 | + $this->assertTrue(true, 'set + remove on the members side must pass validation'); |
| 82 | + } |
| 83 | + |
| 84 | + public function testMixingRemoveMembersAndSetUuidsThrows(): void |
| 85 | + { |
| 86 | + // Regression: with the old `or` bug $members was computed as false, so this |
| 87 | + // mixed members+uuids call slipped through validation. It must now be rejected. |
| 88 | + $endpoint = $this->pubnub->manageMembers() |
| 89 | + ->channel('ch') |
| 90 | + ->removeMembers([new PNChannelMember('u1')]) |
| 91 | + ->setUuids(['u2']); |
| 92 | + |
| 93 | + $this->expectException(PubNubValidationException::class); |
| 94 | + $this->expectExceptionMessage('Either members or uuids should be provided'); |
| 95 | + $this->invokeValidateParams($endpoint); |
| 96 | + } |
| 97 | + |
| 98 | + public function testMixingSetMembersAndRemoveUuidsThrows(): void |
| 99 | + { |
| 100 | + $endpoint = $this->pubnub->manageMembers() |
| 101 | + ->channel('ch') |
| 102 | + ->setMembers([new PNChannelMember('u1')]) |
| 103 | + ->removeUuids(['u2']); |
| 104 | + |
| 105 | + $this->expectException(PubNubValidationException::class); |
| 106 | + $this->expectExceptionMessage('Either members or uuids should be provided'); |
| 107 | + $this->invokeValidateParams($endpoint); |
| 108 | + } |
| 109 | + |
| 110 | + public function testNothingProvidedThrows(): void |
| 111 | + { |
| 112 | + $endpoint = $this->pubnub->manageMembers() |
| 113 | + ->channel('ch'); |
| 114 | + |
| 115 | + $this->expectException(PubNubValidationException::class); |
| 116 | + $this->expectExceptionMessage('Members or a list of uuids missing'); |
| 117 | + $this->invokeValidateParams($endpoint); |
| 118 | + } |
| 119 | + |
| 120 | + public function testMissingChannelThrows(): void |
| 121 | + { |
| 122 | + $endpoint = $this->pubnub->manageMembers() |
| 123 | + ->setMembers([new PNChannelMember('u1')]); |
| 124 | + |
| 125 | + $this->expectException(PubNubValidationException::class); |
| 126 | + $this->expectExceptionMessage('channel missing'); |
| 127 | + $this->invokeValidateParams($endpoint); |
| 128 | + } |
| 129 | +} |
0 commit comments