Write tests only for your own code
Tags: testing, tdd, laravel, logging
Published: 2022-04-09 14:51:58

Real-life example to answer 'what should I test?'

The developers who are just getting started with writing tests often get confused about what to test and how to test it *'properly'*. Terms like Feature tests, Unit tests, Integration tests, and Acceptance tests are overwhelming.

But It's easier than most people think. You just need to write the test code that confirms whether your web app code does what it is supposed to do. Nothing more, nothing less.

### Example

Let's take an example to make it clear. We have an open-source package called [Laravel Log Enhancer](https://github.com/freshbitsweb/laravel-log-enhancer) that adds contextual data to the Laravel log entries.

The default Laravel log entry looks something like this:

![before.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649140045385/7OucB0ro1.png)

And the package improves it to:

![after.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649140099286/LH-2op7Ht.png)

This package has been downloaded 222k+ times but let me tell you a secret 🤫. It is just connecting the dots and has a tiny codebase.

Laravel uses the [Monolog](https://github.com/Seldaek/monolog) library to manage the logging module. And Monolog offers something called [processors](https://seldaek.github.io/monolog/doc/01-usage.html#using-processors) to add extra data for all the log entries.

Okay, with the introduction out of our way, we can take a look at some code:

```PHP
foreach ($logger->getHandlers() as $handler) {
    if (config('laravel_log_enhancer.log_request_details')) {
        $handler->pushProcessor(new WebProcessor);
    }

    if (config('laravel_log_enhancer.log_memory_usage')) {
        $handler->pushProcessor(new MemoryUsageProcessor);
    }

    $handler->pushProcessor(new RequestDataProcessor);
}
```

This piece of code is the heart of the package. Based on the configuration, it is *just* pushing the processor(s) to the Monolog handlers. The rest of the work is done by the Monolog library.

### Tests for our example

Theoretically, our log enhancer package is supposed to add extra data to the log entries. Hence we should write automated tests that verify that behavior. Right?

Should it be something like:

```PHP
/** @test */
public function it_adds_extra_data_to_the_log_entries()
{
    // Given
    config(['laravel_log_enhancer.log_memory_usage' => true]);
    config(['laravel_log_enhancer.log_request_details' => true]);

    // When
    // Run the code that logs something

    // Then
    // Fetch the log entry and check whether extra details are present in it
}
```

No!!

The package is only pushing some processors to the handlers. It shouldn't worry about what happens to the final log entry. It is the responsibility of the Monolog tests to verify that part. Makes sense? Good.

With that understanding, let's take a look at the actual test code from the package:

```PHP
/** @test */
public function it_adds_respective_processors_to_the_log_handler()
{
    config(['laravel_log_enhancer.log_memory_usage' => true]);
    config(['laravel_log_enhancer.log_request_details' => true]);
    $logger = $this->app[Logger::class];

    $handlers = $logger->getHandlers();
    foreach ($handlers as $handler) {
        $this->assertInstanceOf(RequestDataProcessor::class, $handler->popProcessor());
        $this->assertInstanceOf(MemoryUsageProcessor::class, $handler->popProcessor());
        $this->assertInstanceOf(WebProcessor::class, $handler->popProcessor());
    }
}
```

This test is confirming exactly what our package is doing: adding processors to the handlers. Nothing more, nothing less.


### Rest

I hope this example helps you decide the boundaries of your tests and increase your confidence in writing them.

Do you have any other examples in mind that explain what to test? Feel free to comment and share. See you!

---

_Credits - Cover Photo by <a href="https://unsplash.com/@pawelskor?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Paul Skorupskas</a> on <a href="https://unsplash.com/@pawelskor?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>_