andybeak
8/30/2016 - 3:41 PM

Speedy phpunit test execution in Laravel

Speedy phpunit test execution in Laravel

#!/bin/bash
rm -f storage/app/testing/database/test_database.sqlite || true
rm -f storage/app/testing/database/test_database.sqlite.backup || true

mkdir -p storage/app/testing/database/
touch storage/app/testing/database/test_database.sqlite
sqlite3 storage/app/testing/database/test_database.sqlite ".databases"
  /**
     * setupBeforeClass
     * @static
     * @access public
     */
    public function setup()
    {
        parent::setUp();

        Session::flush();

        Theme::configSet('themes_path', base_path('resources/testing/300/'));

        $start = microtime(true);

        \DB::commit();

        // if we're using a sqlite file lets check if there is an existing backup file and overwrite our
        // database with that - it's faster than migrating and seeding
        $databaseConnections = Config::get('database.connections');

        $databaseFile = $databaseConnections['sqlite_testing']['database'];

        if (strpos($databaseFile, 'memory') === false) {

            $backupFileName = $databaseFile . '.backup';

            if (file_exists($backupFileName) && filesize($backupFileName) > 0) {

                copy($backupFileName, $databaseFile);

                $seedTime = microtime(true) - $start;

                Log::info(__METHOD__ . " : Copying existing backup file took " . $seedTime);

            } else {

                Artisan::call('migrate:refresh', ['--force' => true]);

                $this->seed();

                $seedTime = microtime(true) - $start;

                Log::info(__METHOD__ . " : Migrate and Seed took " . $seedTime);

                copy($databaseFile, $backupFileName);

            }

        }

    }