• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

How do I run time-dependent database tests with PHPUnit?

I have a PHP application and I want to test the functionality where it records each login attempt in the database, including the time of the login attempt. The SQL statement that I'm trying to test is "INSERT INTO logins VALUES {$email}, {$user_id}, NOW(), {$_SERVER['REMOTE_ADDR']}, {$login_successful}". The problem is, I can't figure out how to use PHPUnit to accomplish this. The issue is that PHPUnit seems to require that I write up my expected data set in an XML file and then compare that file to what's in the database --- but since the value of NOW() depends on when I'm running the test, I can't have a pre-written file to compare against. Has anybody else run into this issue, and what's the best way to work around it? Thanks.
1 Solution
For those test I directly access the database, read the records and compare the value of the singele fields with the expected ones. For times you can add a delta at which the value is valid.

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now