PHPUnit - 需要多次包含的过程代码

时间:2017-09-14 19:33:22

标签: php phpunit procedural

我目前正在尝试构建一个phpunit测试套件来测试一些程序性编写的ajax代码。 我无法编辑原始代码,fooBar.php,因为它可能会导致其他地方出现问题。当我尝试使用不同的参数多次运行php文件时,会出现此问题;代码具有抛出重新声明异常的函数。以下是我正在处理的一个例子。

fooBar.php - 使用ajax调用点击的php文件

$("#mytextfield").on('keyup',function(){
    let getValue;

    if($("#totaldays").children(":selected").attr("id") == 'a') {
        getvalue = 10;
    }

    if($("#totaldays").children(":selected").attr("id") == 'b') {
        getvalue = 20;
    }
    if($("#totaldays").children(":selected").attr("id") == 'c') {
        getvalue = 30;
    }

    var total= getvalue * $(this).val() 
    $(".total").html(total);
});

fooBarTest.php - phpunit测试文件

$foo = $_POST['bar'];

function randomFunctionName(){
    echo "randomFunctionName";
}

if($foo == "fooBar"){
     $jsonResponse = array('success'=>true);
}else{
     $jsonResponse = array('success'=>false);
}

echo json_encode($jsonResponse);

因此,当我运行此测试时,我收到以下错误

class fooBarTest extends \PHPUnit\Framework\TestCase
{   

   private function _execute() {
       ob_start();
       require 'fooBar.php';
       return ob_get_clean();
   }    

   public function testFooBarSuccess(){
       $_POST['bar'] = "fooBar";

       $response = $this->_execute();
       $this->assertTrue((strpos($response, 'success') !== false) && (strpos($response, 'true') !== false));

   }        
   public function testFooBarFailure(){
       $_POST['bar'] = "notFooBar";

       $response = $this->_execute();
       $this->assertTrue((strpos($response, 'success') !== false) && (strpos($response, 'false') !== false));

   }

问题来自这样一个事实:当第二个测试testFooBarFailure()运行时, fooBar.php 在技术上已经存在。如您所见,我需要重新运行 fooBar.php 才能获得新的响应。

有没有办法从php堆栈/内存中删除 fooBar.php ,这样我就可以再次运行它,好像它从未在第一次测试中加载一样?我已经尝试将第二个测试函数拉到自己的测试类中但是当我整个运行测试套件时,我得到了同样的错误。

2 个答案:

答案 0 :(得分:1)

所以我找到了一种方法来做我想要的。长话短说,我使用CURL来点击ajax文件。这允许我在测试中多次击中文件而不会导致任何重新声明问题。以下是我对 fooBarTest.php 文件的解决方案示例。

class fooBarTest extends \PHPUnit\Framework\TestCase
{   

   public function testFooBarSuccess(){
       $postData = array('bar'=>"fooBar");

       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 

       $response = curl_exec($ch);      
       curl_close($ch);

       $this->assertTrue((strpos($response, 'success') !== false) && (strpos($response, 'true') !== false));

   }        
   public function testFooBarFailure(){
       $postData = array('bar'=>"notFooBar");

       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 

       $response = curl_exec($ch);      
       curl_close($ch);

       $this->assertTrue((strpos($response, 'success') !== false) && (strpos($response, 'false') !== false));

    }
}

答案 1 :(得分:0)

PHP具有内置函数来检查特定函数是否已定义,它被称为function_exists。例如:

if (false === function_exists('randomFunctionName')) {
    function randomFunctionName()
    {
        echo "randomFunctionName";
    }
}

多亏了这一点,您可以多次include / require文件,但会加载一次函数。

第二种方法是使用fooBar.php代替require_onceDifference between require, include and require_once?)导入require

相关问题