Skip to content
This repository has been archived by the owner on May 25, 2019. It is now read-only.

dennislindsey/artisan-test-helpers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Artisan Test Helpers

This package adds 3 new artisan commands to your Laravel project to help make your TDD workflow a little easier:

php artisan make:featuretest {name}

php artisan make:unittest {name}

php artisan make:factory {class}

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.

Inspired by Adam Wathan's "Test Driven Laravel" course https://adamwathan.me/test-driven-laravel/

Setup

To get started, add the package to your composer.json file, under require-dev:

"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:

'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:

public function register()
{
    if ($this->app->environment() !== 'production') {
        $this->app->register(\DennisLindsey\ArtisanTestHelpers\Providers\ArtisanTestHelperServiceProvider::class);
    }
    // ...
}

Make sure to publish the stubs directories

$ php artisan vendor:publish --provider="DennisLindsey\ArtisanTestHelpers\Providers\ArtisanTestHelperServiceProvider"

Usage

$ php artisan make:featuretest NameOfFeature

This will generate a file in your project's /tests/feature/ directory called NameOfFeatureTest.php

<?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

<?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

<?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'),
//     ];
// });

About

Helper commands for working with phpunit tests

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages