-1

我正在编写一个简单的测试用例来操纵各种变量组以实现一些特定的结果,但我需要“第三只眼睛”来查看我做错了什么。我没有得到如下所述的预期结果。

Sample Code:

$this->_default['method'] = 'index';

// Input-
// Group one var Option:
$pMethod = 'mission';
$arg1    = 'NULL';
$arg2    = 'NULL';
$arg3    = 'NULL';

// Group two var Option:
$pMethod = 'mission-statements-p_2';;
$arg1    = 'NULL';
$arg2    = 'NULL';
$arg3    = 'NULL';

// Group three var Option:
$pMethod = 'mission';
$arg1    = 'mission-statements-p_2';
$arg2    = 'NULL';
$arg3    = 'NULL';


if ($pMethod && $pMethod !== $this->_default['method'])
{

$find = 'p_';
$pos1  = strpos($pMethod, $find);
$pos2  = strpos($arg1, $find);


if ($pos1 !== false)
{ 
    list($arg1,$arg2)= explode("-p_",$pMethod);
    $method = 'page';
    $arg1 = !empty($arg1) ? $arg1 : NULL;
    $arg2 = !empty($arg2) ? $arg2 : NULL;
    $arg3 = NULL;

}
else if ($pos1 === false && $pos2 !== false)
{
    list($arg2,$arg3)= explode("-p_",$arg1);
    $method = 'page';
    $arg1 = $pMethod;
        $arg2 = !empty($arg2) ? $arg2 : NULL;
        $arg3 = !empty($arg3) ? $arg3 : NULL;
    }
}
else 
{
$method = $pMethod;
    $arg1 = !empty($arg1) ? $arg1 : NULL;
    $arg2 = !empty($arg2) ? $arg2 : NULL;
    $arg3 = NULL;
}


// The expecting output results should be:

// Group one var Option: no changes

// Group two var Option:
$pMethod = 'page';
$arg1    = 'mission-statements';
$arg2    = 2;
$arg3    = 'NULL';

// Group three var Option:
$pMethod = 'page';
$arg1    = 'mission';
$arg2    = 'mission-statements';
$arg3    = 2;
4

1 回答 1

1

您正在测试!empty($arg1)where $arg1was defined as $arg1 = 'NULL';,它不会评估为 empty,因为它是不被视为空的字符串'NULL',而不是NULL常量,即。

要解决此问题,请更改您的定义以省略字符串:

$arg1 = NULL; 
// etc...

您也不需要重复这些定义,您主要是用它们拥有的值覆盖它们:

// Input-
// Group one var Option:
$pMethod = 'mission';
$arg1    = 'NULL';
$arg2    = 'NULL';
$arg3    = 'NULL';

// Group two var Option:
$pMethod = 'mission-statements-p_2';;
$arg1    = 'NULL'; // These
$arg2    = 'NULL'; // Are
$arg3    = 'NULL'; // Redundant
于 2013-06-13T13:53:28.850 回答