php - Test that method is called with some parameters, among others -


i'm testing method phpunit , have following scenario:

  • method 'setparameter' called unkown amount of times
  • method 'setparameter' called different kinds of arguments
  • among various arguments method 'setparameter' must called set of arguments.

i've tried doing way:

$mandatoryparameters = array('param1', 'param2', 'param3'); foreach ($mandatoryparameters $parameter) {     $class->expects($this->once())         ->method('setparameter')         ->with($parameter); } 

unfortunately test failed because before method called these parameters called other parameters too. error is:

parameter 0 invocation namespace\class::setparameter('random_param', 'random_value') not match expected value. failed asserting 2 strings equal. 

try using $this->at() method. overwriting mock each time loop.

$mandatoryparameters = array('param1', 'param2', 'param3'); $a = 0; foreach ($mandatoryparameters $parameter) {     $class->expects($this->at($a++);         ->method('setparameter')         ->with($parameter); } 

this set mock expect setparameter called number of times , each call different parameter. need know call specific on parameters , adjust number accordingly. if calls not sequential, can set key index each param.

$mandatoryparameters = array(2 =>'param1', 5 => 'param2', 6 => 'param3');  foreach ($mandatoryparameters $index => $parameter) {     $class->expects($this->at($index);         ->method('setparameter')         ->with($parameter); } 

the index 0 based remember start counting 0 rather 1.

http://phpunit.de/manual/current/en/phpunit-book.html#test-doubles.mock-objects.tables.matchers


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -