unicorn / lumen-testbench
Lumen fork of the orchestra/testbench Laravel Testing Helper for Packages Development
Requires
- php: >=7.2
- laravel/framework: ^6.4
- mockery/mockery: ^1.2.3
- orchestra/testbench-core: ^4.3
- phpunit/phpunit: ^8.3
- dev-master
- 4.x-dev
- v4.3.0
- v4.2.0
- v4.1.0
- v4.0.1
- v4.0.0
- 3.9.x-dev
- v3.9.2
- v3.9.1
- v3.9.0
- 3.8.x-dev
- v3.8.5
- v3.8.4
- v3.8.3
- v3.8.2
- v3.8.1
- v3.8.0
- 3.7.x-dev
- v3.7.7
- v3.7.6
- v3.7.5
- v3.7.4
- v3.7.3
- v3.7.2
- v3.7.1
- v3.7.0
- 3.6.x-dev
- v3.6.6
- v3.6.5
- v3.6.4
- v3.6.3
- v3.6.2
- v3.6.1
- v3.6.0
- 3.5.x-dev
- v3.5.5
- v3.5.4
- v3.5.3
- v3.5.2
- v3.5.1
- v3.5.0
- 3.4.x-dev
- v3.4.12
- v3.4.11
- v3.4.10
- v3.4.9
- v3.4.8
- v3.4.7
- v3.4.6
- v3.4.5
- v3.4.4
- v3.4.3
- v3.4.2
- v3.4.1
- v3.4.0
- 3.3.x-dev
- v3.3.7
- v3.3.6
- v3.3.5
- v3.3.4
- v3.3.3
- v3.3.2
- v3.3.1
- v3.3.0
- 3.2.x-dev
- v3.2.8
- v3.2.7
- v3.2.6
- v3.2.5
- v3.2.4
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- 3.1.x-dev
- v3.1.10
- v3.1.9
- v3.1.8
- v3.1.7
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.2.1
- v2.2.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
This package is auto-updated.
Last update: 2024-11-09 10:57:09 UTC
README
Testbench Component is a simple package that has been designed to help you write tests for your Laravel package, especially when there is routing involved.
- Version Compatibility
- Getting Started
- Installation
- Usage
- Example
- Alternative Testing
- Troubleshoot
- Changelog
Version Compatibility
Getting Started
Before going through the rest of this documentation, please take some time to read the Package Development section of Laravel's own documentation, if you haven't done so yet.
Installation
To install through composer, run the following command from terminal:
composer require --dev "orchestra/testbench"
Usage
To use Testbench Component, all you need to do is extend Orchestra\Testbench\TestCase
instead of PHPUnit\Framework\TestCase
. The fixture app
booted by Orchestra\Testbench\TestCase
is predefined to follow the base application skeleton of Laravel 6.
<?php class TestCase extends Orchestra\Testbench\TestCase { // }
Custom Service Provider
To load your package service provider, override the getPackageProviders
.
protected function getPackageProviders($app) { return ['Acme\AcmeServiceProvider']; }
Custom Aliases
To load your package alias, override the getPackageAliases
.
protected function getPackageAliases($app) { return [ 'Acme' => 'Acme\Facade' ]; }
Overriding setUp() method
Since Orchestra\Testbench\TestCase
replace Laravel's Illuminate\Foundation\Testing\TestCase
, if you need your own setUp()
implementation, do not forget to call parent::setUp()
:
/** * Setup the test environment. */ protected function setUp() { parent::setUp(); // Your code here }
Setup Environment
If you need to add something early in the application bootstrapping process (which executed between registering service providers and booting service providers) you could use the getEnvironmentSetUp()
method:
/** * Define environment setup. * * @param \Illuminate\Foundation\Application $app * @return void */ protected function getEnvironmentSetUp($app) { // Setup default database to use sqlite :memory: $app['config']->set('database.default', 'testbench'); $app['config']->set('database.connections.testbench', [ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', ]); }
Setup Environment using Annotation
New in Testbench Core 4.4 is the ability to use @environment-setup
annotation to customise use of getEnvironmentSetUp
specific for each test.
protected function useMySqlConnection($app) { $app->config->set('database.default', 'mysql'); } protected function useSqliteConnection($app) { $app->config->set('database.default', 'sqlite'); } /** * @environment-setup useMySqlConnection */ public function testItCanBeConnectedWithMySql() { // write your tests } /** * @environment-setup useSqliteConnection */ public function testItCanBeConnectedWithSqlite() { // write your tests }
Memory SQLite Connection
To reduce setup configuration, you could use testing
database connection (:memory:
with sqlite
driver) via setting it up under getEnvironmentSetUp()
or by defining it under PHPUnit Configuration File:
<phpunit> // ... <php> <env name="DB_CONNECTION" value="testing"/> </php> </phpunit>
Overriding Console Kernel
You can easily swap Console Kernel for application bootstrap by overriding resolveApplicationConsoleKernel()
method:
/** * Resolve application Console Kernel implementation. * * @param \Illuminate\Foundation\Application $app * @return void */ protected function resolveApplicationConsoleKernel($app) { $app->singleton('Illuminate\Contracts\Console\Kernel', 'Acme\Testbench\Console\Kernel'); }
Overriding HTTP Kernel
You can easily swap HTTP Kernel for application bootstrap by overriding resolveApplicationHttpKernel()
method:
/** * Resolve application HTTP Kernel implementation. * * @param \Illuminate\Foundation\Application $app * @return void */ protected function resolveApplicationHttpKernel($app) { $app->singleton('Illuminate\Contracts\Http\Kernel', 'Acme\Testbench\Http\Kernel'); }
Overriding Application Timezone
You can also easily override application default timezone, instead of the default "UTC"
:
/** * Get application timezone. * * @param \Illuminate\Foundation\Application $app * @return string|null */ protected function getApplicationTimezone($app) { return 'Asia/Kuala_Lumpur'; }
Using Migrations
Package developer should be using ServiceProvider::loadMigrationsFrom()
feature to automatically handle migrations for packages.
$this->artisan('migrate', ['--database' => 'testbench'])->run();
Using Laravel Migrations
By default Testbench doesn't execute the default Laravel migrations which include users
and password_resets
table. In order to run the migration just add the following command:
$this->loadLaravelMigrations();
You can also set specific database connection to be used by adding --database
options:
$this->loadLaravelMigrations(['--database' => 'testbench']);
Running Testing Migrations
To run migrations that are only used for testing purposes and not part of your package, add the following to your base test class:
/** * Setup the test environment. */ protected function setUp() { parent::setUp(); $this->loadMigrationsFrom(__DIR__ . '/database/migrations'); // and other test setup steps you need to perform }
Notes and Considerations
- Your migration files has to suite Laravel's convention, e.g.
0000_00_00_000000_create_package_test_tables.php
. - You may choose to put your migrations folder in
tests/database/
. - You may choose to change your test-migrations class name to be different from the published class names, e.g. from
CreateUsersTable
toCreateUsersTestTable
or otherwise you may encounter composer class loader collision.
Using Model Factories
Testbench include withFactories()
method to allow you to register custom model factory path for your test suite.
$this->withFactories(__DIR__.'/factories');
Example
To see a working example of testbench including how to set your configuration, check the file:
Alternative Testing
There also 3rd party packages that extends Testbench:
- Testbench with Laravel Dusk
- Testbench with BrowserKit
- Testbench with CodeCeption
- Testbench with PHPSpec
Troubleshoot
No supported encrypter found. The cipher and / or key length are invalid.
RuntimeException: No supported encrypter found. The cipher and / or key length are invalid.
This error would only occur if your test suite require usages of the encrypter. To solve this you can add a dummy APP_KEY
or use a specific key to your application/package phpunit.xml
.
<phpunit> // ... <php> <env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/> </php> </phpunit>
Why Testbench doesn't include any of the App
classes.
The reason Testbench remove all the classes is to make sure that you would never depends on it when developing Laravel Packages. Classes such as App\Http\Controllers\Controller
and App\User
are simple to be added but the problems with these classes is that it can be either:
- Removed, moved to other location such as
App\Models\User
, or - Renamed using
php artisan app:name Acme
which would renameApp\User
toAcme\User
.
Class 'GuzzleHttp\Client' not found
If you plan to use the new HTTP Client in Laravel 7, you need to include guzzlehttp/guzzle
to your package's composer.json
:
composer require "guzzlehttp/guzzle=^6.3.1"
We can't guarantee that any requirements in
laravel/laravel
will always be maintained as it is. Developer may remove any of the optional requirements such asguzzlehttp/guzzle
,fideloper/proxy
,fruitcake/laravel-cors
orlaravel/tinker
.
Missing Browser Kit support after testing on Laravel 5.4
Replace orchestra/testbench
with orchestra/testbench-browser-kit
and follow the installation guide.