Unit Testing with items that need to send headers

后端 未结 11 1923
春和景丽
春和景丽 2020-12-03 01:01

I\'m currently working with PHPUnit to try and develop tests alongside what I\'m writing, however, I\'m currently working on writing the Session Manager, and am having issue

11条回答
  •  春和景丽
    2020-12-03 01:25

    I think the "right" solution is to create a very simple class (so simple it doesn't need to be tested) that's a wrapper for PHP's session-related functions, and use it instead of calling session_start(), etc. directly.

    In the test pass mock object instead of a real stateful, untestable session class.

    private function __construct(SessionWrapper $wrapper)
    {
       if (!$wrapper->headers_sent())
       {
          $wrapper->session_start();
          $this->session_id = $wrapper->session_id();
       }
    }
    

提交回复
热议问题