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