Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 20, 2023
1 parent 12c4ce2 commit 30e14fe
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
7 changes: 6 additions & 1 deletion src/Pusher/Http/Controllers/ChannelUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public function handle(RequestInterface $request, Connection $connection, ...$ar
return new JsonResponse((object) [], 400);
}

return new JsonResponse((object) []);
$connections = collect($channel->connections())
->map(fn ($connection) => $connection->data())
->map(fn ($data) => ['id' => $data['user_id']])
->values();

return new JsonResponse((object) ['users' => $connections]);
}
}
17 changes: 15 additions & 2 deletions tests/Feature/Reverb/ChannelUsersControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use Laravel\Reverb\Contracts\ApplicationProvider;
use Laravel\Reverb\Contracts\ChannelManager;
use Laravel\Reverb\Tests\Connection;
use Laravel\Reverb\Tests\ReverbTestCase;
use React\Http\Message\ResponseException;

Expand All @@ -12,5 +15,15 @@
})->throws(ResponseException::class);

it('returns the user data', function () {
//
})->todo();
$channel = app(ChannelManager::class)
->for(app()->make(ApplicationProvider::class)->findByKey('pusher-key'))
->find('presence-test-channel');
$channel->subscribe($connection = new Connection('test-connection-one'), validAuth($connection, 'presence-test-channel', $data = json_encode(['user_id' => 1, 'user_info' => ['name' => 'Taylor']])), $data);
$channel->subscribe($connection = new Connection('test-connection-two'), validAuth($connection, 'presence-test-channel', $data = json_encode(['user_id' => 2, 'user_info' => ['name' => 'Joe']])), $data);
$channel->subscribe($connection = new Connection('test-connection-three'), validAuth($connection, 'presence-test-channel', $data = json_encode(['user_id' => 3, 'user_info' => ['name' => 'Jess']])), $data);

$response = await($this->signedRequest('channels/presence-test-channel/users'));

expect($response->getStatusCode())->toBe(200);
expect($response->getBody()->getContents())->toBe('{"users":[{"id":1},{"id":2},{"id":3}]}');
});
30 changes: 15 additions & 15 deletions tests/Feature/Reverb/ServerTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Laravel\Reverb\Channels\ChannelBroker;
use Laravel\Reverb\Contracts\ApplicationProvider;
use Laravel\Reverb\Contracts\ChannelManager;
use Laravel\Reverb\Contracts\ConnectionManager;
Expand Down Expand Up @@ -63,7 +63,7 @@

expect(Str::contains($response, 'pusher_internal:subscription_succeeded'))->toBeTrue();
expect(Str::contains($response, '"hash\":{\"1\":{\"name\":\"Test User\"}}'))->toBeTrue();
})->todo();
});

it('can notify other subscribers of a presence channel when a new member joins', function () {
$connectionOne = $this->connect();
Expand All @@ -82,7 +82,7 @@

expect(await($promiseOne))->toBe('{"event":"pusher_internal:member_added","data":{"user_id":2,"user_info":{"name":"Test User 2"}},"channel":"presence-test-channel"}');
expect(await($promiseTwo))->toBe('{"event":"pusher_internal:member_added","data":{"user_id":3,"user_info":{"name":"Test User 3"}},"channel":"presence-test-channel"}');
})->todo();
});

it('can notify other subscribers of a presence channel when a member leaves', function () {
$connectionOne = $this->connect();
Expand All @@ -109,7 +109,7 @@

expect(await($promiseThree))->toBe('{"event":"pusher_internal:member_removed","data":{"user_id":3},"channel":"presence-test-channel"}');
expect(await($promiseFour))->toBe('{"event":"pusher_internal:member_removed","data":{"user_id":3},"channel":"presence-test-channel"}');
})->todo();
});

it('can receive a message broadcast from the server', function () {
$connectionOne = $this->connect();
Expand Down Expand Up @@ -147,7 +147,7 @@
);

expect(await($promise))->toBe('{"event":"App\\\\Events\\\\TestEvent","data":{"foo":"bar"},"channel":"presence-test-channel"}');
})->todo();
});

