jeudi 1 octobre 2015

PHPUnit: assert E_USER_WARNING is generated as expected without ending a test

I want to test code to show that it produces an E_USER_WARNING in certain circumstances.

I found this article which addresses the problem, so as I see it I have these options:

  1. Copying that code into every TestSuite which needs to test for warnings
  2. Making a subclass of PHPUnit's TestCase that has that code, and have my tests inherit from that
  3. Use composition, i.e. make a helper class that achieves the same.

I don't like 1, for obvious reasons - I can't imagine the author had that option in mind.

I don't like 2, because there are some tests that I might use a third party TestCase that already extends PHPUnit's TestCase, so it would get complicated or messy if I wanted to add that functionality for that subclass as well by subclassing that subclass.

Option 3 sounds the best. But...

This all seems to be reinventing the wheel. Aren't there any off-the-shelf solutions for PHPUnit to assert a warning has been generated as expected? If not, is there a good pattern for this that anyone could share?

For better or worse, I very strongly want to stick with PHPUnit for reasons such as:

  • I want to keep my tests consistent, and not have to re-write all my current tests
  • It is the most supported PHP test framework for IDEs etc
  • I don't want to have to learn another framework

Aucun commentaire:

Enregistrer un commentaire