Package Data | |
---|---|
Maintainer Username: | darrynten |
Maintainer Contact: | crynobone@gmail.com (Mior Muhammad Zaki) |
Package Create Date: | 2019-10-29 |
Package Last Update: | 2020-03-19 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-08 03:21:09 |
Package Statistics | |
---|---|
Total Downloads: | 79 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 1 |
Total Forks: | 1 |
Total Open Issues: | 0 |
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.
Laravel | Testbench :---------|:---------- 5.0.x | 3.0.x 5.1.x | 3.1.x 5.2.x | 3.2.x 5.3.x | 3.3.x 5.4.x | 3.4.x 5.5.x | 3.5.x 5.6.x | 3.6.x 5.7.x | 3.7.x 5.8.x | 3.8.x 6.x | 4.x
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.
To install through composer, simply put the following in your composer.json
file:
{
"require-dev": {
"orchestra/testbench": "^4.0"
}
}
And then run composer install
from the terminal.
Above installation can also be simplify by using the following command:
composer require --dev "orchestra/testbench=^4.0"
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
{
//
}
To load your package service provider, override the getPackageProviders
.
protected function getPackageProviders($app)
{
return ['Acme\AcmeServiceProvider'];
}
To load your package alias, override the getPackageAliases
.
protected function getPackageAliases($app)
{
return [
'Acme' => 'Acme\Facade'
];
}
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
}
If you need to add something early in the application bootstrapping process, 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' => '',
]);
}
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>
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');
}
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');
}
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';
}
Package developer should be using ServiceProvider::loadMigrationsFrom()
feature to automatically handle migrations for packages.
$this->artisan('migrate', ['--database' => 'testbench'])->run();
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']);
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
}
0000_00_00_000000_create_package_test_tables.php
.tests/database/
.CreateUsersTable
to CreateUsersTestTable
or otherwise you may encounter composer class loader collision.Testbench include withFactories()
method to allow you to register custom model factory path for your test suite.
$this->withFactories(__DIR__.'/factories');
To see a working example of testbench including how to set your configuration, check the file:
There also 3rd party packages that extends Testbench:
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>
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:
App\Models\User
, orphp artisan app:name Acme
which would rename App\User
to Acme\User
.Replace orchestra/testbench
with orchestra/testbench-browser-kit
and follow the installation guide.