xref: /webtrees/tests/TestCase.php (revision a49feaba99650e3f877939452f9959345feeda67)
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    protected static $uses_transactions = false;
33
34    /**
35     * Things to run once, before all the tests.
36     */
37    public static function setUpBeforeClass()
38    {
39        parent::setUpBeforeClass();
40
41        defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/');
42        defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/');
43        defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/');
44
45        if (static::$uses_database) {
46            static::createTestDatabase();
47        }
48    }
49
50    /**
51     * Things to run once, AFTER all the tests.
52     */
53    public static function tearDownAfterClass()
54    {
55        if (static::$uses_database) {
56            $pdo = DB::connection()->getPdo();
57            $pdo = null;
58        }
59
60        parent::tearDownAfterClass();
61    }
62
63    /**
64     * Things to run before every test.
65     */
66    protected function setUp()
67    {
68        parent::setUp();
69
70        defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/');
71        defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/');
72        defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/');
73        defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US'));
74
75        if (static::$uses_database) {
76            DB::connection()->beginTransaction();
77        }
78    }
79
80    /**
81     * Things to run after every test
82     */
83    protected function tearDown()
84    {
85        if (static::$uses_database && static::$uses_transactions) {
86            DB::connection()->rollBack();
87        }
88    }
89
90    /**
91     * Create an SQLite in-memory database for testing
92     */
93    protected static function createTestDatabase(): void
94    {
95        $capsule = new DB();
96        $capsule->addConnection([
97            'driver'   => 'sqlite',
98            'database' => ':memory:',
99        ]);
100        $capsule->setAsGlobal();
101
102        // Create tables
103        Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
104
105        // Create config data
106        (new SeedDatabase())->run();
107    }
108
109    /**
110     * Import a GEDCOM file into the test database.
111     *
112     * @param string $gedcom_file
113     *
114     * @return Tree
115     */
116    protected function importTree(string $gedcom_file): Tree
117    {
118        $tree = Tree::create(basename($gedcom_file), basename($gedcom_file));
119
120        DB::table('gedcom_chunk')->insert([
121            'gedcom_id'  => $tree->id(),
122            'chunk_data' => file_get_contents($gedcom_file),
123        ]);
124
125        return $tree;
126    }
127}
128