Skip to content

Commit

Permalink
[1.x] Sends data property when empty (#124)
Browse files Browse the repository at this point in the history
* Fix missing data property for pusher internal events with empty data

* Provide dedicated implementation of internal payload formatter

* wip

---------

Co-authored-by: Szymon Jabłoński <[email protected]>
Co-authored-by: Joe Dixon <[email protected]>
  • Loading branch information
3 people authored Apr 5, 2024
1 parent d79f896 commit 598bc99
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/Protocols/Pusher/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ public function formatPayload(string $event, array $data = [], ?string $channel
*/
public function formatInternalPayload(string $event, array $data = [], $channel = null): string|false
{
return static::formatPayload($event, $data, $channel, 'pusher_internal:');
return json_encode(
array_filter([
'event' => 'pusher_internal:'.$event,
'data' => json_encode((object) $data),
'channel' => $channel,
])
);
}
}
16 changes: 8 additions & 8 deletions tests/Feature/Protocols/Pusher/Reverb/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
$response = subscribe('test-channel');

expect(channels()->find('test-channel')->connections())->toHaveCount(1);
expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","channel":"test-channel"}');
expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","data":"{}","channel":"test-channel"}');
});

it('can subscribe to a private channel', function () {
$response = subscribe('private-test-channel');

expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","channel":"private-test-channel"}');
expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","data":"{}","channel":"private-test-channel"}');
});

it('can subscribe to a presence channel', function () {
Expand All @@ -41,13 +41,13 @@
it('can subscribe to a cache channel', function () {
$response = subscribe('cache-test-channel');

expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","channel":"cache-test-channel"}');
expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","data":"{}","channel":"cache-test-channel"}');
});

it('can subscribe to a private cache channel', function () {
$response = subscribe('private-cache-test-channel');

expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","channel":"private-cache-test-channel"}');
expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","data":"{}","channel":"private-cache-test-channel"}');
});

it('can subscribe to a presence cache channel', function () {
Expand Down Expand Up @@ -141,21 +141,21 @@
$connection->assertReceived('{"event":"App\\\\Events\\\\TestEvent","data":"{\"foo\":\"bar\"}","channel":"presence-cache-test-channel"}');
});

it('can receive a cach missed message when joining a cache channel with an empty cache', function () {
it('can receive a cache missed message when joining a cache channel with an empty cache', function () {
$connection = connect();
subscribe('cache-test-channel', connection: $connection);

$connection->assertReceived('{"event":"pusher:cache_miss","channel":"cache-test-channel"}');
});

it('can receive a cach missed message when joining a private cache channel with an empty cache', function () {
it('can receive a cache missed message when joining a private cache channel with an empty cache', function () {
$connection = connect();
subscribe('private-cache-test-channel', connection: $connection);

$connection->assertReceived('{"event":"pusher:cache_miss","channel":"private-cache-test-channel"}');
});

it('can receive a cach missed message when joining a presence cache channel with an empty cache', function () {
it('can receive a cache missed message when joining a presence cache channel with an empty cache', function () {
$connection = connect();
subscribe('presence-cache-test-channel', connection: $connection);

Expand Down Expand Up @@ -455,7 +455,7 @@
],
], $connection);

expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","channel":"my-channel"}');
expect($response)->toBe('{"event":"pusher_internal:subscription_succeeded","data":"{}","channel":"my-channel"}');
});

it('buffers large requests correctly', function () {
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Protocols/Pusher/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

$this->connection->assertReceived([
'event' => 'pusher_internal:subscription_succeeded',
'data' => '{}',
'channel' => 'test-channel',
]);
});
Expand Down Expand Up @@ -63,11 +64,10 @@
'foo',
['bar' => 'baz'],
'test-channel',
'reverb:'
);

expect($payload)->toBe(json_encode([
'event' => 'reverb:foo',
'event' => 'pusher:foo',
'data' => json_encode(['bar' => 'baz']),
'channel' => 'test-channel',
]));
Expand All @@ -84,7 +84,6 @@
'foo',
['bar' => 'baz'],
'test-channel',
'reverb:'
);

expect($payload)->toBe(json_encode([
Expand All @@ -97,5 +96,6 @@

expect($payload)->toBe(json_encode([
'event' => 'pusher_internal:foo',
'data' => '{}',
]));
});
5 changes: 5 additions & 0 deletions tests/Unit/Protocols/Pusher/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

$connection->assertReceived([
'event' => 'pusher_internal:subscription_succeeded',
'data' => '{}',
'channel' => 'test-channel',
]);
});
Expand Down Expand Up @@ -108,6 +109,7 @@

$connection->assertReceived([
'event' => 'pusher_internal:subscription_succeeded',
'data' => '{}',
'channel' => 'test-channel',
]);
});
Expand All @@ -125,6 +127,7 @@

$connection->assertReceived([
'event' => 'pusher_internal:subscription_succeeded',
'data' => '{}',
'channel' => 'private-test-channel',
]);
});
Expand Down Expand Up @@ -165,6 +168,7 @@

$connection->assertReceived([
'event' => 'pusher_internal:subscription_succeeded',
'data' => '{}',
'channel' => 'cache-test-channel',
]);
$connection->assertReceived([
Expand Down Expand Up @@ -199,6 +203,7 @@

$connection->assertReceived([
'event' => 'pusher_internal:subscription_succeeded',
'data' => '{}',
'channel' => 'cache-test-channel',
]);
$connection->assertReceived(['foo' => 'bar']);
Expand Down

0 comments on commit 598bc99

Please sign in to comment.