I have noticed that when my laptop is connected to the internet my PHPUnit tests takes between ~90 sec ~200 sec to finish. But when I disconnect it from the internet it runs in less than 20 sec!! that makes me happy and sad at the same time!
In both cases all the tests are passing, I'm sure I'm mocking every request to external API's.
I'm using Laravel and MySQL for real data storage and in-memory sqlite for the tests environment. Also my development environment is all on running on Docker.
Is this something related to PHPUnit or to my code!! any one has an idea on what's going on. Thanks
More Info
The domain I'm using is something.dev
and my API's uses api.something.dev
. Every test makes at least one call to each API endpoint.
DNS! If you think this is due to DNS lookup: I changed all the domain and subdomains to 127.0.0.1 just to test it, and it didn't helped the tests are still slow. Should this eliminate the possibility of DNS lookup!
In addition I tried mocking the DNS using The PHPUnit Bridge with PHPUnit but I guess I couldn't make it work due to the lack of documentation, so I didn't knew what to pass as parameter to DnsMock::withMockedHosts([here!!])
after calling it from my setUp()
function.
Something else I think the problem is related to the data storage because the delay happens before and after querying the database, mostly to store data.
Wow that wasn't expected. Turns out my tests are slow because of the image()
function provided by the PHP Faker package $faker->image()
.
I was using it in one of my factories to prepare a fake Image for the DB, I didn't know it's literally downloading images and storing them in folder like this /private/var/folders/51/5ybn3kjn8f332jfrsx7nmam00000gn/T/
.
I was able to find that by monitoring what the PHP process is doing while the test is running, to find out it has an open .jpg file in that directory, so I looked in my code anything related to images and discovered that, after about 6 hours of debugging. Happy coding :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With