Laravel TDD Session – Create An Application in Laravel – Part 1

Laravel TDD Session - Create An Application in Laravel With TDD - Part 1

Introduction

Hi, Laravel TDD or Test Driven Development with Laravel is a hot topic today. This is the first article in the series of Laravel TDD. If you are new to Laravel TDD and want to get start. Then you are at right place.

If you are beginner in Laravel and don’t know where to start. You can read me previous article Laravel Beginners Session With MySQL and MongoDB – Part 1. This will help you setup a fresh Laravel application with step by step process.

If you are an intermediate or experienced Laravel developer and want to know how to get start Test Driven Development or TDD in Laravel. You can get start here.

Hard code developer section

Make a fresh Laravel installation

$ composer create-project --prefer-dist laravel/laravel forum

Change directory to project root

$ cd forum

Create a model, migration and resourceful controller

$ php artisan make:model Thread -mr

Now, go to thread migration and edit up() function with following code

public function up()
 {
     Schema::create('threads', function (Blueprint $table) {
         $table->increments('id');
         $table->integer('user_id');
         $table->string('title');
         $table->text('body');
         $table->timestamps();
     });
 }

Optional Section

Install Laravel 5 IDE Helper Generator

$ composer require --dev barryvdh/laravel-ide-helperlaravel

Install doctrine/dbal package

$ composer require doctrine/dbal

Add following code to register() method of app/Providers/AppServiceProvider.php

public function register()
 {
     if ($this->app->environment() !== 'production') {
         $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
     }
 }

Continue From Here…

Open MySQL database and create a new database called forum.

$ create database forum

Open .env file from Laravel project and edit your database configuration. Go to terminal and run migration

$ php artisan migrate

Create a model Reply from your terminal by running following command:

$ php artisan make:model Reply -mr

Open migration file of replies and modify up() method with following code:

public function up()
{
    Schema::create('replies', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('thread_id');
        $table->integer('user_id');
        $table->text('body');
        $table->timestamps();
    });
}

Run migration with following command:

$ php artisan migrate

Open database/factories/ModelFactory.php and add following code:

$factory->define(\App\Thread::class, function ($faker) {
    return [
        'user_id' => function () {
            return factory(\App\User::class)->create()->id;
        },
        'title' => $faker->sentence,
        'body' => $faker->paragraph
    ];
});

Laravel Tinker – Your New Friend

Open terminal and run following command:

$ php artisan tinker

It will give you access to Laravel’s tinker command. We shall use tinker for generating seeding data for our threads model. Run following command:

$ factory('App\Thread', 50);

It will fill 50 users and thread records in your database.

Add following code to your ModalFactory.php

$factory->define(\App\Reply::class, function ($faker) {
    return [
        'thread_id' => function () {
            return factory(\App\Thread::class)->create()->id;
        },
        'user_id' => function () {
            return factory(\App\User::class)->create()->id;
        },
        'body' => $faker->paragraph
    ];
});

Refresh your migration

$ php artisan migrate:refresh

This will delete all the seed data generated in previous step. So now run tinker below commands to generate Users, Threads and Replies

$ php artisan tinker
$ thread = factory('App\Thread', 50)->create(); 
$ $thread->each(function ($thread) { factory('App\Reply', 10)->create(['thread_id' => $thread->id]); });

First command will get you into Laravel tinker.

Second command will generate 50 users and threads in database and will assign thread object to $thread.

Third command will generate 10 replies for each thread.

This is the first part of series. We have a Laravel application with database setup and running. We will continue this in next post as we go along.

If you have any questions, post in comments section. If you like it share it with your friends and team.