xref: /webtrees/tests/TestCase.php (revision 32f20c14b2839ce85ae9a14fd182d48b6c1989a6)
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
200115bc16SGreg Roachuse Fisharebest\Webtrees\Schema\SeedDatabase;
210115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
22061b43d7SGreg Roachuse function basename;
230115bc16SGreg Roachuse function file_get_contents;
240115bc16SGreg Roach
2584e2cf4eSGreg Roach/**
2684e2cf4eSGreg Roach * Base class for unit tests
2784e2cf4eSGreg Roach */
2884e2cf4eSGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase
2984e2cf4eSGreg Roach{
30061b43d7SGreg Roach    protected static $uses_database = false;
31061b43d7SGreg Roach
32061b43d7SGreg Roach    /**
33061b43d7SGreg Roach     * Things to run once, before all the tests.
34061b43d7SGreg Roach     */
35061b43d7SGreg Roach    public static function setUpBeforeClass()
36061b43d7SGreg Roach    {
37061b43d7SGreg Roach        parent::setUpBeforeClass();
38061b43d7SGreg Roach
39061b43d7SGreg Roach        if (static::$uses_database) {
40*32f20c14SGreg Roach            defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/');
41*32f20c14SGreg Roach
42061b43d7SGreg Roach            static::createTestDatabase();
43061b43d7SGreg Roach        }
44061b43d7SGreg Roach    }
45061b43d7SGreg Roach
46061b43d7SGreg Roach    /**
47061b43d7SGreg Roach     * Things to run once, AFTER all the tests.
48061b43d7SGreg Roach     */
49061b43d7SGreg Roach    public static function tearDownAfterClass()
50061b43d7SGreg Roach    {
51061b43d7SGreg Roach        if (static::$uses_database) {
52061b43d7SGreg Roach            $pdo = DB::connection()->getPdo();
53*32f20c14SGreg Roach            unset($pdo);
54061b43d7SGreg Roach        }
55061b43d7SGreg Roach
56061b43d7SGreg Roach        parent::tearDownAfterClass();
57061b43d7SGreg Roach    }
58061b43d7SGreg Roach
59061b43d7SGreg Roach    /**
60061b43d7SGreg Roach     * Things to run before every test.
61061b43d7SGreg Roach     */
620115bc16SGreg Roach    protected function setUp()
630115bc16SGreg Roach    {
640115bc16SGreg Roach        parent::setUp();
650115bc16SGreg Roach
66061b43d7SGreg Roach        defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/');
67*32f20c14SGreg Roach        defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/');
68061b43d7SGreg Roach        defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/');
69061b43d7SGreg Roach        defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US'));
70061b43d7SGreg Roach
71061b43d7SGreg Roach        if (static::$uses_database) {
72061b43d7SGreg Roach            DB::connection()->beginTransaction();
73061b43d7SGreg Roach        }
74061b43d7SGreg Roach    }
75061b43d7SGreg Roach
76061b43d7SGreg Roach    /**
77061b43d7SGreg Roach     * Things to run after every test
78061b43d7SGreg Roach     */
79a49feabaSGreg Roach    protected function tearDown()
80a49feabaSGreg Roach    {
81*32f20c14SGreg Roach        if (static::$uses_database) {
82061b43d7SGreg Roach            DB::connection()->rollBack();
83061b43d7SGreg Roach        }
84*32f20c14SGreg Roach
85*32f20c14SGreg Roach        Site::$preferences = [];
86*32f20c14SGreg Roach        User::$cache = [];
87*32f20c14SGreg Roach        Tree::$trees = [];
88*32f20c14SGreg Roach
89*32f20c14SGreg Roach        Auth::logout();
900115bc16SGreg Roach    }
910115bc16SGreg Roach
920115bc16SGreg Roach    /**
930115bc16SGreg Roach     * Create an SQLite in-memory database for testing
940115bc16SGreg Roach     */
95061b43d7SGreg Roach    protected static function createTestDatabase(): void
960115bc16SGreg Roach    {
970115bc16SGreg Roach        $capsule = new DB();
980115bc16SGreg Roach        $capsule->addConnection([
990115bc16SGreg Roach            'driver'   => 'sqlite',
1000115bc16SGreg Roach            'database' => ':memory:',
1010115bc16SGreg Roach        ]);
1020115bc16SGreg Roach        $capsule->setAsGlobal();
1030115bc16SGreg Roach
1040115bc16SGreg Roach        // Create tables
1050115bc16SGreg Roach        Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
1060115bc16SGreg Roach
1070115bc16SGreg Roach        // Create config data
1080115bc16SGreg Roach        (new SeedDatabase())->run();
1090115bc16SGreg Roach    }
1100115bc16SGreg Roach
1110115bc16SGreg Roach    /**
1120115bc16SGreg Roach     * Import a GEDCOM file into the test database.
1130115bc16SGreg Roach     *
1140115bc16SGreg Roach     * @param string $gedcom_file
115061b43d7SGreg Roach     *
116061b43d7SGreg Roach     * @return Tree
1170115bc16SGreg Roach     */
118061b43d7SGreg Roach    protected function importTree(string $gedcom_file): Tree
1190115bc16SGreg Roach    {
120061b43d7SGreg Roach        $tree = Tree::create(basename($gedcom_file), basename($gedcom_file));
1210115bc16SGreg Roach
1220115bc16SGreg Roach        DB::table('gedcom_chunk')->insert([
123061b43d7SGreg Roach            'gedcom_id'  => $tree->id(),
1240115bc16SGreg Roach            'chunk_data' => file_get_contents($gedcom_file),
1250115bc16SGreg Roach        ]);
126061b43d7SGreg Roach
127061b43d7SGreg Roach        return $tree;
1280115bc16SGreg Roach    }
12984e2cf4eSGreg Roach}
130