[Package] Lumen Testing Helper for Packages Development

Overview

testbench-lumen codecov Total Downloads Latest Stable Version

anik/testbench-lumen is a package, highly inspired by the orchestral/testbench. orchestral/testbench that is a tool for testing Laravel packages. Whereas the anik/testbench-lumen can only be used with Lumen, starting from Lumen 6.x and afterwards.

Installation

composer require anik/testbench-lumen

Docs

  • The package uses the phpunit.xml. Set up your environment variables in the phpunit.xml file.
">
<phpunit>
    // ...
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
    php>
phpunit>

NOTE: The package doesn't use the .env file. You'll have to primarily set all your variables in your phpunit.xml file.

  • Your testcases should extend the \Anik\Testbench\TestCase class.
  • Your testcases will have access to the Lumen testing APIs.

Bootstrapping

The package internally boots the Lumen application for the test cases. While bootstrapping, you can add some functionalities.

  • afterApplicationCreated($callback) registers the callbacks that will be called after the application is created. If you register the callback after the application is created, it'll be fired immediately. The callback will access to the Laravel\Lumen\Application instance.
  • afterApplicationRefreshed($callback) registers the callbacks that will be called after the application is refreshed. If you register the callback after the application is refreshed, it'll be fired immediately. The callback will have access to the Laravel\Lumen\Application instance.
  • beforeApplicationDestroyed($callback) registers the callback that will be called before the application is getting destroyed. Will have access to the Laravel\Lumen\Application instance.
  • afterApplicationDestroyed($callback) registers the callback that will be called after the application has been destroyed. Will have access to the Laravel\Lumen\Application instance.

The application does not by default loads the facade and eloquent. If you need to enable

  • Facade, the return true from the withFacade method. Default: false.
  • Eloquent, the return true from the withEloquent method. Default: false.

To load your required service providers, you can return an array of providers from the serviceProviders() method. Default is [].



protected function serviceProviders(): array
{
    return [
        // AppServiceProvider::class,
        // FormRequestServiceProvider::class,
        // AmqpServiceProvider::class,
    ];
}

To add your middlewares, you can add both the global and route middlewares.

  • To add global middlewares, you have to return an array of middlewares from the globalMiddlewares method. The method has access to the Laravel\Lumen\Application instance.


protected function globalMiddlewares(Application $app): array
{
    return [
        // CorsMiddleware::class,
        // NewrelicMiddleware::class,
    ];
}
  • To add route middlewares, you have to return an associative array of middlewares from the routeMiddlewares. The method has access to the Laravel\Lumen\Application instance.


protected function routeMiddlewares(Application $app): array
{
    return [
        // 'auth' => Authenticate::class,
        // 'admin' => AdminMiddleware::class,
    ];
}

By default, the application has the access to the/ endpoint returning the app()->version() as the response. To define your routes for the test purpose, you can use the routes method. The method has access to the Laravel\Lumen\Routing\Router instance. Defining routes in this method is as same as writing methods in the routes/web.php or routes/api.php



protected function routes(Router $router): void
{
    $router->get('test-route', function () {
        return response()->json([
            'error' => false,
            'message' => 'Test route is executed'
        ], 202);
    });
}

If you don't want to report an Exception, you can use the dontReportExceptions method. The defined exceptions will not be reported. Default is [].



protected function dontReportExceptions(): array
{
    return [
        // AuthenticationException::class,
    ];
}

Annotations

If your tests need to perform some sort of actions before running it, i.e. changing environment values, binding something to the container, etc. then you can perform those actions with annotations @setup-before. The annotated tasks are executed in a synchronous manner, and the returned value from the previous task is carried to the next task. Only the method level annotations are executed. The first parameter to the task is the Laravel\Lumen\Application instance, and the second parameters will the returned value from the previous task.

See Annotation Test class to get the hang of it.



protected function firstCalled(Application $app)
{
    $app->bind('value-should-be-found', function () {
        return 'as-is';
    });
}

protected function secondCalled(Application $app)
{
    $app->bind('value-should-be-found', function () {
        return 'modified';
    });
}

/**
 * @setup-before firstCalled
 * @setup-before secondCalled
 */
public function testMultipleAnnotations()
{
    $this->assertTrue('modified' === $this->app->make('value-should-be-found'));
}

Examples

