Answer the question
In order to leave comments, you need to log in
Is it possible to set an expectation for the execution order of mock methods in phpunit?
Good afternoon,
Is it possible to set an expectation for the execution order of mock methods in phpunit? and should it be done in unit tests?
For example, there is a class under test:
<?php
class XXX
{
private $db;
public function __construct($db)
{
$this->db = $db;
}
public function doSomething(array $list)
{
$db->truncate();
foreach ($list as $item) {
$db->insert($item);
}
}
}
class XXXTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function doSomething()
{
$xxx = new XXX($this->createDBMock($this->getList()));
$xxx->doSomething($this->getList())
}
private function createDBMock($list)
{
$mock = $this->createMock(DB::class);
$mock
->expects($this->once())
->method('truncate');
$mock
->expects($this->exactly(count($list)))
->method('save')
->with($list[0], $list[1]);
return $mock;
}
private function getList()
{
return ['foo', 'bar'];
}
}
public function doSomething(array $list)
{
foreach ($list as $item) {
$db->insert($item);
}
$db->truncate();
}
Answer the question
In order to leave comments, you need to log in
and should it be done in unit tests?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question