summaryrefslogtreecommitdiff
path: root/tests/Unit/HasDatabase.php
blob: 3fe29e4a652a256fd5cbd0b8502f52e20689c9bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

namespace Engelsystem\Test\Unit;

use Engelsystem\Database\Database;
use Engelsystem\Database\Migration\Migrate;
use Engelsystem\Database\Migration\MigrationServiceProvider;
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use PDO;

trait HasDatabase
{
    /** @var Database */
    protected $database;

    /**
     * Setup in memory database
     */
    protected function initDatabase()
    {
        $dbManager = new CapsuleManager();
        $dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
        $dbManager->bootEloquent();

        $connection = $dbManager->getConnection();
        $connection->getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->database = new Database($connection);

        $this->app->instance(Database::class, $this->database);
        $this->app->register(MigrationServiceProvider::class);

        /** @var Migrate $migration */
        $migration = $this->app->get('db.migration');
        $migration->initMigration();

        $this->database
            ->getConnection()
            ->table('migrations')
            ->insert(
                [
                    ['migration' => '2018_01_01_000001_import_install_sql'],
                    ['migration' => '2018_01_01_000002_import_update_sql'],
                    ['migration' => '2018_01_01_000003_fix_old_tables'],
                    ['migration' => '2018_01_01_000004_cleanup_group_privileges'],
                    ['migration' => '2018_01_01_000005_add_angel_supporter_permissions'],
                    ['migration' => '2018_12_27_000000_fix_missing_arrival_dates'],
                    ['migration' => '2019_09_07_000000_migrate_admin_schedule_permissions'],
                ]
            );

        $migration->run(__DIR__ . '/../../db/migrations');
    }
}