Rudy
Rudy

Reputation: 7044

Unit Testing with CodeIgniter's post

I have this lines of code inside controller, written using codeigniter framework.

public function search($bookmarkIndex=0)
{
    $searchString = trim($this->input->post("searchString"));
    $searchType = trim($this->input->post("searchType"));

    $search = array(
            'searchString'=>$searchString,
            'type'=>$searchType);

    // DOING SEARCH HERE...
}

As you can see, the method is using CodeIgniter's $this->input->post. I found this is hard to be unit-tested. How should I set the value if I need it tested using PHPUnit? Or is there way to mock this? Below is my current unit test method.

// inside PHPUNIT TEST FOLDER
public function test_search()
{
    // I know this is not the way to set "post", below is just my 
    // expectation if I were able to set it. 
    $this->CI->input->post("searchString",'a');
    $this->CI->input->post("searchType",'contact');

    $searchString = $this->CI->input->post("searchString");
    echo $searchString; //always false.
    $this->CI->search();
    $out = output();
    // DO ASSERT HERE... 
}

Upvotes: 1

Views: 2188

Answers (1)

Rudy
Rudy

Reputation: 7044

Change the unit test to below, using $POST and it's working fine.

public function test_search()
{

    $_POST["searchString"] = 'a';
    $_POST["searchType"] = 'contact';

    $this->CI->search();
    $out = output();

    $searchString = $this->CI->input->post("searchString");
    echo $searchString;
}

Upvotes: 2

Related Questions