Skip to content

Tests Setup

JP Barbosa edited this page Mar 14, 2016 · 3 revisions

Tests Setup

Add environment verification to allow run the server with testing environment
nano bootstrap/app.php
...
if (!empty($argv) && in_array('--env=testing', $argv)) {
    $app->loadEnvironmentFrom('.env.testing');
}

return $app;
Copy env file for test environment
cp .env .env.testing
Change app environment and add database file for test
nano .env.testing
APP_ENV=testing
...
SQLITE_FILE=testing.sqlite
Change database with environment
nano config/database.php
...
'sqlite' => [
            'driver'   => 'sqlite',
            'database' => database_path(env('SQLITE_FILE', 'database.sqlite')),
            'prefix'   => '',
],
...
Create database
touch database/testing.sqlite
Migrate test database
php artisan migrate --env=testing
Add doctrine package with composer
composer require doctrine/dbal
Generate command to truncate database
php artisan make:console DbTruncate --command=db:truncate
Add truncate behavior and description
nano app/Console/Commands/DbTruncate.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;

class DbTruncate extends Command
{
    ...

    protected $description = 'Truncate tables on testing environment.';

    ...

    public function handle()
    {
        if (!\App::environment('testing')) {
            echo 'Truncate aborted. You are not on a testing environment.';
            return;
        }
        $tables = DB::connection()->getDoctrineSchemaManager()->listTableNames();

        foreach ($tables as $name) {
            //if you don't want to truncate migrations
            if ($name == 'migrations') {
                continue;
            }
            DB::table($name)->truncate();
        }
    }
}
Add db:truncate command to the app
nano app/Console/Kernel.php
class Kernel extends ConsoleKernel
{
    ...

    protected $commands = [
        ...
        \App\Console\Commands\DbTruncate::class,
    ];

    ...
}
Add Codeception package with composer
composer require codeception/codeception --dev
Generate basic Codeception files
vendor/bin/codecept bootstrap
Add Laravel5 to Unit module
nano tests/unit.suite.yml
class_name: UnitTester
modules:
    enabled:
        - Laravel5
        - Asserts
        - \Helper\Unit
Add Laravel5 and Rest modules for functional tests
nano tests/functional.suite.yml
class_name: FunctionalTester
modules:
    enabled:
        - Laravel5:
            cleanup: false
            environment_file: .env.testing
        - REST:
           depends: PhpBrowser
           url: 'http://localhost:8000'
        - \Helper\Functional
Verify if everything is ok
vendor/bin/codecept run
Codeception PHP Testing Framework v2.1.1
Powered by PHPUnit 4.7.7 by Sebastian Bergmann and contributors.

Acceptance Tests (0) ------------------------
---------------------------------------------

Functional Tests (0) ------------------------
---------------------------------------------

Unit Tests (0) ------------------------------
---------------------------------------------

Time: 174 ms, Memory: 13.50Mb

No tests executed!
Add tests setup to Git
git add .
git commit -m "Add tests setup"
Next step: Functional Tests
Clone this wiki locally