【发布时间】:2015-02-20 15:29:24
【问题描述】:
目前我正在测试 Symfony2 中的一些服务,我正在尝试使用 Guzzle MockPlugin 来控制 CURL 响应。使用 Symfony 2.3.8 版本。我遇到了一个有趣的行为,我不确定这是否是 Symfony2 错误。
我在 services.yml 中有这些服务:
lookup_service_client:
class: FOO
public: false
factory_service: lookup_client_builder
factory_method: build
lookup_repository_auth_type:
class: AuthType
arguments: ["@lookup_service_client"]
lookup_repository_cancel_reason:
class: CancelReason
arguments: ["@lookup_service_client"]
payment_service_client:
class: FOO
public: false
factory_service: payment_client_builder
factory_method: build
payment_repository:
class: Payment
arguments: ["@payment_service_client"]
类的名称并不重要。可以看到“lookup_service_client”和“lookup_service_client”都是PRIVATE服务。
我有一个测试类,它扩展了 Symfony\Bundle\FrameworkBundle\Test\WebTestCase。在一项测试中,我需要执行以下操作:
$lookup = $this->client->getContainer()->get('lookup_service_client');
$payment = $this->client->getContainer()->get('payment_service_client');
我预计,将这些服务设置为 PRIVATE,不会让我在测试中从容器中检索服务,但实际结果是:
$lookup = $this->client->getContainer()->get('lookup_service_client'); => returns the service instance
$payment = $this->client->getContainer()->get('payment_service_client'); => returns an exception saying: "You have requested a non-existent service"
这两个 service_client 服务之间的唯一区别是“lookup_service_client”被注入到其他几个服务中,而“payment_service_client”只被注入到一个其他服务中。
所以,问题是:
为什么我可以从容器“lookup_service_client”中检索,因为我已将其设置为私有?
为什么我可以检索“lookup_service_client”,但无法检索“payment_service_client”,因为上面给出了唯一的区别?
是我可以访问私有服务的 Symfony2 错误吗?
【问题讨论】:
标签: php symfony dependency-injection phpunit private