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

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners