2017-09-14 6 views
2

현재 절차 적으로 작성된 일부 아약스 코드를 테스트하기 위해 phpunit testsuite를 구축하려고합니다. 원래 코드, fooBar.php을 편집 할 수 없습니다. 다른 곳에서 문제가 발생할 수 있기 때문입니다. 이 문제는 여러 매개 변수로 PHP 파일을 여러 번 실행하려고 할 때 발생합니다. 코드에는 재 선언 예외를 throw하는 함수가 있습니다. 아래는 제가 다루고있는 예입니다.PHPUnit - 여러 번 포함해야하는 절차 코드

fooBar.php - 아약스 호출

$foo = $_POST['bar']; function randomFunctionName(){ echo "randomFunctionName"; } if($foo == "fooBar"){ $jsonResponse = array('success'=>true); }else{ $jsonResponse = array('success'=>false); } echo json_encode($jsonResponse); 

fooBarTest.php 명중 PHP 파일 -이 테스트를 실행할 때 phpunit을 테스트 파일

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을 다시 실행해야합니다.

fooBar.php을 PHP 스택/메모리에서 제거 할 수 있습니까? 그렇듯이 첫 번째 테스트에서로드 된 적이없는 것처럼 다시 실행할 수 있습니까? 두 번째 테스트 함수를 자체 테스트 클래스로 가져 오려고했지만 테스트 스위트를 전체적으로 실행하면 똑같은 오류가 발생합니다.

답변

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)); 

    } 
} 
0

PHP에는 특정 기능이 정의되어 있는지 확인하는 기능이 내장되어 있으며 function_exists이라고합니다. 예 : 이것에

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

감사합니다, 당신은 include/require 파일을 여러 번 있지만 기능은 한 번에로드 할 수 있습니다.

두 번째 방법은 (Difference between require, include and require_once?) 대신 fooBar.php 번을 require_once 대신 한 번 가져 오기만하면됩니다.

+0

제안 사항 중 어느 것도 작동하지 않습니다. 함수가 존재하는지 확인하는 것은 원래 fooBar.php를 변경하는 것입니다. 앞에서 언급했듯이 원래 코드를 변경할 수는 없습니다. PHPUnit 테스트 파일에서 파일을 새로 고침하여 새로운 정보를 보내야합니다. 한 번 요구하거나 포함 시키면 그렇게 할 수 없습니다. –