如何在 laravel-lighthouse 中进行 phpunit 订阅?

时间:2021-07-16 02:25:35

标签: php graphql laravel-lighthouse

如何仅使用 PHP/phpunit/Laravel 和 laravel-lighthouse GraphQL 库进行 phpunit 订阅?

我正在尝试为我的应用程序创建一个单元测试,该单元测试将订阅“hello world”订阅。此类测试的目的是断言订阅功能。功能测试应该监听一个频道并断言接收到的消息包含相同的发布消息(发布在同一个 phpunit 文件中)。

这是我目前所拥有的:

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');
    
}

我很高兴它有效并返回如下内容:

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
}

但我不知道接下来要做什么。

文档没有谈论它。有人能指点我一个方向吗? 任何博客、git repo、教程等的链接将不胜感激。谢谢。

0 个答案:

没有答案