Category: PHP, MySQL, api

If you have a web-based application, chances are high that it uses some sort of database to store information. One of the questions you then have to answer is, “How do I test something that queries a database?”

If you have a getCurrentUser method, a mock can ensure that method returns an object with appropriate data.

You can avoid issues of staleness by running your tests against a real database: That introduces some new challenges — you have to figure out how to replicate your database schema, how to generate test data, and how to scrub the database once a test is complete, but otherwise it’s pretty reliable.

Whenever you’re simulating anything for tests, you run the risk that your simulation is slightly off, leading to tests passing that really should fail.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners