Answer the question
In order to leave comments, you need to log in
How can PHPunit emulate a database query?
Hello!
I want to write a test for a function:
function calculateLimits($pdo, $interval, $timeStart)
{
$intervalMinutes = getMinutesFromInterval($interval);
$lastUpdate = $pdo->query("SELECT updated FROM last_updates
WHERE table_name = 'candles${interval}';")->fetchColumn();
$lastUpdateCarbon = Carbon::parse($lastUpdate, 'UTC');
$diffInSecond = $timeStart->diffInSeconds($lastUpdateCarbon);
$limit = $diffInSecond / (60 * $intervalMinutes);
return ceil($limit);
}
class CandlestickTest extends TestCase
{
private $stub;
public function setUp()
{
$this->stub = $this->getMockBuilder('PDO')
->disableOriginalConstructor()
->setMethods(['query', 'fetchColumn'])->getMock();
}
public function testCalculateLimits()
{
$this->stub->method('query')->with("SELECT updated FROM last_updates
WHERE table_name = 'candles30m';")
->method('fetchColumn')->willReturn('2019-01-26 23:50:49');
$this->assertEquals(48, C\calculateLimits($this->stub, '30m', '2019-01-27 23:50:49'));
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question