Page 1 of 1

phpunit Mockobject weirdness

Posted: Tue Jun 28, 2011 8:44 am
by MrJingles
Hi

I'm getting pretty werid behavior when using phpunit's mockobjects to test for function calls with certain arguments:

Code: Select all

public function testSetStatusAndMaskedFetch() {
		//create mock for database abstraction objects
		$objOfferData = $this->getMock('OfferData', array('updateFieldById', 'getMetadataAsArrayByMask'), array(&$this->config, &$this->dbHandle));
		
		//set expectation
		/** set expectation for updateFieldById **/
		
		$objOfferData ->expects($this->once())
					  ->method('getMetadataAsArrayByMask')
					  ->with($this->equalTo('0-1-1', '99994', 9, true, true, 8, 8, 8, 88, 4566));
		
		/** run function which should call $objOfferData->getMetadataAsArrayByMask **/
	}
The strange thing is, that getMetadataAsArrayByMask has the following signature:

Code: Select all

getMetadataAsArrayByMask($dateFrom, $dateTo, $companyNameIn, $status, $orderBy)
- I don't use any type hints anywhere.

Now here's the things I do not get:
1. why does the test run through, even if the number of arguments do not match (also the values of the arguments do not match, they should by ('0000-0-0', '9999-99-99', '('company1'), array(of boolean), 'id ASC'))
2. why does it fail if I change the second parameter to 'a99994' ?
The reason for failing is: InvalidArgumentException: Argument #2 of PHPUnit_Framework_Constraint_IsEqual:__construct() is no numeric

Re: phpunit Mockobject weirdness

Posted: Tue Jun 28, 2011 8:59 am
by John Cartwright

Code: Select all

->with($this->equalTo('0-1-1', '99994', 9, true, true, 8, 8, 8, 88, 4566));
Should be

Code: Select all

->with(
   $this->equalTo('0-1-1'), 
   $this->equalTo('99994'),
   $this->equalTo(9),
   $this->equalTo(true), 
   $this->equalTo(true), 
   $this->equalTo(8), 
   $this->equalTo(8), 
   $this->equalTo(8), 
   $this->equalTo(88), 
   $this->equalTo(4566)
);
Each parameter is tested individually, and even some can be omitted if you do not care about checking them.

Re: phpunit Mockobject weirdness

Posted: Tue Jun 28, 2011 9:40 am
by MrJingles
this does the trick, thank you !