phpUnit - Functions in test class that aren't tests

Refresh

April 2019

Views

625 time

1

I'm doing some speed comparisons between reports in phpunit, as I am trying to figure out an optimization problem.

I have a couple of functions that aren't necessarily tests, but don't belong in the functionality of the project either. I am using them in order to make my tests small and readable. The function I am using does a cUrl operation with the parameters I pass to it.

So, I am running two Urls (two versions of a project, one in its original form, and one with the optimization) and seeing if they return text equal to each other. I would not do this within the app itself. I'm doing this because its quicker than trying to figure out the correct function calls because the project is a bit messy.

So I have a test like this:

public function testOne(){

    $results = $this->testRange(13,1,2013,16,1,2013);
    $this->assertEquals($results['opt'], $results['non_opt']);

}//tests

And my two non test functions:

protected function testRange($fromDay,
                          $fromMonth,
                          $fromYear,
                          $toDay,
                          $toMonth,
                          $toYear){

    $this->params['periodFromDay'] = $fromDay;
    $this->params['periodFromMonth'] = $fromMonth;
    $this->params['periodFromYear'] = $fromYear;
    $this->params['periodToDay'] = $toDay;
    $this->params['periodToMonth'] = $toMonth;
    $this->params['periodToYear'] = $toYear;

    $this->data['from']=$fromDay."-".$fromMonth."-".$fromYear;
    $this->data['to']=$toDay."-".$toMonth."-".$toYear;;

    return $this->testRunner();

}//testOneDay


protected function testRunner(){

    //include"test_bootstrap.php";
    $response = array();

    foreach($this->types as $key=>$type){

        $params = http_build_query($this->params);
        $url=$this->paths[$type];
        $curl_url = $url."?".$params;
        $ch = curl_init($curl_url);
        $cookieFile = "tmp/cookie.txt";

        if(!file_exists($cookieFile))
        {

            $fh = fopen($cookieFile, "w");
            fwrite($fh, "");
            fclose($fh);

        }//if

        curl_setopt($ch,CURLOPT_COOKIEFILE,$cookieFile);
        curl_setopt($ch,CURLOPT_COOKIEJAR,$cookieFile);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

        $result[$type] = curl_exec($ch);

        $dump = "logs/report_results/".
                 $this->data['from']."_".
                 $this->data['to']."_".
                 $type.".txt";

        $fh = fopen($dump, "w");
        fwrite($fh, $result[$type]);
        fclose($fh);

    }//foreach

    return $result;

}//testRunner

I'm wondering if

A: it is possible to write functions in the test file, and have phpunit ignore them, or if there is a more appropriate place to put them.

B: there is a more sensible way to handle this sort of thing. I like this approach, but I am open to suggestions.

1 answers

4

PHPUnit will ignore any method whose name does not start with "test*" and do not have a @Test annotation so feel free to put stuff in private helper functions.