简体   繁体   English

可以在Symfony2单元测试中更改语言环境吗?

[英]Is it possible to change locale in Symfony2 unit tests?

I created a Locale Switcher for my project in symfony2 which consists in one route, and one method. 我在symfony2中为我的项目创建了一个语言环境切换器,它包含一种途径和一种方法。 The method get the locale given via GET, and if it fits the constraints, switch from the current locale to the one given. 该方法通过GET获取给定的语言环境,如果适合约束,则从当前语言环境切换到给定的语言环境。 It works well. 它运作良好。

My problem occurs when i want to write some unit tests concerning that Locale Switcher. 当我想编写有关该区域设置切换器的一些单元测试时,会发生我的问题。

Even when my testing client get redirected (which means the locale switcher got called), the locale doesn't seems to change. 即使我的测试客户端被重定向(这意味着调用了语言环境切换器),语言环境似乎也没有改变。

Firstly, is it possible to change the locale in unit tests ? 首先,可以在单元测试中更改语言环境吗? And Secondly, how can i get the current locale since i can't get the Request object in my testing method ? 其次,由于我无法在测试方法中获取Request对象,因此如何获取当前语言环境?

Thanks 谢谢

Well, i found the solution. 好吧,我找到了解决方案。 I tried to access the Request object by creating one and hoping it'll be filled in the testing process. 我试图通过创建一个请求对象来访问它,并希望在测试过程中将其填充。 But, the request is stocked in the $client during unit tests. 但是,在单元测试期间,该请求将存储在$ client中。

So, to access the request object during units tests, simply do: 因此,要在单元测试期间访问请求对象,只需执行以下操作:

$client = static::createClient();
$client->getRequest()->getLocale();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM