dennislindsey / artisan-test-helpers by dennislindsey

PHP Artisan Test Helpers
277
0
1
Package Data
Maintainer Username: dennislindsey
Maintainer Contact: denniseblindsey@gmail.com (Dennis Lindsey)
Package Create Date: 2016-12-19
Package Last Update: 2017-01-03
Language: PHP
License: Unknown
Last Refreshed: 2019-11-11 03:36:17
Package Statistics
Total Downloads: 277
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

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