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; 210115bc16SGreg Roachuse Fisharebest\Webtrees\Schema\SeedDatabase; 22126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService; 230115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 24061b43d7SGreg Roachuse function basename; 250115bc16SGreg Roachuse function file_get_contents; 260115bc16SGreg Roach 2784e2cf4eSGreg Roach/** 2884e2cf4eSGreg Roach * Base class for unit tests 2984e2cf4eSGreg Roach */ 3084e2cf4eSGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase 3184e2cf4eSGreg Roach{ 32061b43d7SGreg Roach protected static $uses_database = false; 33061b43d7SGreg Roach 34061b43d7SGreg Roach /** 35061b43d7SGreg Roach * Things to run once, before all the tests. 36061b43d7SGreg Roach */ 37061b43d7SGreg Roach public static function setUpBeforeClass() 38061b43d7SGreg Roach { 39061b43d7SGreg Roach parent::setUpBeforeClass(); 40061b43d7SGreg Roach 41061b43d7SGreg Roach if (static::$uses_database) { 4232f20c14SGreg Roach defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/'); 4332f20c14SGreg Roach 44061b43d7SGreg Roach static::createTestDatabase(); 45061b43d7SGreg Roach } 46061b43d7SGreg Roach } 47061b43d7SGreg Roach 48061b43d7SGreg Roach /** 49061b43d7SGreg Roach * Things to run once, AFTER all the tests. 50061b43d7SGreg Roach */ 51061b43d7SGreg Roach public static function tearDownAfterClass() 52061b43d7SGreg Roach { 53061b43d7SGreg Roach if (static::$uses_database) { 54061b43d7SGreg Roach $pdo = DB::connection()->getPdo(); 5532f20c14SGreg Roach unset($pdo); 56061b43d7SGreg Roach } 57061b43d7SGreg Roach 58061b43d7SGreg Roach parent::tearDownAfterClass(); 59061b43d7SGreg Roach } 60061b43d7SGreg Roach 61061b43d7SGreg Roach /** 62061b43d7SGreg Roach * Things to run before every test. 63061b43d7SGreg Roach */ 640115bc16SGreg Roach protected function setUp() 650115bc16SGreg Roach { 660115bc16SGreg Roach parent::setUp(); 670115bc16SGreg Roach 68061b43d7SGreg Roach defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/'); 6932f20c14SGreg Roach defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/'); 70061b43d7SGreg Roach defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/'); 71061b43d7SGreg Roach defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US')); 72061b43d7SGreg Roach 73061b43d7SGreg Roach if (static::$uses_database) { 74061b43d7SGreg Roach DB::connection()->beginTransaction(); 75061b43d7SGreg Roach } 76061b43d7SGreg Roach } 77061b43d7SGreg Roach 78061b43d7SGreg Roach /** 79061b43d7SGreg Roach * Things to run after every test 80061b43d7SGreg Roach */ 81a49feabaSGreg Roach protected function tearDown() 82a49feabaSGreg Roach { 8332f20c14SGreg Roach if (static::$uses_database) { 84061b43d7SGreg Roach DB::connection()->rollBack(); 85061b43d7SGreg Roach } 8632f20c14SGreg Roach 8732f20c14SGreg Roach Site::$preferences = []; 8832f20c14SGreg Roach User::$cache = []; 8932f20c14SGreg Roach Tree::$trees = []; 9032f20c14SGreg Roach 9132f20c14SGreg Roach Auth::logout(); 920115bc16SGreg Roach } 930115bc16SGreg Roach 940115bc16SGreg Roach /** 950115bc16SGreg Roach * Create an SQLite in-memory database for testing 960115bc16SGreg Roach */ 97061b43d7SGreg Roach protected static function createTestDatabase(): void 980115bc16SGreg Roach { 990115bc16SGreg Roach $capsule = new DB(); 1000115bc16SGreg Roach $capsule->addConnection([ 1010115bc16SGreg Roach 'driver' => 'sqlite', 1020115bc16SGreg Roach 'database' => ':memory:', 1030115bc16SGreg Roach ]); 1040115bc16SGreg Roach $capsule->setAsGlobal(); 1050115bc16SGreg Roach 1060115bc16SGreg Roach // Create tables 1070115bc16SGreg Roach Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 1080115bc16SGreg Roach 1090115bc16SGreg Roach // Create config data 1100115bc16SGreg Roach (new SeedDatabase())->run(); 1110115bc16SGreg Roach } 1120115bc16SGreg Roach 1130115bc16SGreg Roach /** 1140115bc16SGreg Roach * Import a GEDCOM file into the test database. 1150115bc16SGreg Roach * 1160115bc16SGreg Roach * @param string $gedcom_file 117061b43d7SGreg Roach * 118061b43d7SGreg Roach * @return Tree 1190115bc16SGreg Roach */ 120061b43d7SGreg Roach protected function importTree(string $gedcom_file): Tree 1210115bc16SGreg Roach { 122061b43d7SGreg Roach $tree = Tree::create(basename($gedcom_file), basename($gedcom_file)); 123*69b58ac8SGreg Roach $tree->importGedcomFile(__DIR__ . '/data/' . $gedcom_file, $gedcom_file); 1240115bc16SGreg Roach 125126654d7SGreg Roach $gedcom_file_controller = new GedcomFileController(); 126126654d7SGreg Roach 127126654d7SGreg Roach do { 128126654d7SGreg Roach $gedcom_file_controller->import(new TimeoutService(microtime()), $tree); 129126654d7SGreg Roach 130126654d7SGreg Roach $imported = $tree->getPreference('imported'); 131126654d7SGreg Roach } while (!$imported); 132061b43d7SGreg Roach 133061b43d7SGreg Roach return $tree; 1340115bc16SGreg Roach } 13584e2cf4eSGreg Roach} 136