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!

有帮助吗?

解决方案

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.

其他提示

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/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top