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