xref: /webtrees/tests/TestCase.php (revision 32f20c14b2839ce85ae9a14fd182d48b6c1989a6)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees;
19
20use Fisharebest\Webtrees\Schema\SeedDatabase;
21use Illuminate\Database\Capsule\Manager as DB;
22use function basename;
23use function file_get_contents;
24
25/**
26 * Base class for unit tests
27 */
28class TestCase extends \PHPUnit\Framework\TestCase
29{
30    protected static $uses_database = false;
31
32    /**
33     * Things to run once, before all the tests.
34     */
35    public static function setUpBeforeClass()
36    {
37        parent::setUpBeforeClass();
38
39        if (static::$uses_database) {
40            defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/');
41
42            static::createTestDatabase();
43        }
44    }
45
46    /**
47     * Things to run once, AFTER all the tests.
48     */
49    public static function tearDownAfterClass()
50    {
51        if (static::$uses_database) {
52            $pdo = DB::connection()->getPdo();
53            unset($pdo);
54        }
55
56        parent::tearDownAfterClass();
57    }
58
59    /**
60     * Things to run before every test.
61     */
62    protected function setUp()
63    {
64        parent::setUp();
65
66        defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/');
67        defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/');
68        defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/');
69        defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US'));
70
71        if (static::$uses_database) {
72            DB::connection()->beginTransaction();
73        }
74    }
75
76    /**
77     * Things to run after every test
78     */
79    protected function tearDown()
80    {
81        if (static::$uses_database) {
82            DB::connection()->rollBack();
83        }
84
85        Site::$preferences = [];
86        User::$cache = [];
87        Tree::$trees = [];
88
89        Auth::logout();
90    }
91
92    /**
93     * Create an SQLite in-memory database for testing
94     */
95    protected static function createTestDatabase(): void
96    {
97        $capsule = new DB();
98        $capsule->addConnection([
99            'driver'   => 'sqlite',
100            'database' => ':memory:',
101        ]);
102        $capsule->setAsGlobal();
103
104        // Create tables
105        Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
106
107        // Create config data
108        (new SeedDatabase())->run();
109    }
110
111    /**
112     * Import a GEDCOM file into the test database.
113     *
114     * @param string $gedcom_file
115     *
116     * @return Tree
117     */
118    protected function importTree(string $gedcom_file): Tree
119    {
120        $tree = Tree::create(basename($gedcom_file), basename($gedcom_file));
121
122        DB::table('gedcom_chunk')->insert([
123            'gedcom_id'  => $tree->id(),
124            'chunk_data' => file_get_contents($gedcom_file),
125        ]);
126
127        return $tree;
128    }
129}
130