Search code examples
phptestingphpunitoutput-buffering

phpunit with output buffering


I'm trying to integrate PHPunit into a big project, everything seems fine except it seems that all methods that rely on ob_start() will result in a risky test.

Reading online, it seems risky tests are such tests which execute code not covered by the testing method. However, I haven't used the @covers annotation at all, And this happens only on ob_start().

So a few questions :

  1. Is it possible to resolve this issue?
  2. Is there something inherently wrong with ob_start when it comes to testing?
  3. Is there a way around it?(if not possible to resolve it).

The use case is using a framework who's views are returned(instead of sent to the browser), Codeigniter comes as a classic example, where you can return views. Returning views depend on ob_start(). Thanks alot!


Solution

  • The solution is two fold, as it revolves around two issues I had.

    1. Regarding the specific problem, using views in a framework(codeigniter), I simply used a mock for the loader, so I implemented an empty function that doesn't actually load and outputs html.
    2. Regarding the actual issue I had with PHPunit's behavior, it seems that PHPunit(4.5) will assume a test is Risky if using ob_start and ob_clean, However when using ob_get_clean the testing works as expected. I'm not sure why as I didn't dive into the code itself, but that solved it for me