PHP code example of czukowski / phpunit-mock-dibi
1. Go to this page and download the library: Download czukowski/phpunit-mock-dibi 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/ */
czukowski / phpunit-mock-dibi example snippets
$dibi = new Dibi\Connection([
'driver' => $factory->createMySqlDriver() // or whatever other driver you may be needing.
]);
$this->createDatabaseMock($dibi)
->expects($this->any())
->willReturnResultSet([
['id' => 1, 'name' => 'foo'],
['id' => 2, 'name' => 'bar'],
]);
$this->createDatabaseMock($dibi)
->expects($this->once())
->willReturnResultSet([
['id' => 1, 'name' => 'foo'],
['id' => 2, 'name' => 'bar'],
]);
$mock = $this->createDatabaseMock($dibi);
$mock->expects($this->once())
->query('SELECT * FROM `t1`')
->willReturnResultSet([['id' => 1, 'name' => 'foo']]);
$mock->expects($this->once())
->query('SELECT * FROM `t2`')
->willReturnResultSet([['id' => 2, 'name' => 'bar']]);
$mock = $this->createDatabaseMock($dibi);
$mock->expects($this->at(1))
->query('INSERT INTO `t1` VALUES (1, "foo")')
->willSetLastInsertId(1);
$mock->expects($this->at(2))
->query('INSERT INTO `t1` VALUES (2, "bar")')
->willSetLastInsertId(2);
$mock->expects($this->once())
->query('SELECT * FROM `t1`')
->willReturnResultSet([]);
$this->createDatabaseMock($dibi)
->expects($this->exactly(3))
->query('INSERT INTO `t1` VALUES ("a", "b", "c")')
->willSetLastInsertId(1, 2, 3);
$this->createDatabaseMock($dibi)
->expects($this->exactly(2))
->query('UPDATE `t1` SET `foo` = "bar" WHERE `id` = 1')
->willSetAffectedRows(1);
$this->createDatabaseMock($dibi)
->expects($this->once())
->query($this->stringStartsWith('SELECT'))
->willReturnResultSet([['id' => 1, 'name' => 'foo']]);
$this->createDatabaseMock($dibi)
->expects($this->exactly(4))
->query($this->stringStartsWith('INSERT'))
->onConsecutiveCalls()
->willSetLastInsertId(1)
->willSetLastInsertId(2)
->willThrowException(new RuntimeException('Deadlock'))
->willSetLastInsertId(3);
$mock = $this->createDatabaseMock($dibi);
$mock->expects($this->any())
->query($this->stringStartsWith('INSERT'))
->willInvokeCallback(function ($invocation) {
$invocation->setLastInsertId(1);
});
$mock->expects($this->any())
->query($this->stringStartsWith('UPDATE'))
->willInvokeCallback(function ($invocation) {
$invocation->setAffectedRows(0);
});
$mock->expects($this->any())
->query($this->stringStartsWith('SELECT'))
->willInvokeCallback(function ($invocation) {
$invocation->setResultSet([]);
});
$mock = $this->createDatabaseMock($dibi);
$mock->setRequireMatch(FALSE);