开发者

How to unit test PCNTL forker class in PHP?

开发者 https://www.devze.com 2023-02-18 10:22 出处:网络
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 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:

  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号