Mockery
// With PHPUnit$phpunitMock=$this->getMock('AClassToBeMocked');// With Mockery$mockeryMock= \Mockery::mock('AClassToBeMocked');
Returning values
$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));
Returning values
$emMock = \Mockery::mock('\Doctrine\ORM\EntityManager',
array(
'getRepository' => new FakeRepository(),
'persist' => null,
'flush' => null,
));
Difference values
$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));
Difference values
$mock->shouldReceive('someMethod')->andReturn($firstValue, $secondValue, $firstValue, $secondValue);
Return Values Based on Given Parameter
$mock->expects($this->any())->method('getNumber')
->with(2)
->will($this->returnValue(2));$mock->expects($this->any())->method('getNumber')
->with(3)
->will($this->returnValue(3));
mockery
$mockeryMock->shouldReceive('getNumber')->with(2)->andReturn(2);
$mockeryMock->shouldReceive('getNumber')->with(3)->andReturn(3);
partial mock
class Calculator {
function add($firstNo, $secondNo) {
}
function subtract($firstNo, $secondNo) {
}
function multiply($value, $multiplier) {
}
}
$phpMock = $this->getMock('Calculator', array('add'));
partial mock
$mockeryMock2 = \Mockery::mock(new Calculator);
Ref
http://code.tutsplus.com/tutorials/mockery-a-better-way--net-28097
Class to be mocked
Mockery allows you to define mocks for classes that do not exist.
Expect
// With PHPUnit$phpunitMock=$this->getMock('AClassToBeMocked');$phpunitMock->expects($this->once())->method('someMethod');// Exercise for PHPUnit$someObject->doSomething($phpunitMock);
Expect
// With Mockery$mockeryMock= \Mockery::mock('AnInexistentClass');$mockeryMock->shouldReceive('someMethod')->once();// Exercise for Mockery$someObject->doSomething($mockeryMock);
Code
classSomeClass {functiondoSomething($anotherObject) {$anotherObject->someMethod();}}
Method expected to be called
No need to define a class called AClassToBeMocked, and a method called someMethod() .
Mockery
By Tiến Võ Xuân
Mockery
- 1,266