PHPUnit with array matching and $ this-> anything ()

I have unit test testing a call PDOStatement::execute()with date()as one of the elements of an array.

Sort of:

$stmt->execute(array ('value1', 'value2', date('Ymd'));

The problem is that my statement uses $this->anything()date functions to represent the result. I think it breaks because it is in an array. Is there a good way to handle this?

My statement is as follows:

$mock->expects($this->once())
  ->method('execute')
  ->with(array ('value1', 'value2', $this->anything()));
+4
source share
1 answer

with() . PHPUnit . with() , .

, . , :

$mock->expects($this->once())
     ->method('execute')
     ->with($this->callback(function($array) {
            return 'value1' == $array[0] && 'value2' == $array[1] && 3 == count($array);
        }));

PHPUnit docs.

+3

Source: https://habr.com/ru/post/1570209/


All Articles