使用不同的系统 ini 设置进行测试
好的,这就是我遇到的问题。在我们的一些生产系统上,我们启用了 Magic Quotes gpc。我对此无能为力。因此,我构建了请求数据处理类来进行补偿:
protected static function clean($var)
{
if (get_magic_quotes_gpc()) {
if (is_array($var)) {
foreach ($var as $k => $v) {
$var[$k] = self::clean($v);
}
} else {
$var = stripslashes($var);
}
}
return $var;
}
我在该方法中做了一些其他事情,但这不是问题。
因此,我目前正在尝试为该方法编写一组单元测试,但遇到了障碍。如何根据 get_magic_quotes_gpc()
的结果测试两个执行路径?我无法在运行时修改 ini 设置(因为它已经加载)...我尝试搜索 PHPUnit 文档,但找不到与此类问题相关的任何内容。我在这里缺少什么吗?或者我是否必须忍受无法测试所有可能的代码执行路径?
谢谢
Ok, so here's the issue I've run into. On some of our production systems, we have magic quotes gpc enabled. There's nothing I can do about that. So, I've built my request data handing classes to compensate:
protected static function clean($var)
{
if (get_magic_quotes_gpc()) {
if (is_array($var)) {
foreach ($var as $k => $v) {
$var[$k] = self::clean($v);
}
} else {
$var = stripslashes($var);
}
}
return $var;
}
I do some other things in that method, but that's not an issue.
So, I'm currently trying to write a set of Unit-Tests for that method, and I've run into a road bock. How can I test both execution paths with respect to the result of get_magic_quotes_gpc()
? I can't modify the ini settings at run time for that (because it's already loaded)... I've tried searching the PHPUnit docs, but I can't find anything related to this type of issue. Is there something that I'm missing here? Or will I have to live with being unable to test all possible code execution paths?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对此不是 100% 确定,但我认为 magic_quotes_gpc 只是意味着所有字符串都已应用
addslashes()
。因此,要模拟 magic_quotes_gpc,您可以递归地对$_GET
、$_POST
和$_COOKIE
数组应用addslashes。这并不能解决get_magic_quotes_gpc()
将返回 false 的问题 - 您只需将get_magic_quotes_gpc()
替换为true
即可我想,进行适当的单元测试。编辑:如 http://www.php.net/manual/en 中所述/function.addslashes.php
'PHP 指令 magic_quotes_gpc 默认情况下处于启用状态,它本质上对所有 GET、POST 和 COOKIE 数据运行addslashes()。'
I'm not 100% certain about this, but I think magic_quotes_gpc just means that all strings have had
addslashes()
applied to them. So to emulate having magic_quotes_gpc, you can apply addslashes recursively to the$_GET
,$_POST
and$_COOKIE
arrays. That doesn't solve the fact thatget_magic_quotes_gpc()
will be returning false - you'll just have to replaceget_magic_quotes_gpc()
withtrue
when doing the appropriate unit test, I guess.Edit: As stated in http://www.php.net/manual/en/function.addslashes.php
'The PHP directive magic_quotes_gpc is on by default, and it essentially runs addslashes() on all GET, POST, and COOKIE data.'
一个可能的(但不是完美的)解决方案是将 get_magic_quotes_gpc() 的值作为参数传递,例如:
Ofc 这有一个缺点......嗯,很丑,但是 ini 设置和定义总是很难测试,这就是为什么你应该尽量避免它们。避免直接使用它们的一种方法是:
A possible (but not perfect) solution would be to pass the value of get_magic_quotes_gpc() as parameter, like:
Ofc this has the disadvantage of... well, being ugly, but ini settings and defines are always horrible to test, thats why you should try to avoid them. One way to avoid using them directly would be:
好吧,我遇到了一个解决方法...
在构造函数中,我调用
get_magic_quotes_gpc()
:然后,为了测试,我只是将其子类化,然后提供一个公共方法来手动设置
$this->magicQuotes
。它并不是很干净,但它很好,因为它节省了每次递归时函数调用的开销......Well, I came across a workaround...
In the constructor, I call
get_magic_quotes_gpc()
:Then, for testing, I just sub-class it, and then provide a public method for manually setting
$this->magicQuotes
. It's not really clean, but it's nice since it saves the overhead of a function call on each recursion...