Skip to content

Testing

Kevin Lee edited this page Feb 12, 2016 · 2 revisions

Laravel comes integrated with PHPUnit to do unit testing.

Seeding

You can seed your database with dummy data for your tests. Seeders are placed in the database/seeds directory. A seeder class only contains one method by default: run. This method is called when the db:seed Artisan command is executed. Within the run method, you may insert data into your database however you wish.

For example, within the DefaultUsersSeeder, we create an admin user:

<?php

use Illuminate\Database\Seeder;

class DefaultUsersSeeder extends Seeder
{

    public function run()
    {
        \App\Domain\Entities\User::create([
            'firstname' => 'Administrator',
            'lastname'  => '',
            'email'     => 'admin',
            'password'  => 'admin'
        ])->save();
    }
}

Calling seeders

Seeders have to be explicitly called in DatabaseSeeder to be executed during the php artisan db:seed command:

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{

    public function run()
    {
        $this->call('DefaultUsersSeeder');
    }
}

Running Tests

Tests are found within the tests directory. Please review the Laravel documentation for creating tests.

Tests can be run by executing the phpunit command from the project root directory in the console. Specific unit test classes can be executed by passing the path for the test file to the command.

Clone this wiki locally