How to test the die() function?
Tags: webdev, php, testing, softwaredevelopment
Published: 2024-02-16 20:01:21

I'd written [a PHP package for those who develop Telegram bots](https://github.com/muhammadmp97/TeleBot), to make it easier and cleaner.

Look at the third argument that is false:
```php
$tg->listen('/start', function () use ($tg) {
    $tg->sendMessage([
        'chat_id' => $tg->user->id,
        'text' => 'Hello, world!',
    ]);
}, false);
```

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:
```php
return $this->dieIf($thenDie);
```

And this is the `dieIf()` method implementation that's just simple:
```php
private function dieIf(bool $condition)
{
    if ($condition) {
        die();
    }
}
```

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:
```php
if (!app()->runningUnitTests()) {
    // Doing things we're not gonna test
}
```
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:
```php
private function dieIf(bool $condition)
{
    if ($condition) {
        throw new TerminationException();
    }
}
```

---

According to [the documentation](https://www.php.net/manual/en/function.exit.php), 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](https://www.php.net/manual/en/language.exceptions.php) 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:
```php
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'));
}
```

---

Thanks for reading. Now it does the same, and I can test it. There are some links for further reading:
- https://stackoverflow.com/questions/23915434/ignore-exit-and-die-with-phpunit
- https://stackoverflow.com/questions/5936094/phpunit-and-die-function
- https://stackoverflow.com/questions/1347794/how-do-you-use-phpunit-to-test-a-function-if-that-function-is-supposed-to-kill-p