Why are PHPUnit tests faster when the machine is disconnected from the Internet?

I noticed that when my laptop is connected to the Internet, my PHPUnit tests take from ~ 90 seconds to 200 seconds to the end. But when I disconnect it from the Internet, it works in less than 20 seconds! it makes me happy and sad at the same time!

In both cases, all tests pass, I am sure that I mock every request of external APIs.

I use Laravel and MySQL for real data storage and sqlite in memory for the testing environment. My development environment also runs on Docker.

This has something to do with PHPUnit or my code! anyone has an idea of ​​what's going on. Thanks

Additional Information

My domain is something.dev , and my API uses api.something.dev . Each test makes at least one call for each API endpoint.

DNS! If you think this is related to DNS lookups: I completely changed the entire domain and subdomains to 127.0.0.1 to check it, and this did not help the tests still slow. If this removes the DNS lookup feature!

Also, I tried to mock DNS using PHPUnit Bridge with PHPUnit, but I think I couldn’t get it to work due to a lack so I didn’t know what to pass as the parameter to DnsMock::withMockedHosts([here!!]) after calling it from my setUp() function.

Something else I think the problem is with data storage, since the delay occurs before and after the database query, mainly for data storage.

+6
source share
1 answer

Wow that was not expected. It turns out my tests are slow due to the image() function provided by PHP Faker package $faker->image() .

I used it at one of my factories to prepare a fake image for the database, I did not know that it literally downloads the images and stores them in a folder like this /private/var/folders/51/5ybn3kjn8f332jfrsx7nmam00000gn/T/ .

I managed to find that, by tracking what the PHP process does during the test, to find out that it has an open .jpg file in this directory, so I looked at something related to images in my code and found that after about 6 hours of debugging. Happy coding :)

+3
source

Source: https://habr.com/ru/post/1013398/


All Articles