1. Go to this page and download the library: Download jcid/phpunit-mock-extension library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jcid / phpunit-mock-extension example snippets
class ExampleTest extends PHPUnit_Framework_TestCase
{
public function testExample()
{
$mock = $this->getMock(\stdClass::class, ['mymethod']);
$mock->expects($this->at(0))
->method('mymethod')
->with('aaa')
->willReturn('bbb');
$mock->expects($this->at(1))
->method('mymethod')
->with('bbb')
->willReturn('ccc');
$this->assertSame('bbb', $mock->mymethod('aaa'));
$this->assertSame('ccc', $mock->mymethod('bbb'));
$mock->mymethod('different');
}
}
class ExampleTest extends PHPUnit_Framework_TestCase
{
public function testLogicOr()
{
$mock = $this->getMock(\stdClass::class, ['mymethod']);
$mock->expects($this->exactly(2))
->method('mymethod')
->with($this->logicalOr(
$this->equalTo('aaa'),
$this->equalTo('bbb')
))
->will($this->onConsecutiveCalls('bbb', 'ccc'));
$this->assertSame('bbb', $mock->mymethod('aaa'));
$this->assertSame('ccc', $mock->mymethod('bbb'));
}
}
class ExampleTest extends PHPUnit_Framework_TestCase
{
public function testReturnValueMap()
{
$mock = $this->getMock(\stdClass::class, ['mymethod']);
$mock->expects($this->exactly(2))
->method('mymethod')
->will($this->returnValueMap([
['aaa', 'bbb'],
['bbb', 'ccc'],
]));
$this->assertSame('bbb', $mock->mymethod('aaa'));
$this->assertSame('ccc', $mock->mymethod('bbb'));
}
}
class ExampleTest extends PHPUnit_Framework_TestCase
{
public function testReturnValueMapStrict()
{
$mock = $this->getMock(\stdClass::class, ['mymethod']);
$mock->expects($this->exactly(2))
->method('mymethod')
->will($this->returnValueMap([
[new DummyValueHolder('aaa'), 'bbb'],
['bbb', 'ccc'],
]));
$this->assertSame('bbb', $mock->mymethod(new DummyValueHolder('aaa')));
$this->assertSame('ccc', $mock->mymethod('bbb'));
}
}
class DummyValueHolder
{
private $data;
public function __construct($data)
{
$this->data = $data;
}
}
use PHPUnit\Extensions\MockObject\Stub\ReturnMapping\Builder;
class ExampleTest extends PHPUnit_Framework_TestCase
{
public function testSolution()
{
$builder = new Builder();
$builder->expects($this->exactly(2))
->with(new DummyValueHolder('aaa'))
->willReturnSelf();
$builder->expects($this->once())
->with('second method call matcher')
->willReturn('second return');
$builder->expects($this->exactly(2))
->with('third method call matcher')
->willReturn('third return');
$dummy = $this->getMock(\stdClass::class, ['test']);
$dummy->expects($builder->matcher())->method('test')->will($builder->mapper());
$this->assertSame($dummy, $dummy->test(new DummyValueHolder('aaa')));
$this->assertSame($dummy, $dummy->test(new DummyValueHolder('aaa')));
$this->assertSame('second return', $dummy->test('second method call matcher'));
$this->assertSame('third return', $dummy->test('third method call matcher'));
$this->assertSame('third return', $dummy->test('third method call matcher'));
}
}
class DummyValueHolder
{
private $data;
public function __construct($data)
{
$this->data = $data;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.