【问题标题】:Is there a way to pass callbacks as constructor arguments in Symfony YAML files?有没有办法在 Symfony YAML 文件中将回调作为构造函数参数传递?
【发布时间】:2020-08-05 13:33:35
【问题描述】:

对于 CI 环境,我会覆盖 HTTP 客户端的响应。

这行得通:

symfony.mock_http_response:
    class: Symfony\Component\HttpClient\Response\MockResponse
    arguments:
        - '{
               "data": "some response data"
           }'


Symfony\Contracts\HttpClient\HttpClientInterface:
    class: Symfony\Component\HttpClient\MockHttpClient
    arguments:
        - '@symfony.mock_http_response'

但是我喜欢选择带有回调的动态方法,记录在这里: https://symfony.com/doc/current/http_client.html#testing-http-clients-and-responses

这样我希望能够根据我正在运行的 Behat 测试定义不同的响应。

问题是:如何在 YAML 文件中将回调函数作为构造函数参数传递给 MockHttpClient?

【问题讨论】:

    标签: symfony yaml behat symfony5


    【解决方案1】:

    你不能直接。即使您在 php 中声明您的服务,如果服务定义中存在对象,则无法转储容器。

    但是,您可以通过创建回调并将其注入模拟客户端实例的工厂来创建服务。

    【讨论】:

      猜你喜欢
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2011-07-28
      • 2011-02-10
      • 2017-07-10
      • 1970-01-01
      • 2011-09-04
      相关资源
      最近更新 更多