使用不同的系统 ini 设置进行测试

发布于 2024-09-09 05:51:41 字数 638 浏览 5 评论 0原文

好的,这就是我遇到的问题。在我们的一些生产系统上,我们启用了 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 技术交流群。

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

发布评论

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

评论(3

怀中猫帐中妖 2024-09-16 05:51:41

我对此不是 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 that get_magic_quotes_gpc() will be returning false - you'll just have to replace get_magic_quotes_gpc() with true 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.'

夏日浅笑〃 2024-09-16 05:51:41

一个可能的(但不是完美的)解决方案是将 get_magic_quotes_gpc() 的值作为参数传递,例如:

protected static function clean($var, $magic_quotes = null)
{
  if ($magic_quotes === null) $magic_quotes = get_magic_quotes_gpc();
  do_stuff();
}

Ofc 这有一个缺点......嗯,很丑,但是 ini 设置和定义总是很难测试,这就是为什么你应该尽量避免它们。避免直接使用它们的一种方法是:

class Config
{
  private static $magic_quotes = null;

  public static GetMagicQuotes()
  {
    if (Config::$magic_quotes === null)
    {
      Config::$magic_quotes = get_magic_quotes_gpc();
    }
    return Config::$magic_quotes;
  }

  public static SetMagicQuotes($new_value)
  {
    Config::$magic_quotes = $new_value;
  }
}

[...somewhere else...]

protected static function clean($var)
{
  if (Config::GetMagicQuotes())
  {
    do_stuff();
  }
}

[... in your tests...]


public test_clean_with_quotes()
{
  Config::SetMagicQuotes(true);
  doTests();
}

public test_clean_without_quotes()
{
  Config::SetMagicQuotes(false);
  doTests();
}

A possible (but not perfect) solution would be to pass the value of get_magic_quotes_gpc() as parameter, like:

protected static function clean($var, $magic_quotes = null)
{
  if ($magic_quotes === null) $magic_quotes = get_magic_quotes_gpc();
  do_stuff();
}

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:

class Config
{
  private static $magic_quotes = null;

  public static GetMagicQuotes()
  {
    if (Config::$magic_quotes === null)
    {
      Config::$magic_quotes = get_magic_quotes_gpc();
    }
    return Config::$magic_quotes;
  }

  public static SetMagicQuotes($new_value)
  {
    Config::$magic_quotes = $new_value;
  }
}

[...somewhere else...]

protected static function clean($var)
{
  if (Config::GetMagicQuotes())
  {
    do_stuff();
  }
}

[... in your tests...]


public test_clean_with_quotes()
{
  Config::SetMagicQuotes(true);
  doTests();
}

public test_clean_without_quotes()
{
  Config::SetMagicQuotes(false);
  doTests();
}
丶视觉 2024-09-16 05:51:41

好吧,我遇到了一个解决方法...

在构造函数中,我调用 get_magic_quotes_gpc()

protected $magicQuotes = null;

public function __construct() {
    $this->magicQuotes = get_magic_quotes_gpc();
}

protected function clean($var) {
    if ($this->magicQuotes) {
        //...
    }
}

然后,为了测试,我只是将其子类化,然后提供一个公共方法来手动设置 $this->magicQuotes。它并不是很干净,但它很好,因为它节省了每次递归时函数调用的开销......

Well, I came across a workaround...

In the constructor, I call get_magic_quotes_gpc():

protected $magicQuotes = null;

public function __construct() {
    $this->magicQuotes = get_magic_quotes_gpc();
}

protected function clean($var) {
    if ($this->magicQuotes) {
        //...
    }
}

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...

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