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