r/PHPhelp • u/SimpleMinded001 • May 11 '20
Unit/Integration testing advice needed
My situation: working in a company, we have one big main project and a couple of microservices. We, like everyone else, have some issues. One of them is lack of tests. I am pushing to write more tests, but it's a bit hard, especially on existing project where people are not used to writing tests. I know what the difference between unit, integration, acceptance tests is, that's not my issue.
Since I started digging, I noticed that most of our business logic relies of data from the db. Examples:
if the customer is of this type - do this thing, otherwise something different
if the customer has orders which has specific type of product, do something
and a lot of more complex relationships, like if a customer of specific type, has any orders for the past month that contain specific items which are in specific state, and these items don't have stock... you get the point
This is textbook integration test logic right there. I know that in the perfect scenario I should have a "testing db" and a "testing db dump" that gets cleaned and repopulated after each test, but how do you deal with the most complex cases like the ones I described? Does it make sense to populate so much different related data after each test? Wouldn't that be super slow at some point? Not to mention the amount of work it'll take to keep this testing db up-to-date. If not, what are some other solutions? We are using Codeception, but my question is more general.
P.S. Thanks to everyone who'd read that wall of text. I tried to be as descriptive as possible.
2
u/warren5236 May 11 '20
We have a very similar situation. What we find is 99% of the time you don't have to completely reset your database to zero between tests because it is so slow.
What we do is have a class that generates an "ideal" set of users for each type/situation and then we reuse those in each test. We occasionally find a test where a change in one test affects the other but it's found quickly and fixed.
2
u/SimpleMinded001 May 11 '20
So, if I understood correctly:
you have an empty db
you have a factory/faker class that generates the ideal set of users, orders, items etc. for a specific test case
you populate the db with this ideal data
you run the test
you clear the db
go back to step 2 for the next test
2
u/warren5236 May 11 '20
It's more like:
Reset so we have an empty db one time at the start of our CI/CD pipeline.
At start of an individual integration test we generate the specific idea models we need for that test unless they're already in the database. If there already in the database we just reuse them.
run the test
database is left as is and resets at start of next run of the CI/CD pipeline
1
3
May 11 '20
You should be getting data from classes that do only that, aka repository classes or DAOs https://en.m.wikipedia.org/wiki/Data_access_object?wprov=sfla1
Then in your unit tests you mock the dao class and its response
This is why you shouldn't have classes that do both business logic and data, single responsibility and dependency injection make unit tests a breeze
1
u/binary-data May 11 '20
Good point, but this may require major rewrites. This may not be acceptable in some cases.
I'd recommend to estimate both alternatives (rewriting to DAOs vs covering existing logic with tests) or a mix of them, and then decide which way to go.
4
u/halfercode May 11 '20
+1 for a solid description, that is quite rare in here.
This is often a huge political and cultural problem. I won't dwell on that, other than to offer sympathy!
What I like to do is to split tests into several categories. Some of your logic is not intertwined with the database, so the automated tests here can be unit tests or integration tests where your database component is mocked out. I favour Mockery for this, but I believe PHPUnit has its own mocking system, which I am sure is perfectly good.
For tests that actually need to be run against a database, I don't think you need a database dump. Here is what I like to do:
Destroy/create an empty test database in a one-off pre-test bootstrap
Ensure your tests connect the system to the test database (and not, say, to your dev database)
Use migrations to convert an empty database to an empty table set.
Before each test, truncate all tables (you sometimes have to do tricks to ignore FK constraints, but that is pretty easy to get around)
Each test just inserts the bare minimum records it needs to perform the test. You can write some utility classes to run some inserts, or maybe hook into your migrations system to run a seeder
The test should run a service or endpoint call
Finally you can test the state of the database, to see if the call did what you expected
This approach obviates the need for a bulky database dump, and is pretty quick to run. It's slower than unit tests, but if your suite takes a couple of minutes to run rather than a few seconds, I don't think that is a major problem.