I created a small library that requires the user to set up a secret constant in order for it to work.
Here is a part of my code:
if (!defined('SOMELIB_SECRET')) {
throw new RuntimeException('You need to define the constant SOMELIB_SECRET to use this library');
}
This code works great, but I don't know how to test the exception throw. I can test everything if I create the constant in phpunit.xml:
<php>
<const name="SOMELIB_SECRET" value="ThisIsTheSecretForTests" />
</php>
I can test the exception if I don't add the constant in the PHP.
What I want to do is test both, I would need a way to unset the constant mid-way, which is not really a thing (turns out constants are... constant).
I tried to create 2 test files: 001_SomeLibWithoutSecretTest.php
and 002_SomeLibWithSecretTest.php
, so that I can create the constant between them. It worked on Linux, but now that I'm using Windows, the tests are called the other way around. Besides, it's a bit hacky: tests should not have to be called in a given order.
After all this introduction, here is my simple question:
How can I run tests, with PHPUnit, defining constants for some tests but not for all?