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 20126654d7SGreg Roachuse Fisharebest\Webtrees\Http\Controllers\GedcomFileController; 21*8136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface; 22*8136679eSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme; 230115bc16SGreg Roachuse Fisharebest\Webtrees\Schema\SeedDatabase; 24126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService; 258b67c11aSGreg Roachuse Illuminate\Cache\ArrayStore; 268b67c11aSGreg Roachuse Illuminate\Cache\Repository; 270115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 28061b43d7SGreg Roachuse function basename; 29d4c04956SGreg Roachuse Symfony\Component\HttpFoundation\Request; 300115bc16SGreg Roach 3184e2cf4eSGreg Roach/** 3284e2cf4eSGreg Roach * Base class for unit tests 3384e2cf4eSGreg Roach */ 3484e2cf4eSGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase 3584e2cf4eSGreg Roach{ 36061b43d7SGreg Roach protected static $uses_database = false; 37061b43d7SGreg Roach 38061b43d7SGreg Roach /** 39061b43d7SGreg Roach * Things to run once, before all the tests. 40061b43d7SGreg Roach */ 41061b43d7SGreg Roach public static function setUpBeforeClass() 42061b43d7SGreg Roach { 43061b43d7SGreg Roach parent::setUpBeforeClass(); 44061b43d7SGreg Roach 45061b43d7SGreg Roach if (static::$uses_database) { 4632f20c14SGreg Roach defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/'); 4732f20c14SGreg Roach 48061b43d7SGreg Roach static::createTestDatabase(); 49061b43d7SGreg Roach } 50061b43d7SGreg Roach } 51061b43d7SGreg Roach 52061b43d7SGreg Roach /** 53061b43d7SGreg Roach * Things to run once, AFTER all the tests. 54061b43d7SGreg Roach */ 55061b43d7SGreg Roach public static function tearDownAfterClass() 56061b43d7SGreg Roach { 57061b43d7SGreg Roach if (static::$uses_database) { 58061b43d7SGreg Roach $pdo = DB::connection()->getPdo(); 5932f20c14SGreg Roach unset($pdo); 60061b43d7SGreg Roach } 61061b43d7SGreg Roach 62061b43d7SGreg Roach parent::tearDownAfterClass(); 63061b43d7SGreg Roach } 64061b43d7SGreg Roach 65061b43d7SGreg Roach /** 66061b43d7SGreg Roach * Things to run before every test. 67061b43d7SGreg Roach */ 680115bc16SGreg Roach protected function setUp() 690115bc16SGreg Roach { 700115bc16SGreg Roach parent::setUp(); 710115bc16SGreg Roach 728b67c11aSGreg Roach // Use an array cache for database calls, etc. 738b67c11aSGreg Roach app()->instance('cache.array', new Repository(new ArrayStore())); 748b67c11aSGreg Roach 75d4c04956SGreg Roach app()->bind(Tree::class, function () { 76d4c04956SGreg Roach return null; 77d4c04956SGreg Roach }); 78d4c04956SGreg Roach 79d4c04956SGreg Roach app()->instance(User::class, User::visitor()); 80d4c04956SGreg Roach 81d4c04956SGreg Roach app()->instance(Request::class, Request::createFromGlobals()); 82d4c04956SGreg Roach 83*8136679eSGreg Roach app()->bind(ModuleThemeInterface::class, WebtreesTheme::class); 84*8136679eSGreg Roach 85061b43d7SGreg Roach defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/'); 8632f20c14SGreg Roach defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/'); 87061b43d7SGreg Roach defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/'); 88061b43d7SGreg Roach defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US')); 89061b43d7SGreg Roach 90061b43d7SGreg Roach if (static::$uses_database) { 91061b43d7SGreg Roach DB::connection()->beginTransaction(); 92061b43d7SGreg Roach } 93061b43d7SGreg Roach } 94061b43d7SGreg Roach 95061b43d7SGreg Roach /** 96061b43d7SGreg Roach * Things to run after every test 97061b43d7SGreg Roach */ 98a49feabaSGreg Roach protected function tearDown() 99a49feabaSGreg Roach { 10032f20c14SGreg Roach if (static::$uses_database) { 101061b43d7SGreg Roach DB::connection()->rollBack(); 102061b43d7SGreg Roach } 10332f20c14SGreg Roach 1048b67c11aSGreg Roach app('cache.array')->flush(); 1058b67c11aSGreg Roach 10632f20c14SGreg Roach Site::$preferences = []; 10732f20c14SGreg Roach Tree::$trees = []; 108bec87e94SGreg Roach GedcomRecord::$gedcom_record_cache = null; 109bec87e94SGreg Roach GedcomRecord::$pending_record_cache = null; 11032f20c14SGreg Roach 11132f20c14SGreg Roach Auth::logout(); 1120115bc16SGreg Roach } 1130115bc16SGreg Roach 1140115bc16SGreg Roach /** 1150115bc16SGreg Roach * Create an SQLite in-memory database for testing 1160115bc16SGreg Roach */ 117061b43d7SGreg Roach protected static function createTestDatabase(): void 1180115bc16SGreg Roach { 1190115bc16SGreg Roach $capsule = new DB(); 1200115bc16SGreg Roach $capsule->addConnection([ 1210115bc16SGreg Roach 'driver' => 'sqlite', 1220115bc16SGreg Roach 'database' => ':memory:', 1230115bc16SGreg Roach ]); 1240115bc16SGreg Roach $capsule->setAsGlobal(); 125c88a5019SGreg Roach Database::registerMacros(); 1260115bc16SGreg Roach 1270115bc16SGreg Roach // Create tables 1280115bc16SGreg Roach Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 1290115bc16SGreg Roach 1300115bc16SGreg Roach // Create config data 1310115bc16SGreg Roach (new SeedDatabase())->run(); 1320115bc16SGreg Roach } 1330115bc16SGreg Roach 1340115bc16SGreg Roach /** 1350115bc16SGreg Roach * Import a GEDCOM file into the test database. 1360115bc16SGreg Roach * 1370115bc16SGreg Roach * @param string $gedcom_file 138061b43d7SGreg Roach * 139061b43d7SGreg Roach * @return Tree 1400115bc16SGreg Roach */ 141061b43d7SGreg Roach protected function importTree(string $gedcom_file): Tree 1420115bc16SGreg Roach { 143061b43d7SGreg Roach $tree = Tree::create(basename($gedcom_file), basename($gedcom_file)); 14469b58ac8SGreg Roach $tree->importGedcomFile(__DIR__ . '/data/' . $gedcom_file, $gedcom_file); 1450115bc16SGreg Roach 1461ad2dde6SGreg Roach View::share('tree', $tree); 147126654d7SGreg Roach $gedcom_file_controller = new GedcomFileController(); 148126654d7SGreg Roach 149126654d7SGreg Roach do { 1501ad2dde6SGreg Roach $gedcom_file_controller->import(new TimeoutService(microtime(true)), $tree); 151126654d7SGreg Roach 152126654d7SGreg Roach $imported = $tree->getPreference('imported'); 153126654d7SGreg Roach } while (!$imported); 154061b43d7SGreg Roach 155061b43d7SGreg Roach return $tree; 1560115bc16SGreg Roach } 15784e2cf4eSGreg Roach} 158