184e2cf4eSGreg Roach<?php 284e2cf4eSGreg Roach/** 384e2cf4eSGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 584e2cf4eSGreg Roach * This program is free software: you can redistribute it and/or modify 684e2cf4eSGreg Roach * it under the terms of the GNU General Public License as published by 784e2cf4eSGreg Roach * the Free Software Foundation, either version 3 of the License, or 884e2cf4eSGreg Roach * (at your option) any later version. 984e2cf4eSGreg Roach * This program is distributed in the hope that it will be useful, 1084e2cf4eSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1184e2cf4eSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1284e2cf4eSGreg Roach * GNU General Public License for more details. 1384e2cf4eSGreg Roach * You should have received a copy of the GNU General Public License 1484e2cf4eSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 1584e2cf4eSGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1884e2cf4eSGreg Roachnamespace Fisharebest\Webtrees; 1984e2cf4eSGreg Roach 20e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 21126654d7SGreg Roachuse Fisharebest\Webtrees\Http\Controllers\GedcomFileController; 228136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface; 238136679eSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme; 240115bc16SGreg Roachuse Fisharebest\Webtrees\Schema\SeedDatabase; 25126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService; 26e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 278b67c11aSGreg Roachuse Illuminate\Cache\ArrayStore; 288b67c11aSGreg Roachuse Illuminate\Cache\Repository; 290115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 30*7def76c7SGreg Roachuse League\Flysystem\Filesystem; 31*7def76c7SGreg Roachuse League\Flysystem\Memory\MemoryAdapter; 32d4c04956SGreg Roachuse Symfony\Component\HttpFoundation\Request; 33*7def76c7SGreg Roachuse function basename; 340115bc16SGreg Roach 3584e2cf4eSGreg Roach/** 3684e2cf4eSGreg Roach * Base class for unit tests 3784e2cf4eSGreg Roach */ 3884e2cf4eSGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase 3984e2cf4eSGreg Roach{ 40061b43d7SGreg Roach protected static $uses_database = false; 41061b43d7SGreg Roach 42061b43d7SGreg Roach /** 43061b43d7SGreg Roach * Things to run once, before all the tests. 44061b43d7SGreg Roach */ 45061b43d7SGreg Roach public static function setUpBeforeClass() 46061b43d7SGreg Roach { 47061b43d7SGreg Roach parent::setUpBeforeClass(); 48061b43d7SGreg Roach 49061b43d7SGreg Roach if (static::$uses_database) { 5032f20c14SGreg Roach defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/'); 5132f20c14SGreg Roach 52061b43d7SGreg Roach static::createTestDatabase(); 53061b43d7SGreg Roach } 54061b43d7SGreg Roach } 55061b43d7SGreg Roach 56061b43d7SGreg Roach /** 57061b43d7SGreg Roach * Things to run once, AFTER all the tests. 58061b43d7SGreg Roach */ 59061b43d7SGreg Roach public static function tearDownAfterClass() 60061b43d7SGreg Roach { 61061b43d7SGreg Roach if (static::$uses_database) { 62061b43d7SGreg Roach $pdo = DB::connection()->getPdo(); 6332f20c14SGreg Roach unset($pdo); 64061b43d7SGreg Roach } 65061b43d7SGreg Roach 66061b43d7SGreg Roach parent::tearDownAfterClass(); 67061b43d7SGreg Roach } 68061b43d7SGreg Roach 69061b43d7SGreg Roach /** 70061b43d7SGreg Roach * Things to run before every test. 71061b43d7SGreg Roach */ 720115bc16SGreg Roach protected function setUp() 730115bc16SGreg Roach { 740115bc16SGreg Roach parent::setUp(); 750115bc16SGreg Roach 768b67c11aSGreg Roach // Use an array cache for database calls, etc. 778b67c11aSGreg Roach app()->instance('cache.array', new Repository(new ArrayStore())); 788b67c11aSGreg Roach 79d4c04956SGreg Roach app()->bind(Tree::class, function () { 80d4c04956SGreg Roach return null; 81d4c04956SGreg Roach }); 82d4c04956SGreg Roach 83e5a6b4d4SGreg Roach app()->instance(UserService::class, new UserService()); 84e5a6b4d4SGreg Roach app()->instance(UserInterface::class, new GuestUser()); 85d4c04956SGreg Roach 86d4c04956SGreg Roach app()->instance(Request::class, Request::createFromGlobals()); 87*7def76c7SGreg Roach app()->instance(Filesystem::class, new Filesystem(new MemoryAdapter())); 88d4c04956SGreg Roach 898136679eSGreg Roach app()->bind(ModuleThemeInterface::class, WebtreesTheme::class); 908136679eSGreg Roach 91061b43d7SGreg Roach defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/'); 9232f20c14SGreg Roach defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/'); 93061b43d7SGreg Roach defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/'); 94061b43d7SGreg Roach defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US')); 95061b43d7SGreg Roach 96061b43d7SGreg Roach if (static::$uses_database) { 97061b43d7SGreg Roach DB::connection()->beginTransaction(); 98061b43d7SGreg Roach } 99061b43d7SGreg Roach } 100061b43d7SGreg Roach 101061b43d7SGreg Roach /** 102061b43d7SGreg Roach * Things to run after every test 103061b43d7SGreg Roach */ 104a49feabaSGreg Roach protected function tearDown() 105a49feabaSGreg Roach { 10632f20c14SGreg Roach if (static::$uses_database) { 107061b43d7SGreg Roach DB::connection()->rollBack(); 108061b43d7SGreg Roach } 10932f20c14SGreg Roach 1108b67c11aSGreg Roach app('cache.array')->flush(); 1118b67c11aSGreg Roach 11232f20c14SGreg Roach Site::$preferences = []; 11332f20c14SGreg Roach Tree::$trees = []; 114bec87e94SGreg Roach GedcomRecord::$gedcom_record_cache = null; 115bec87e94SGreg Roach GedcomRecord::$pending_record_cache = null; 11632f20c14SGreg Roach 11732f20c14SGreg Roach Auth::logout(); 1180115bc16SGreg Roach } 1190115bc16SGreg Roach 1200115bc16SGreg Roach /** 1210115bc16SGreg Roach * Create an SQLite in-memory database for testing 1220115bc16SGreg Roach */ 123061b43d7SGreg Roach protected static function createTestDatabase(): void 1240115bc16SGreg Roach { 1250115bc16SGreg Roach $capsule = new DB(); 1260115bc16SGreg Roach $capsule->addConnection([ 1270115bc16SGreg Roach 'driver' => 'sqlite', 1280115bc16SGreg Roach 'database' => ':memory:', 1290115bc16SGreg Roach ]); 1300115bc16SGreg Roach $capsule->setAsGlobal(); 131c88a5019SGreg Roach Database::registerMacros(); 1320115bc16SGreg Roach 1330115bc16SGreg Roach // Create tables 1340115bc16SGreg Roach Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 1350115bc16SGreg Roach 1360115bc16SGreg Roach // Create config data 1370115bc16SGreg Roach (new SeedDatabase())->run(); 1380115bc16SGreg Roach } 1390115bc16SGreg Roach 1400115bc16SGreg Roach /** 1410115bc16SGreg Roach * Import a GEDCOM file into the test database. 1420115bc16SGreg Roach * 1430115bc16SGreg Roach * @param string $gedcom_file 144061b43d7SGreg Roach * 145061b43d7SGreg Roach * @return Tree 1460115bc16SGreg Roach */ 147061b43d7SGreg Roach protected function importTree(string $gedcom_file): Tree 1480115bc16SGreg Roach { 149061b43d7SGreg Roach $tree = Tree::create(basename($gedcom_file), basename($gedcom_file)); 15069b58ac8SGreg Roach $tree->importGedcomFile(__DIR__ . '/data/' . $gedcom_file, $gedcom_file); 1510115bc16SGreg Roach 1521ad2dde6SGreg Roach View::share('tree', $tree); 153126654d7SGreg Roach $gedcom_file_controller = new GedcomFileController(); 154126654d7SGreg Roach 155126654d7SGreg Roach do { 1561ad2dde6SGreg Roach $gedcom_file_controller->import(new TimeoutService(microtime(true)), $tree); 157126654d7SGreg Roach 158126654d7SGreg Roach $imported = $tree->getPreference('imported'); 159126654d7SGreg Roach } while (!$imported); 160061b43d7SGreg Roach 161061b43d7SGreg Roach return $tree; 1620115bc16SGreg Roach } 16384e2cf4eSGreg Roach} 164