DEV Community

Muhammad MP
Muhammad MP

Posted on

How to test the die() function?

I'd written a PHP package for those who develop Telegram bots, to make it easier and cleaner.

Look at the third argument that is false:

$tg->listen('/start', function () use ($tg) {
    $tg->sendMessage([
        'chat_id' => $tg->user->id,
        'text' => 'Hello, world!',
    ]);
}, false);
Enter fullscreen mode Exit fullscreen mode

It's $thenDie to terminate the script after running the command, so the rest of the code won't be executed. How does it do that? By calling the dieIf() method:

return $this->dieIf($thenDie);
Enter fullscreen mode Exit fullscreen mode

And this is the dieIf() method implementation that's just simple:

private function dieIf(bool $condition)
{
    if ($condition) {
        die();
    }
}
Enter fullscreen mode Exit fullscreen mode

Okay, so we use the die() function to terminate the script and say goodbye to the request. Where's the problem?

The problem is that I wanted to write some tests for the package and the die() function not only terminates the script, but also prevents the tests from working too! Surprising, huh?

What can we do here?
I searched the whole Stackoverflow for find a sufficient answer, and I list the best solutions provided by some smart guys:

A. Move it to another class then mock it, bruh!
The problem with this solution is that we don't want to create another class and make an instance of that, just because we need to test one line of code.

B. Check the environment
You can use constants to ensure if the code is running tests. You may have done this in Laravel before:

if (!app()->runningUnitTests()) {
    // Doing things we're not gonna test
}
Enter fullscreen mode Exit fullscreen mode

It's certainly sensible to use the die() function there, and write an else for the testing environment, right? Not really! Testable code, is code that don't need using these tricks to get tested. Moreover, this plan is similar to the previous one and adds unjustifiable complexity to the project.

If it’s hard to test your code, consider changing the design.

C. Use something other than the die() function
I've adopted this approach, and replaced the die() function with throwing an exception:

private function dieIf(bool $condition)
{
    if ($condition) {
        throw new TerminationException();
    }
}
Enter fullscreen mode Exit fullscreen mode

According to the documentation, the die() function is an equivalent to the exit() function.

exit — Output a message and terminate the current script

By throwing an exception, you have more control on what happens; you can catch the exception and do whatever you want; reporting to Sentry, writing logs, showing error pages and so on. Using the die() function doesn't provide these advantages at all, and above that you can't test it like exceptions!

This is one of my test cases after that significant change:

public function test_terminates_if_secret_token_didnt_match()
{
    $_SERVER['HTTP_X_TELEGRAM_BOT_API_SECRET_TOKEN'] = 'password';

    $this->expectException(TerminationException::class);

    (new TeleBot('some-token', 'indeedpassword'));
}
Enter fullscreen mode Exit fullscreen mode

Thanks for reading. Now it does the same, and I can test it. There are some links for further reading:

Top comments (0)