简体   繁体   中英

Returning Different Results in PHPUnit Mock Object

I've been working on getting our systems more compatible with PHPUnit so we can do more unit testing of our classes and have managed to get some of them working with mock objects, but I've run across a problem which I can't seem to get around.

One of the classes we have (which I'm creating a mock version of) is for queries. You pass the query string into it's "query()" method, it logs the query, runs it and returns the result. It also wraps the mysql_fetch_assoc with a method called "get_row()", which returns an array value much like the original.

The problem is that, in some methods, there's more than one query being passed to the "query()" method and as a result it needs to run through multiple while loops to load the data into different variables. I've created a simplified version below:

class object{
    public function __construct($query){
        $this->query = $query;
    }

    public function loadData(){

        $data1 = queryDataSource("SELECT * FROM data1");
        $data2 = queryDataSource("SELECT * FROM data2");

        return Array(
            "data1" => $data1,
            "data2" => $data2,
        );

    }

    private function queryDataSource($query){
        $this->query->query($query)

        while($row = $this->query->get_row()){
            $result[] = $row;
        }

        return $result
    }
}

class testObject extends PHPUnit_Framework_TestCase{
    method testLoadData(){
        $test_data = Array('name' => 'Bob', 'number' => '98210');

        $query = $this->getMock('Query');
        $query->expects($this->any())->method('query');
        $query->expects($this->at(1))->method('get_row')->will($this->returnValue($test_data);
        $query->expects($this->at(2))->method('get_row')->will($this->returnValue(False);
        $query->expects($this->at(3))->method('get_row')->will($this->returnValue($test_data);
        $query->expects($this->at(4))->method('get_row')->will($this->returnValue(False);
    }
}

In order to escape the first while loop in $object->queryDataSource() I'm returning a boolean FALSE value, as would happen when doing mysql_fetch_assoc. The problem is that, when it tries to run the second query and fetch the data through get_row(), the mock object seems to keep returning FALSE ratehr than moving on to the at(3) point. This happens even with 4 objects, only the first will get the test data as a return value then get FALSE the second time, the others will get FALSE every time.

Does anyone know if there's a way to get around this? I tried removing the FALSE flags and just having the odd values in at(), but that had the same problem, and I tried just having it return the data for at(1-2), but that just passed all the data into the first while loop and nothing for the other.

Thanks for any help you can give, hope the description of the problem's clear enough

I can't run the code as it seems to only be pseudocode but from what I understood is that you are trying to mock like this:

Call to query, get_row, get_row, query, get_row, get_row.

The issue you seem to have run into is that the number in the ->at() matcher doesn't count up per method but per object .

So what you probably want to write is:

    $query->expects($this->any())->method('query');
    $query->expects($this->at(1))->method('get_row')->will($this->returnValue($test_data);
    $query->expects($this->at(2))->method('get_row')->will($this->returnValue(False);
    $query->expects($this->at(4))->method('get_row')->will($this->returnValue($test_data);
    $query->expects($this->at(5))->method('get_row')->will($this->returnValue(False);

Or to make it a litte easer to read maybe even:

    $query->expects($this->at(0))->method('query');
    $query->expects($this->at(1))->method('get_row')->will($this->returnValue($test_data);
    $query->expects($this->at(2))->method('get_row')->will($this->returnValue(False);
    $query->expects($this->at(3))->method('query');
    $query->expects($this->at(4))->method('get_row')->will($this->returnValue($test_data);
    $query->expects($this->at(5))->method('get_row')->will($this->returnValue(False);

With your mocks you ran into the issue that the second call to "query" was counting up one "call" and hence skipping over the second return($test_data); .

Unfortunately the at() binds your tests to the implementations very strongly.

Imagine if you rearranged 2 method calls inside a tested method, the functionality is exactly the same but all tests using at() would now fail, often with cryptic messages such as method doesn't exist at index N

On the occasions you want to specifically say "this called exactly this way then this called exactly this way" that's great, but if you just want assertions then one of the PHPUnit Mock Extensions seems more friendly, particularly Mockery and a guide here (a touch out of date I believe)

There are others also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM