Search code examples
unit-testingphpunitphpmagic-quotes-gpc

Testing with varying system ini settings


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


Solution

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