I want to write a test for the 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); } As you can see, there is a variable that depends on the response of the database. I just can not figure out how to emulate a query to the database. I do this:
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')); } } As soon as -> method ('fetchColumn') after method ('query') -> with (), the tests give me a warning: Method name matcher is already defined, cannot redefine
Google on this error does not give anything - only similar errors. Why does he think that I redefine the method ?! I do not understand how to make it work.
I need that during the testing of the calculateLimits function, when a query to the database occurs, this very query was not executed and the string '2019-01-27 23:50:49' was simply substituted, and not all of that.
Ps. I do not understand the purpose of with and will. It seems that PHPunit wants me to substitute REAL arguments with with and the actual return of the query function in will.