如何在 symfony 1.4 的功能性石灰测试中覆盖 sfConfig 设置

发布于 2024-12-17 01:28:05 字数 380 浏览 7 评论 0原文

我的问题如下,我想测试一些依赖于 sfConfig 中特定设置的功能。 建议设置为 app_foo,默认值为 false

测试的“标准”方法是这样的:

$browser = new sfTestFunctional(new sfBrowser());

$browser->
  get('/path_to/index')->
  ...
  end();

但是改变设置是行不通的

sfConfig::set('app_foo', true);
$browser = new sfTestFunctional(new sfBrowser());

My Problem is the following i wanted to test some functionality that depended on a certain setting in the sfConfig.
Proposed the setting is app_foo and its default is false.

The "standard" way to test is like:

$browser = new sfTestFunctional(new sfBrowser());

$browser->
  get('/path_to/index')->
  ...
  end();

BUT it does not work to change the setting like

sfConfig::set('app_foo', true);
$browser = new sfTestFunctional(new sfBrowser());

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

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

发布评论

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

评论(2

帅冕 2024-12-24 01:28:05

sfConfig 在测试中无法被覆盖,因为 sfConfig 会在下一个请求时重置。

但是浏览器实例中名为“rawConfiguration”的字段将添加到请求的 sfConfig 中。所以解决方案是设置这个数组:

$browserEngine = new sfBrowser();
$browser = new sfTestFunctional($browserEngine);
// overwrite an sfConfig parameter in the browser context
$browserEngine->rawConfiguration = array_merge($browserEngine->rawConfiguration, array('app_foo' => true));

然后操作知道 sfConfig::get('app_foo') 的值为 true。

The sfConfig cannot be overwritten in the test because the sfConfig is reset on the next request.

BUT a field named 'rawConfiguration' in the browser instance is added to the sfConfig for the request. So the solution is to set this array:

$browserEngine = new sfBrowser();
$browser = new sfTestFunctional($browserEngine);
// overwrite an sfConfig parameter in the browser context
$browserEngine->rawConfiguration = array_merge($browserEngine->rawConfiguration, array('app_foo' => true));

Then the action knows the sfConfig::get('app_foo') with the value true.

夏日浅笑〃 2024-12-24 01:28:05

samura 的答案很好,您还必须知道配置文件是环境感知的,因此您可以在 app.yml 中执行此操作:

all:
  my_setting: "super"

test:
  my_setting: "awesome"

这也适用于 database.yml 文件,允许您在另一个数据库中运行功能测试

The samura answer is nice, you also have to know that config files are environement aware so you can do that in your app.yml:

all:
  my_setting: "super"

test:
  my_setting: "awesome"

This is also working on the database.yml file, allowing you to run functionnal tests in another database

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