How one can phpunit subscriptions using only PHP/phpunit/Laravel and laravel-lighthouse GraphQL library?
I am trying to create a unit test for my app that will subscribe to a "hello world" subscription. The purpose of such test is to assert the subscription feature. The feature test should listen to a channel and assert the received message contains the same published message (that was published in he same phpunit file).
This is what I have so far:
public function test_hello_world_build_subscription()
{
$config = config('broadcasting.connections.pusher');
$graphql = app(\Nuwave\Lighthouse\GraphQL::class);
$context = app(\Nuwave\Lighthouse\Schema\Context::class);
$result = $graphql->executeQuery('
subscription BuildEvent($input: BuildEventInput!) {
buildEvent (input: $input) {
message
}
}
', $context, [
'input' => [
"uuid" => "0a53b12e-7ae0-4db1-a030-26a7ffdb1902",
]
],
);
$channel = data_get($result, 'extensions.lighthouse_subscriptions.channel');
$pusher = new Pusher($config['key'], $config['secret'], $config['app_id'], [
'host' => 'localhost',
'port' => 6001,
'debug' => true,
]);
$pusher->trigger($channel, 'App\Events\ExampleEvent', 'a hello world message');
}
I am glad it worked and returned something like this:
GraphQL\Executor\ExecutionResult^ {#2045
+data: array:1 [
"buildEvent" => null
]
+errors: []
+extensions: array:1 [
"lighthouse_subscriptions" => array:2 [
"version" => 2
"channel" => "private-lighthouse-0IIVzatBhabTtfEe33vp9ARBHGa1iP1y-1626402747"
]
]
-errorFormatter: null
-errorsHandler: null
}
But I dont know what to do next.
The documentation did not talk anything about it. Could anyone point me to a direction? Any link for a blog, git repo, tutorial, etc. would be appreciated. Thanks.