it('can respond to a ping', function () {
$connection = $this->connect();
Expand Down Expand Up @@ -231,13 +231,13 @@
expect(connectionManager()->all())->toHaveCount(1);
expect(channelManager()->all())->toHaveCount(4);

$connection = connectionManager()->all()[0];
$connection = Arr::first(connectionManager()->all());

channelManager()->all()->each(function ($channel) use ($connection) {
expect($channel->connections())->toHaveCount(1);
expect(channelManager()->connectionKeys($channel)->map(fn ($conn, $index) => (string) $index))->toContain($connection->identifier());
expect(collect($channel->connections())->map(fn ($conn, $index) => (string) $index))->toContain($connection->identifier());
});
})->todo();
});

it('can subscribe multiple connections to multiple channels', function () {
$connection = $this->connect();
Expand All @@ -253,11 +253,11 @@
expect(connectionManager()->all())->toHaveCount(2);
expect(channelManager()->all())->toHaveCount(4);

expect(channelManager()->connectionKeys(ChannelBroker::create('test-channel')))->toHaveCount(2);
expect(channelManager()->connectionKeys(ChannelBroker::create('test-channel-2')))->toHaveCount(1);
expect(channelManager()->connectionKeys(ChannelBroker::create('private-test-channel-3')))->toHaveCount(2);
expect(channelManager()->connectionKeys(ChannelBroker::create('presence-test-channel-4')))->toHaveCount(1);
})->todo();
expect(channelManager()->find('test-channel')->connections())->toHaveCount(2);
expect(channelManager()->find('test-channel-2')->connections())->toHaveCount(1);
expect(channelManager()->find('private-test-channel-3')->connections())->toHaveCount(2);
expect(channelManager()->find('presence-test-channel-4')->connections())->toHaveCount(1);
});

it('fails to subscribe to a private channel with invalid auth signature', function () {
$response = $this->subscribe('private-test-channel', auth: 'invalid-signature');
Expand Down Expand Up @@ -303,8 +303,8 @@
['foo' => 'bar']
);

expect(await($promise))->toBe('{"event":"App\\\\Events\\\\TestEvent","channel":"presence-test-channel","data":{"foo":"bar"}}');
})->todo();
expect(await($promise))->toBe('{"event":"App\\\\Events\\\\TestEvent","data":{"foo":"bar"},"channel":"presence-test-channel"}');
});

it('can publish and subscribe to a client whisper', function () {
$this->usingRedis();
Expand Down
11 changes: 6 additions & 5 deletions tests/Feature/ServerTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Laravel\Reverb\Contracts\ChannelManager;
use Laravel\Reverb\Managers\Connections;
use Laravel\Reverb\Server;
use Laravel\Reverb\Tests\Connection;
use Laravel\Reverb\Tests\TestCase;
Expand Down Expand Up @@ -199,8 +198,10 @@
});

it('unsubscribes a user from a presence channel on disconnection', function () {
$this->channelManager->shouldReceive('connections')->andReturn(Connections::make());
$this->channelManager->shouldReceive('connectionKeys')->andReturn(collect());
$channelManager = Mockery::spy(ChannelManager::class);
$channelManager->shouldReceive('for')
->andReturn($channelManager);
$this->app->singleton(ChannelManager::class, fn () => $channelManager);

$this->server->message(
$connection = new Connection,
Expand All @@ -214,10 +215,10 @@

$this->server->close($connection);

$this->channelManager->shouldHaveReceived('unsubscribeFromAll')
$channelManager->shouldHaveReceived('unsubscribeFromAll')
->once()
->with($connection);
})->todo();
});

it('it rejects a connection from an invalid origin', function () {
$this->app['config']->set('reverb.apps.apps.0.allowed_origins', ['laravel.com']);
Expand Down

0 comments on commit 30e14fe

Please sign in to comment.