phpunit
2010-09-12 81 views 0 likes 
0

我都停留在一個點相當長的一段時間

我一定要考其中的參數是從$ _POST全局數組提取功能提取。

看看一個更好的澄清

我的功能看起來像這樣

function getUsers() 
     { 

      extract($_POST); 
     $usersQry=$this->db->query("select user from user_table where org_type='".$orgType."'") 
     return $usersQry; 
} 

在上述$ orgType以下是指數在$ _POST數組。

,因爲沒有參數傳遞給函數的getUser()我不能從測試file.see傳遞參數數組下面

$testdata=$this->users_model->getUsers($orgType);// i can not go for this option in test file 

請張貼一些替代品,幫助我擺脫這種契機。

謝謝。

+2

你爲什麼不能傳遞參數getUsers? – 2010-09-12 12:17:21

+0

@Anti暗示什麼會比從課堂內的$ _POST'獲取東西要乾淨得多。如果可能的話,考慮改變程序的結構 – 2010-09-12 12:24:30

+0

是的,我已經提到過,我無法將參數從測試文件傳遞給getUsers()。即使我不能改變程序的結構,因爲我沒有編寫代碼,所以有些替代方法會很方便。 – sidhartha 2010-09-12 12:48:23

回答

1

從技術上講,在調用getUsers()之前,沒有什麼能夠阻止您在測試代碼中更改$ _POST。它只是一個數組。 $ _POST ['orgType'] =會有效果。

你也可能要啓用backupGlobals,如下所述:www.phpunit.de

1

您提供的代碼是相當馬車,所以很難進行測試。

  1. 你是無法操縱(例如假,假)$ _ POST值的方法
  2. 您允許通過extract
  3. 您傳遞未經驗證的數據到SQL查詢來設置任何變量(SQL注入)

更好的辦法是:

function getUsers($post = null) 
{ 
    if (null === $post) { 
     $post = $this->getSanitizedPost(); 
    } 

    if (isset($post['orgType']) { 
     throw new Exception('Missing required parameter…'); 
    } 

    $orgType = $post['orgType']; 
    $usersQry = $this->db->query("select user from user_table where org_type='".$orgType."'"); 

    return $usersQry; 
} 
/** 
* Assert exception… 
*/ 
function testHasRequiredParamter() 
{ 
    $post = array('param1'=>'val1'); 
    $users = $this->tested->getUsers($post); 
    ... 
} 
相關問題