phpunit Mockobject weirdness

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
MrJingles
Forum Newbie
Posts: 4
Joined: Sat Jun 25, 2011 8:57 am

phpunit Mockobject weirdness

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: phpunit Mockobject weirdness

Post 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.
MrJingles
Forum Newbie
Posts: 4
Joined: Sat Jun 25, 2011 8:57 am

Re: phpunit Mockobject weirdness

Post by MrJingles »

this does the trick, thank you !
Post Reply