dev-master
9999999-devPHP Artisan Test Helpers
The Requires
- php >=5.6.0
- illuminate/console >=5
- laravel/framework >=5
by Dennis Lindsey
laravel test php lumen command helper artisan
Wallogit.com
2017 © Pedro Peláez
PHP Artisan Test Helpers
This package adds 3 new artisan commands to your Laravel project to help make your TDD workflow a little easier:, (*1)
php artisan make:featuretest {name}, (*2)
php artisan make:unittest {name}, (*3)
php artisan make:factory {class}, (*4)
A Laravel service provider is also included. This provider will generate directories named /stubs/ in your project's
/test/ and database directories, allowing you to customize the files that are generated by these commands., (*5)
Inspired by Adam Wathan's "Test Driven Laravel" course https://adamwathan.me/test-driven-laravel/, (*6)
To get started, add the package to your composer.json file, under require-dev:, (*7)
"dennislindsey/artisan-test-helpers": "dev-master"
Once you've run a composer update, you need to register the Laravel service provider. You may add it to your /config/app.php:, (*8)
'providers' => [
...
DennisLindsey\ArtisanTestHelpers\Providers\ArtisanTestHelperServiceProvider::class,
],
...Or alternatively, I suggest adding the following code to your app/Providers/AppServiceProvider.php file, within the register() method:, (*9)
public function register()
{
if ($this->app->environment() !== 'production') {
$this->app->register(\DennisLindsey\ArtisanTestHelpers\Providers\ArtisanTestHelperServiceProvider::class);
}
// ...
}
Make sure to publish the stubs directories, (*10)
$ php artisan vendor:publish --provider="DennisLindsey\ArtisanTestHelpers\Providers\ArtisanTestHelperServiceProvider"
$ php artisan make:featuretest NameOfFeature
This will generate a file in your project's /tests/feature/ directory called NameOfFeatureTest.php, (*11)
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class NameOfFeatureTest extends TestCase
{
// use DatabaseMigrations;
protected function setUp()
{
parent::setUp();
// Shared test setup code should be placed after parent::setUp()
}
/** @test */
function testNameOfFeature()
{
// Arrange
// Act
// Assert
}
}
$ php artisan make:unittest NameOfUnit
This will generate a file in your project's /tests/unit/ directory called NameOfUnitTest.php, (*12)
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class NameOfUnitTest extends TestCase
{
// use DatabaseMigrations;
protected function setUp()
{
parent::setUp();
// Shared test setup code should be placed after parent::setUp()
}
/** @test */
function testNameOfUnit()
{
// Arrange
// Act
// Assert
}
}
$ php artisan make:factory User
This will generate a file in your project's /database/factories/ directory called UserFactory.php, (*13)
<?php
$factory->define(App\User::class, function (Faker\Generator $faker) {
// static $password;
return [
// 'name' => $faker->name,
// 'email' => $faker->unique()->safeEmail,
// 'password' => $password ?: $password = bcrypt('secret'),
// 'remember_token' => str_random(10),
];
});
// $factory->state(App\User}::class, 'deleted', function ($faker) {
// return [
// 'deleted_at' => date('Y-m-d H:i:s'),
// ];
// });
PHP Artisan Test Helpers
laravel test php lumen command helper artisan