All the scenarios are covered with tests. You can use them as examples.

Issues? Bugs or Need a feature?

Not working as expected? Feel free to fork, work on it and send PRs. You're welcome.

Open Swoole IDE Helper

Open Swoole IDE Helper This repo works with Open Swoole since release version v4.7.1. This package contains IDE help files for OpenSwoole. You may use

Open Swoole 1 Oct 17, 2021
Laravel API Documentation Generator

Laravel API Documentation Generator Automatically generate your API documentation from your existing Laravel/Lumen/Dingo routes. php artisan apidoc:ge

Marcel Pociot 3.1k Oct 15, 2021
A cli tool for creating Laravel packages

Laravel Packager This package provides you with a simple tool to set up a new package and it will let you focus on the development of the package inst

JeroenG 1.1k Oct 18, 2021
⚙️ A Laravel package to decompose your installed packages, their dependencies, your app & server environment

Introduction Laravel Decomposer decomposes and lists all the installed packages and their dependencies along with the Laravel & the Server environment

LUBUS 501 Oct 12, 2021
Creates an 'artisan workflow:make' command to scaffold out a number of useful GitHub actions workflows for Laravel

Laravel workflow generator This creates a make:workflow artisan command to scaffold out a number of useful GitHub actions workflows for Laravel. Insta

Len Woodward 9 Jul 18, 2021
A MySQL Workbench plugin which exports a Model to Laravel 5 Migrations

MySQL Workbench Export Laravel 5 Migrations Plugin A MySQL Workbench plugin that allows for exporting a model to Laravel 5 migrations that follow PSR-

Brandon Eckenrode 889 Oct 16, 2021
A handful of tools for PHP developers.

Belt A handful of tools for PHP developers. Version 2.0.0 is out now. Clear documentation, improved tests and code quality. Installation In case you w

Ilya 738 Sep 4, 2021
This package extends the core file generators that are included with Laravel 5

Extended Migration Generators for Laravel 6, 7 and 8 Easily define the migration schema right in your make:migration command. The new commands this pa

Laracasts 2.4k Oct 18, 2021
ICEcoder is a browser based code editor, which provides a modern approach to building websites

ICEcoder Code editor awesomeness ...in your browser ICEcoder is a browser based code editor, which provides a modern approach to building websites. By

ICEcoder 1.3k Oct 5, 2021
A PHP-based sandboxing library with a full suite of configuration and validation options.

A full-scale PHP 5.4+ sandbox class that utilizes PHP-Parser to prevent sandboxed code from running unsafe code. It also utilizes FunctionParser to di

Corveda 165 Oct 15, 2021
🍪 Write gorgeous documentation for your products using Markdown inside your Laravel app.

LaRecipe Write gorgeous documentations for your products using Markdown inside your Laravel app. LaRecipe ?? LaRecipe is simply a code-driven package

Saleem Hadad 1.9k Oct 17, 2021
Module Generator Composer Package For Laravel

Module Generator Installation You can install the package via composer: composer require teacoders/module-generator Run the command below to publish t

Tea Coders 20 Sep 6, 2021
Prequel for Laravel. Clear and concise database management.

TL;DR? Test Prequel here! What is Prequel exactly? Prequel is meant to be a database management tool for Laravel to replace the need for separate stan

Protoqol 1.3k Oct 17, 2021
:rocket: A Smart CRUD Generator For Laravel

Features Generate your models, views, controllers, routes and migrations just in a few clicks. Models visualization through a graph presentation (New

Houssain Amrani 860 Oct 21, 2021
InfyOm Laravel Generator - API, Scaffold, Tests, CRUD Laravel Generator

InfyOm Laravel Generator Generate Admin Panels CRUDs and APIs in Minutes with tons of other features and customizations with 3 different themes. Read

InfyOmLabs (InfyOm Technologies) 3.3k Oct 13, 2021
⛔️ Laravel Tinx is archived and no longer maintained.

⛔️ Laravel Tinx (Deprecated) Laravel Tinx was archived on 12th December 2019 and is no longer maintained. Looking for a reloadable version of Laravel

James Furey 432 Oct 15, 2021
Generate migrations from existing database structures

Laravel Migration Generator Generate migrations from existing database structures, an alternative to the schema dump provided by Laravel. A primary us

Bennett Treptow 240 Oct 22, 2021