vendredi 18 mai 2018

Validating configuration files through Unit Tests?

I'm building an application where users can choose one of the defined timezones in config/timezones.php:

return [
    'malta'   => 'Europe/Malta',
    'manila'  => 'Asia/Manila',
    'newyork' => 'America/New_York',
];

This file has an array with an identifier and a valid time zone string (as found on PHP.net)

I'm trying to build this application based on test driven development, so naturally I wrote a unit test to check if the time changed correctly when a user changed its timezone:

namespace Tests\Unit;

use Carbon\Carbon;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class TimeZoneTest extends TestCase
{

    use RefreshDatabase;

    /** @test */
    public function the_date_and_time_will_be_displayed_based_on_the_timezone_of_the_user() {

        $user = factory(\App\User::class)->create([
            'timezone' => 'Asia/Manila' // +8 hours
        ]);

        $date = Carbon::create(2018, 1, 1, 0, 0, 0); // 2018-01-01 00:00:00

        $adjustedDate = $date->timezone($user->timezone)->format('Y-m-d H:i:s'); // +8 hours = 2018-01-01 08:00:00

        $this->assertEquals('2018-01-01 08:00:00', $adjustedDate);
    }
}

This works fine, but here's the problem: if I were to make a typo in the configuration file, the unit test will still pass but the application will break.

I could simply create another unit test where I loop and validate the items of my configuration file using in_array($timezone, timezone_identifiers_list()), but I am not sure if that belongs in a unit test. It feels to me that this only needs to be validated once upon committing your code or something.

My questions: how/where/when do I check if my configuration file is valid before it's deployed to production?

Aucun commentaire:

Enregistrer un commentaire