PHPUnit mock related issues

Can someone provide me with a link to a good PHPUnit mock reference? Official documentation

+5
source share
2 answers

[Note: for all code samples on linked sites, follow the links for more detailed explanations.]

Returns different values

The current documentation suggests PHPUnit using a callback or onConsecutiveCalls():

$stub->expects($this->any())
      ->method('doSomething')
      ->will($this->returnCallback('str_rot13'));

$stub->expects($this->any())
     ->method('doSomething')
     ->will($this->onConsecutiveCalls(2, 3, 5, 7));

Expect a few options

with()may contain several parameters :

$observer->expects($this->once())
         ->method('reportError')
         ->with($this->greaterThan(0),
                $this->stringContains('Something'),
                $this->anything());

Check multiple calls

, ( PHPUnit, ), at() to :

$inputFile->expects($this->at(0))
          ->method('read')
          ->will($this->returnValue("3 4"));
$inputFile->expects($this->at(1))
          ->method('read')
          ->will($this->returnValue("4 6"));
$inputFile->expects($this->at(2))
          ->method('read')
          ->will($this->returnValue(NULL));
+12

( Mock):

class tableMock extends Table {
    public function __construct() {
    }

    public function find($id) {
        return $id;
    }
}

$tableMock = new tableMock();

//Do your testing here...

Mock, ...

+3

All Articles