https://github.com/ssi-anik/testbench-lumen
[Package] Lumen Testing Helper for Packages Development
https://github.com/ssi-anik/testbench-lumen
lumen package php phpunit testbench testing testing-tools
Last synced: 26 days ago
JSON representation
[Package] Lumen Testing Helper for Packages Development
- Host: GitHub
- URL: https://github.com/ssi-anik/testbench-lumen
- Owner: ssi-anik
- License: mit
- Created: 2021-04-11T03:31:51.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-03-26T23:25:36.000Z (about 2 years ago)
- Last Synced: 2025-04-19T18:16:48.873Z (about 1 month ago)
- Topics: lumen, package, php, phpunit, testbench, testing, testing-tools
- Language: PHP
- Homepage: https://packagist.org/packages/anik/testbench-lumen
- Size: 52.7 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
testbench-lumen
[](https://codecov.io/gh/ssi-anik/testbench-lumen)
[](//packagist.org/packages/anik/testbench-lumen)
[](//packagist.org/packages/anik/testbench-lumen)
==============`anik/testbench-lumen` is a package, highly inspired by
the [orchestral/testbench](https://github.com/orchestral/testbench). `orchestral/testbench` that is a tool for testing
Laravel packages. Whereas the `anik/testbench-lumen` can only be used with [Lumen](https://github.com/laravel/lumen),
starting from Lumen `6.x` and afterwards.## Installation
```shell
composer require --dev anik/testbench-lumen
```## Documentation
- The package uses the `phpunit.xml`. Set up your environment variables in the `phpunit.xml` file.
```xml
// ...
```
**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](https://lumen.laravel.com/docs/master/testing).### 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 `[]`.```php
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````php
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 `[]`.```php
set(['my-package.enabled' => false]);
}
```### Annotations
There are three types of annotations considered during the test run. All the annotated tasks are executed synchronously.
All the tasks will receive the `\Laravel\Lumen\Application\Application` instance in their parameter.- `@pre-service-register` - Annotated tasks will run before the service providers are being registered. Maybe
useful for modifying values in config before the service provider gets registered.
- `@post-service-register` - Annotated tasks will run after the service providers are being registered.
- `@setup-before` - Annotated tasks will run after the application has boot properly and before running each testcases.
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. Only the method level
annotations are executed.See [Annotation Test class](https://github.com/ssi-anik/testbench-lumen/blob/main/tests/Integration/AnnotationTest.php)
to get the hang of it.```php
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->assertEquals('modified', $this->app->make('value-should-be-found'));
}public function defineEnvironmentVariables(Application $app)
{
$app['config']->set(['testbench-lumen.enabled' => true]);
}/**
* @pre-service-register defineEnvironmentVariables
*/
public function testDefineEnvAnnotation()
{
$this->assertEquals(true, $this->app['config']->get('testbench-lumen.enabled'));
}
```---
### Examples
All the scenarios are covered with tests. You can use them as examples.