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 t开发者_如何学Goips 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!
As gphilip said, you can use runkit to mock pcntl_fork() and test all branches of code. Here's an example: http://kpayne.me/2012/01/17/how-to-unit-test-fork/
There are two things that need to be tested here:
- If forking works
- 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.
精彩评论