First of all, the title would suggest this is a duplicate of this or this, but for a couple of reasons, those answers don't work for me, even if my initial problem is the same. I'll explain why.
My problem is this: I have a few occasions in my code where I want to send a header and a body and then terminate processing. Unlike those other questions, I cannot use return
or throw an Exception instead (those are obviously different functions designed for different purposes than exit, and this is not an error; it's simply an early runtime termination in some specific cases).
Still, I want to write unit tests that run these methods, make sure that the appropriate headers are set (solution found here), the output body is correct (solved with the $this->expectOutputString()
-method in the test case) and then continue testing. In between, the exit
will happen.
I've tried the @runInSeparateProcess
-annotation in PHPUnit, I've also checked the test_helpers extension, which works, but I don't want to add another extension (be advised that tests will be run in production as well) for one single line of native PHP code that breaks everything. There must be a simpler way without sacrificing best practices.
Does anyone have a good solution to this problem?