xref: /webtrees/tests/TestCase.php (revision 8136679edbff8170fb92bd6e8dd8df8eb645bb1a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 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\Http\Controllers\GedcomFileController;
21use Fisharebest\Webtrees\Module\ModuleThemeInterface;
22use Fisharebest\Webtrees\Module\WebtreesTheme;
23use Fisharebest\Webtrees\Schema\SeedDatabase;
24use Fisharebest\Webtrees\Services\TimeoutService;
25use Illuminate\Cache\ArrayStore;
26use Illuminate\Cache\Repository;
27use Illuminate\Database\Capsule\Manager as DB;
28use function basename;
29use Symfony\Component\HttpFoundation\Request;
30
31/**
32 * Base class for unit tests
33 */
34class TestCase extends \PHPUnit\Framework\TestCase
35{
36    protected static $uses_database = false;
37
38    /**
39     * Things to run once, before all the tests.
40     */
41    public static function setUpBeforeClass()
42    {
43        parent::setUpBeforeClass();
44
45        if (static::$uses_database) {
46            defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/');
47
48            static::createTestDatabase();
49        }
50    }
51
52    /**
53     * Things to run once, AFTER all the tests.
54     */
55    public static function tearDownAfterClass()
56    {
57        if (static::$uses_database) {
58            $pdo = DB::connection()->getPdo();
59            unset($pdo);
60        }
61
62        parent::tearDownAfterClass();
63    }
64
65    /**
66     * Things to run before every test.
67     */
68    protected function setUp()
69    {
70        parent::setUp();
71
72        // Use an array cache for database calls, etc.
73        app()->instance('cache.array', new Repository(new ArrayStore()));
74
75        app()->bind(Tree::class, function () {
76            return null;
77        });
78
79        app()->instance(User::class, User::visitor());
80
81        app()->instance(Request::class, Request::createFromGlobals());
82
83        app()->bind(ModuleThemeInterface::class, WebtreesTheme::class);
84
85        defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/');
86        defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/');
87        defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/');
88        defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US'));
89
90        if (static::$uses_database) {
91            DB::connection()->beginTransaction();
92        }
93    }
94
95    /**
96     * Things to run after every test
97     */
98    protected function tearDown()
99    {
100        if (static::$uses_database) {
101            DB::connection()->rollBack();
102        }
103
104        app('cache.array')->flush();
105
106        Site::$preferences = [];
107        Tree::$trees = [];
108        GedcomRecord::$gedcom_record_cache = null;
109        GedcomRecord::$pending_record_cache = null;
110
111        Auth::logout();
112    }
113
114    /**
115     * Create an SQLite in-memory database for testing
116     */
117    protected static function createTestDatabase(): void
118    {
119        $capsule = new DB();
120        $capsule->addConnection([
121            'driver'   => 'sqlite',
122            'database' => ':memory:',
123        ]);
124        $capsule->setAsGlobal();
125        Database::registerMacros();
126
127        // Create tables
128        Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
129
130        // Create config data
131        (new SeedDatabase())->run();
132    }
133
134    /**
135     * Import a GEDCOM file into the test database.
136     *
137     * @param string $gedcom_file
138     *
139     * @return Tree
140     */
141    protected function importTree(string $gedcom_file): Tree
142    {
143        $tree = Tree::create(basename($gedcom_file), basename($gedcom_file));
144        $tree->importGedcomFile(__DIR__ . '/data/' . $gedcom_file, $gedcom_file);
145
146        View::share('tree', $tree);
147        $gedcom_file_controller = new GedcomFileController();
148
149        do {
150            $gedcom_file_controller->import(new TimeoutService(microtime(true)), $tree);
151
152            $imported = $tree->getPreference('imported');
153        } while (!$imported);
154
155        return $tree;
156    }
157}
158