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 } 81 82 /** 83 * Things to run after every test 84 */ 85 protected function tearDown() 86 { 87 if (static::$uses_database) { 88 DB::connection()->rollBack(); 89 } 90 91 app('cache.array')->flush(); 92 93 Site::$preferences = []; 94 Tree::$trees = []; 95 GedcomRecord::$gedcom_record_cache = null; 96 GedcomRecord::$pending_record_cache = null; 97 98 Auth::logout(); 99 } 100 101 /** 102 * Create an SQLite in-memory database for testing 103 */ 104 protected static function createTestDatabase(): void 105 { 106 $capsule = new DB(); 107 $capsule->addConnection([ 108 'driver' => 'sqlite', 109 'database' => ':memory:', 110 ]); 111 $capsule->setAsGlobal(); 112 Database::registerMacros(); 113 114 // Create tables 115 Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 116 117 // Create config data 118 (new SeedDatabase())->run(); 119 } 120 121 /** 122 * Import a GEDCOM file into the test database. 123 * 124 * @param string $gedcom_file 125 * 126 * @return Tree 127 */ 128 protected function importTree(string $gedcom_file): Tree 129 { 130 $tree = Tree::create(basename($gedcom_file), basename($gedcom_file)); 131 $tree->importGedcomFile(__DIR__ . '/data/' . $gedcom_file, $gedcom_file); 132 133 View::share('tree', $tree); 134 $gedcom_file_controller = new GedcomFileController(); 135 136 do { 137 $gedcom_file_controller->import(new TimeoutService(microtime(true)), $tree); 138 139 $imported = $tree->getPreference('imported'); 140 } while (!$imported); 141 142 return $tree; 143 } 144} 145