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 91 Auth::logout(); 92 } 93 94 /** 95 * Create an SQLite in-memory database for testing 96 */ 97 protected static function createTestDatabase(): void 98 { 99 $capsule = new DB(); 100 $capsule->addConnection([ 101 'driver' => 'sqlite', 102 'database' => ':memory:', 103 ]); 104 $capsule->setAsGlobal(); 105 106 // Create tables 107 Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 108 109 // Create config data 110 (new SeedDatabase())->run(); 111 } 112 113 /** 114 * Import a GEDCOM file into the test database. 115 * 116 * @param string $gedcom_file 117 * 118 * @return Tree 119 */ 120 protected function importTree(string $gedcom_file): Tree 121 { 122 $tree = Tree::create(basename($gedcom_file), basename($gedcom_file)); 123 $tree->importGedcomFile(__DIR__ . '/data/' . $gedcom_file, $gedcom_file); 124 125 View::share('tree', $tree); 126 $gedcom_file_controller = new GedcomFileController(); 127 128 do { 129 $gedcom_file_controller->import(new TimeoutService(microtime(true)), $tree); 130 131 $imported = $tree->getPreference('imported'); 132 } while (!$imported); 133 134 return $tree; 135 } 136} 137