// With PHPUnit$phpunitMock=$this->getMock('AClassToBeMocked');// With Mockery$mockeryMock= \Mockery::mock('AClassToBeMocked');
$emMock = $this->getMock('\Doctrine\ORM\EntityManager',
array('getRepository', 'persist', 'flush'));
$emMock->expects($this->any())
->method('getRepository')
->will($this->returnValue(new FakeRepository()));
$emMock->expects($this->any())
->method('persist')
->will($this->returnValue(null));
$emMock = \Mockery::mock('\Doctrine\ORM\EntityManager',
array(
'getRepository' => new FakeRepository(),
'persist' => null,
'flush' => null,
));
$mock->expects($this->at(0))->method('someMethod')
->will($this->returnValue($firstValue));
$mock->expects($this->at(1))->method('someMethod')
->will($this->returnValue($secondValue));
$mock->expects($this->at(2))->method('someMethod')
->will($this->returnValue($firstValue));
$mock->shouldReceive('someMethod')->andReturn($firstValue, $secondValue, $firstValue, $secondValue);
$mock->expects($this->any())->method('getNumber')
->with(2)
->will($this->returnValue(2));$mock->expects($this->any())->method('getNumber')
->with(3)
->will($this->returnValue(3));
$mockeryMock->shouldReceive('getNumber')->with(2)->andReturn(2);
$mockeryMock->shouldReceive('getNumber')->with(3)->andReturn(3);
class Calculator {
function add($firstNo, $secondNo) {
}
function subtract($firstNo, $secondNo) {
}
function multiply($value, $multiplier) {
}
}
$phpMock = $this->getMock('Calculator', array('add'));
$mockeryMock2 = \Mockery::mock(new Calculator);
http://code.tutsplus.com/tutorials/mockery-a-better-way--net-28097
Mockery allows you to define mocks for classes that do not exist.
// With PHPUnit$phpunitMock=$this->getMock('AClassToBeMocked');$phpunitMock->expects($this->once())->method('someMethod');// Exercise for PHPUnit$someObject->doSomething($phpunitMock);
// With Mockery$mockeryMock= \Mockery::mock('AnInexistentClass');$mockeryMock->shouldReceive('someMethod')->once();// Exercise for Mockery$someObject->doSomething($mockeryMock);
classSomeClass {functiondoSomething($anotherObject) {$anotherObject->someMethod();}}
No need to define a class called AClassToBeMocked, and a method called someMethod() .