5

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!

3
  • are you using strict mode (check phpunit.xml)? Commented Mar 25, 2015 at 23:09
  • I'm not using an XML, rather an inline invocation, I'm not using the --strict flag. when adding --strict(even though it's deprecated), more of the tests are now flagged as risky Commented Mar 27, 2015 at 8:50
  • I actually meant --strict-coverage (phpunit 4.5) Commented Mar 27, 2015 at 12:45

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

3 Comments

// Fetch the buffered output then ob_get_clean .... thank you! <br/> $out = output(); $out = ob_get_clean();
Thanks for point two, I'm creating a prettier var_dump function :)
its a risky test because flush leave the output in the buffer so the next test would start with a dirty buffer, ob_get_clean wipes the buffer out so nothing left to interfere with other tests

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.