Laravel Snippets for a fast TDD workflow


A Page I refer to when I do TDD development with Laravel. Cover most of my use cases without going to the official docs. Will be updated continously.

Handy Artisan Commands


# Create Migrations
php artisan make:migration create_users_table --create=users
php artisan make:migration add_votes_to_users_table --table=users

# Create a test in the Feature directory...
php artisan make:test PurchaseTicketsTest

# Create a test in the Unit directory...
php artisan make:test UserTest --unit

# Laravel Dusk Testing Commands

php artisan dusk:make LoginTest

composer require --dev laravel/dusk
php artisan dusk:install
php artisan dusk

# Create a model and migrations
php artisan make:model Photo --migration

# Create a factory
php artisan make:factory PhotoFactory --model=Photo

# Create a Seeder
php artisan make:seeder UsersTableSeeder

# Create a Resource Controller
php artisan make:controller PhotosController --resource

# Create a Resource Controller with methods type hinted for route-model binding
php artisan make:controller PhotosController --resource --model=Photo

php artisan make:controller API/PhotosController --api
php artisan make:controller PhotosController --api

# Create a Policy
php artisan make:policy PostPolicy --model=Post

# Commands for Backpack (Copied from backpack documentation website)

# STEP 1. create a model, a request and a controller for the admin panel
php artisan backpack:crud tag #use singular, not plural

# STEP 2. add a route for this admin panel to routes/backpack/custom.php
php artisan backpack:base:add-custom-route "CRUD::resource('tag', 'TagCrudController');"

# STEP 3. add sidebar item to resources/views/vendor/backpack/base/inc/sidebar_content.blade.php
php artisan backpack:base:add-sidebar-content "<li><a href='{{ backpack_url('tag') }}'><i class='fa fa-tag'></i> <span>Tags</span></a></li>"

Feature & Unit Tests References

Factories Snippets

Defining Factories


$factory->state(App\User::class, 'delinquent', [
    'account_status' => 'delinquent',
]);

$factory->state(App\User::class, 'address', function ($faker) {
    return [
        'address' => $faker->address,
    ];
});

Using Factories

$user = factory(App\User::class)->make();

$users = factory(App\User::class, 3)->make();

$users = factory(App\User::class, 5)->states('delinquent')->make();

$users = factory(App\User::class, 5)->states('premium', 'delinquent')->make();

$user = factory(App\User::class)->make([
    'name' => 'Abigail',
]);

// Make and save to database
$user = factory(App\User::class)->create();

Making Factories

# https://github.com/JeffreyWay/council/blob/master/database/factories/ModelFactory.php

$factory->define(App\User::class, function (Faker\Generator $faker) {
    static $password;
    return [
        'name' => $faker->name,
        'username' => $faker->unique()->userName,
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('secret'),
        'remember_token' => str_random(10),
        'confirmed' => true
    ];
});

Controllers

https://github.com/adamwathan/laracon2017 (Controller Design)

https://github.com/ziming/is439-iot-power/blob/master/app/Http/Controllers/Api/ApiController.php (A good ApiController Super class)

Resource Controller

Verb URI Action Route Name
GET /photos index photos.index
GET /photos/create create photos.create
POST /photos store photos.store
GET /photos/{photo} show photos.show
GET /photos/{photo}/edit edit photos.edit
PUT/PATCH /photos/{photo} update photos.update
DELETE /photos/{photo} destroy photos.destroy