xref: /webtrees/tests/TestCase.php (revision 0115bc1678cafd957052cd2d256563ad3e242cc1)
184e2cf4eSGreg Roach<?php
284e2cf4eSGreg Roach/**
384e2cf4eSGreg Roach * webtrees: online genealogy
484e2cf4eSGreg Roach * Copyright (C) 2018 webtrees development team
584e2cf4eSGreg Roach * This program is free software: you can redistribute it and/or modify
684e2cf4eSGreg Roach * it under the terms of the GNU General Public License as published by
784e2cf4eSGreg Roach * the Free Software Foundation, either version 3 of the License, or
884e2cf4eSGreg Roach * (at your option) any later version.
984e2cf4eSGreg Roach * This program is distributed in the hope that it will be useful,
1084e2cf4eSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1184e2cf4eSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1284e2cf4eSGreg Roach * GNU General Public License for more details.
1384e2cf4eSGreg Roach * You should have received a copy of the GNU General Public License
1484e2cf4eSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1584e2cf4eSGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1884e2cf4eSGreg Roachnamespace Fisharebest\Webtrees;
1984e2cf4eSGreg Roach
20*0115bc16SGreg Roachuse function basename;
21*0115bc16SGreg Roachuse Fisharebest\Webtrees\Schema\SeedDatabase;
22*0115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
23*0115bc16SGreg Roachuse function file_get_contents;
24*0115bc16SGreg Roach
2584e2cf4eSGreg Roach/**
2684e2cf4eSGreg Roach * Base class for unit tests
2784e2cf4eSGreg Roach */
2884e2cf4eSGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase
2984e2cf4eSGreg Roach{
30*0115bc16SGreg Roach    protected function setUp()
31*0115bc16SGreg Roach    {
32*0115bc16SGreg Roach        parent::setUp();
33*0115bc16SGreg Roach
34*0115bc16SGreg Roach        defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/');
35*0115bc16SGreg Roach        defined('WT_DATA_DIR') || define('WT_DATA_DIR', 'data/');
36*0115bc16SGreg Roach        defined('WT_ROOT') || define('WT_ROOT', '');
37*0115bc16SGreg Roach        I18N::init('en-US');
38*0115bc16SGreg Roach    }
39*0115bc16SGreg Roach
40*0115bc16SGreg Roach    /**
41*0115bc16SGreg Roach     * Create an SQLite in-memory database for testing
42*0115bc16SGreg Roach     */
43*0115bc16SGreg Roach    protected function createTestDatabase(): void
44*0115bc16SGreg Roach    {
45*0115bc16SGreg Roach        $capsule = new DB();
46*0115bc16SGreg Roach        $capsule->addConnection([
47*0115bc16SGreg Roach            'driver'   => 'sqlite',
48*0115bc16SGreg Roach            'database' => ':memory:',
49*0115bc16SGreg Roach        ]);
50*0115bc16SGreg Roach        $capsule->setAsGlobal();
51*0115bc16SGreg Roach
52*0115bc16SGreg Roach        // Create tables
53*0115bc16SGreg Roach        Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
54*0115bc16SGreg Roach
55*0115bc16SGreg Roach        // Create config data
56*0115bc16SGreg Roach        (new SeedDatabase())->run();
57*0115bc16SGreg Roach    }
58*0115bc16SGreg Roach
59*0115bc16SGreg Roach    /**
60*0115bc16SGreg Roach     * Import a GEDCOM file into the test database.
61*0115bc16SGreg Roach     *
62*0115bc16SGreg Roach     * @param string $gedcom_file
63*0115bc16SGreg Roach     */
64*0115bc16SGreg Roach    protected function importTree(string $gedcom_file): void
65*0115bc16SGreg Roach    {
66*0115bc16SGreg Roach        $x = DB::table('gedcom')->insert([
67*0115bc16SGreg Roach            'gedcom_name' => basename($gedcom_file),
68*0115bc16SGreg Roach        ]);
69*0115bc16SGreg Roach
70*0115bc16SGreg Roach        var_dump($x);exit;
71*0115bc16SGreg Roach
72*0115bc16SGreg Roach        DB::table('gedcom_chunk')->insert([
73*0115bc16SGreg Roach            'chunk_data' => file_get_contents($gedcom_file),
74*0115bc16SGreg Roach        ]);
75*0115bc16SGreg Roach    }
7684e2cf4eSGreg Roach}
77