phpunit-testingforkprocess-control

How to unit test PCNTL forker class in PHP?


I have an abstract PHP class that is responsible for doing process forks and also detaching the current process from terminal and continue as deamon.

I really would like to get tips about how to unit test this class (PHPUnit). Let's say implement a minimal socket server in the test based on this abstract and communicating with that? Any better ideas?

The class can be seen here: https://github.com/tcz/PHPTracker/blob/master/lib/PHPTracker/Threading/Forker.php

Thank you!


Solution

  • There are two things that need to be tested here:

    1. If forking works
    2. The rest of the code

    Testing if the fork worked can be done by saving the ID of the forked process somewhere and see if it's still running, and the rest of the class can be tested as usual without forking.