2017 © Pedro Peláez
 

library artisan-test-helpers

PHP Artisan Test Helpers

image

dennislindsey/artisan-test-helpers

PHP Artisan Test Helpers

  • Tuesday, January 3, 2017
  • by dennislindsey
  • Repository
  • 1 Watchers
  • 0 Stars
  • 271 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

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)

Setup

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"

Usage

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

The Versions

03/01 2017

dev-master

9999999-dev

PHP Artisan Test Helpers

  Sources   Download

The Requires

 

by Dennis Lindsey

laravel test php lumen command helper artisan