Mock exit() function in PHP

2019-07-03 21:59发布

问题:

For a library I am writing I would like to be able to mock the exit PHP function.

I tried to use php-mock to provide a namespaced version of exit, something like

namespace MyNamespace;

function exit()
{
    // my mocked version of the function
}

But this creates problems to the parser, which throws the following ParseError: syntax error, unexpected 'exit' (T_EXIT), expecting '('.

Is there any other way to mock a built in function without incurring in parsing problems? Should I try to modify the AST with something like BetterReflection?

回答1:

As per comments, I guess that mocking language constructs is not feasible.

To test exit() I ended up spawning other processes with exec and asserting on their output and exist status