PHPUnit mocks - an assert method called

I am new to phpunit and have read the documentation on mock objects, but this is not very clear.

I am trying to write a simple test that claims a method inside a class. In the following code, I verify that when calling Client :: exchangeArray, a call to Client :: getInputFilter is made.

class Client implements InputFilterAwareInterface { public function getInputFilter() { if(!$this->_inputFilter){ $inputFactory = new InputFactory(); $inputFilter = new InputFilter(); $inputFilter->add($inputFactory->createInput(array( 'name' => 'id', 'required' => true, 'filters' => array( array( 'name' => 'Int' ) ) ))); $inputFilter->add($inputFactory->createInput(array( 'name' => 'name', 'required' => true, 'filters' => array( array( 'name' => 'StripTags' ), array( 'name' => 'StringTrim' ), array( 'name' => 'StripNewLines' ), array( 'name' => 'Alpha' ) ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 2, 'max' => 100 ) ) ) ))); $inputFilter->add($inputFactory->createInput(array( 'name' => 'surname', 'required' => true, 'filters' => array( array( 'name' => 'StripTags' ), array( 'name' => 'StringTrim' ) ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 2, 'max' => 100 ) ) ) ))); $inputFilter->add($inputFactory->createInput(array( 'name' => 'email', 'required' => false, 'filters' => array( array( 'name' => 'StripTags' ), array( 'name' => 'StringTrim' ) ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 2, 'max' => 150 ) ), array( 'name' => 'EmailAddress' ) ) ))); $this->_inputFilter = $inputFilter; } return $this->_inputFilter; } public function exchangeArray($data){ $inputFilter = $this->getInputFilter(); $inputFilter->setData($data); if(!$inputFilter->isValid()){ throw new \Exception('Invalid client data'); } $cleanValues = $inputFilter->getValues(); $this->_id = (isset($cleanValues['id']) ? $cleanValues['id'] : null); $this->_name = (isset($cleanValues['name']) ? $cleanValues['name'] : null); $this->_surname = (isset($cleanValues['surname']) ? $cleanValues['surname'] : null); $this->_email = (isset($cleanValues['email']) ? $cleanValues['email'] : null); } } 

Here is my test case:

 public function testExchangeArrayCallsInputFilter(){ $data = array('id' => 54, 'name' => 'john', 'surname' => 'doe', 'email' => ' john.doe@domain.com ' ); $mock = $this->getMock('Client', array('exchangeArray')); $mock->expects($this->once()) ->method('getInputFilter'); $mock->exchangeArray($data); } 

... and I get the following error:

The wait error for the method name is equal when called 1 time (s). The method should have been called 1 time, actually called 0 times.

Where am I mistaken?

+7
source share
1 answer

It all depends on what you want to check and what you want to mock. Based on the name of your test, I assume that you want to use the exchangeArray method.

The getMock method takes as the second argument the names of the methods you want mock. This means that they are never called.

So, if you want to test the exchangeArray and mock getInputFilter methods, you must pass "getInputFilter" in the second argument, as shown below:

 $mock = $this->getMock('Client', array('getInputFilter')); $mock->expects($this->once()) ->method('getInputFilter'); $mock->exchangeArray($data); 

But be careful. You did not tell your layout to return anything, so it will return a null value. This means that you will receive a fatal error in the second line of the exchangeArray method (attempt to call a method for a non-object). You need to prepare some fake filter object that you need to deal with, for example:

 // $preparedFilterObject = ... $mock = $this->getMock('Client', array('getInputFilter')); $mock->expects($this->once()) ->method('getInputFilter') ->will($this->returnValue($preparedFilterObject); $mock->exchangeArray($data); 

And if you want to call the "real" getInputFilter method - then you simply cannot mock this method.

+9
source

All Articles