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