在 Zend Framework 应用程序上使用 PHPUnit,如何强制将语言环境用于给定测试?

发布于 2024-09-24 04:44:05 字数 180 浏览 1 评论 0原文

我正在使用 phpunit 测试一个多语言网站。我想要执行的测试之一是应用程序将检测用户的区域设置并自动重定向。

即用户访问/上的站点。该应用程序检测到它们来自法国并重定向到 /fr-FR/

该应用程序似乎确实执行了此操作,但尝试为此编写单元测试似乎是不可能的。为了测试的目的,我需要伪造语言环境。有人可以建议吗?

I am testing a multi-lingual site with phpunit. One of the tests I want to perform is that the application will detect the locale of the user and automatically redirect.

That is, user accesses the site on /. The application detects they're from France and redirects to /fr-FR/

The application does appear to do this, but trying to write a unit test for this seems impossible. I need to forge the locale for the purpose of the test. Can anyone advise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷血 2024-10-01 04:44:05

解决方案是使代码本身更易于测试。

在我的测试中,我有:

    $locale = new Zend_Locale();

    $locale->setLocale('fr_FR');
    Zend_Registry::set('Zend_Locale', $locale); 

现在在我的应用程序中我使用:

    $locale = Zend_Locale::findLocale(); 
    $locale = new Zend_Locale($locale);

设置区域设置。 findLocale 首先检查 Zend_Registry 中的条目。

The solution has been to make the code itself more testable.

In my test I have:

    $locale = new Zend_Locale();

    $locale->setLocale('fr_FR');
    Zend_Registry::set('Zend_Locale', $locale); 

And now in my app I use:

    $locale = Zend_Locale::findLocale(); 
    $locale = new Zend_Locale($locale);

To set the locale. As findLocale checks the Zend_Registry for an entry first.

雾里花 2024-10-01 04:44:05

它可能正在查看 Accept-Language 标头浏览器发送。您可以使用 $_SERVER['HTTP_ACCEPT_LANGUAGE'] 在 PHP 中访问它。它是全局的,因此在您的测试设置中,您可以更改其值:

$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en";

It's probably looking at the Accept-Language header that the browser sends. You can access this in PHP using $_SERVER['HTTP_ACCEPT_LANGUAGE']. It's a global, so in your test setup, you could change its value:

$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文