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