lundi 20 avril 2020

Does a "simple" way to test package on Laravel and Lumen exist?

I'm writing a package for Laravel and I also would like it to be compatible with Lumen. At the moment, I'm using orchestral/testbench for testing my package, but it was made for Laravel testing, not for Lumen.

I'm looking for a way to be able to test both of them without changing my composer dependencies between test runs.

I saw that it is possible to install laravel/framework: ^7.0 and laravel/lumen-framework: ^7.0 at the same time so finally created a dummy application with a bootstrap.laravel.php and bootstrap.lumen.php.

For my tests, I created a bootstrap file for PHPUnit:

<?php
declare(strict_types=1);

use Illuminate\Foundation\Testing\TestCase as LaravelTestCase;
use Laravel\Lumen\Testing\TestCase as LumenTestCase;

require_once dirname(__DIR__) . '/vendor/autoload.php';

putenv('TEST_MODE=' . env('TEST_MODE', 'laravel'));

$mode = env('TEST_MODE', 'laravel');

if ($mode === 'lumen') {
    class_alias(LumenTestCase::class, 'Sandbox\\TestCase');
    require dirname(__DIR__) . '/sandbox/bootstrap/bootstrap.laravel.php';
} else {
    class_alias(LaravelTestCase::class, 'Sandbox\\TestCase');
    require dirname(__DIR__) . '/sandbox/bootstrap/bootstrap.lumen.php';
}

which allow my tests to extend the Sandbox\TestCase which points to the Laravel or Lumen test case depending of the TEST_MODE environment variable.

This workaround sounds pretty hacky and I was wondering if there was any standard to do this kind of tests.

Aucun commentaire:

Enregistrer un commentaire