1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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\Schema\SeedDatabase; 21use Illuminate\Database\Capsule\Manager as DB; 22use function basename; 23use function file_get_contents; 24 25/** 26 * Base class for unit tests 27 */ 28class TestCase extends \PHPUnit\Framework\TestCase 29{ 30 protected static $uses_database = false; 31 32 protected static $uses_transactions = false; 33 34 /** 35 * Things to run once, before all the tests. 36 */ 37 public static function setUpBeforeClass() 38 { 39 parent::setUpBeforeClass(); 40 41 defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/'); 42 defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/'); 43 defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/'); 44 45 if (static::$uses_database) { 46 static::createTestDatabase(); 47 } 48 } 49 50 /** 51 * Things to run once, AFTER all the tests. 52 */ 53 public static function tearDownAfterClass() 54 { 55 if (static::$uses_database) { 56 $pdo = DB::connection()->getPdo(); 57 $pdo = null; 58 } 59 60 parent::tearDownAfterClass(); 61 } 62 63 /** 64 * Things to run before every test. 65 */ 66 protected function setUp() 67 { 68 parent::setUp(); 69 70 defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/'); 71 defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/'); 72 defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/'); 73 defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US')); 74 75 if (static::$uses_database) { 76 DB::connection()->beginTransaction(); 77 } 78 } 79 80 /** 81 * Things to run after every test 82 */ 83 protected function tearDown() { 84 if (static::$uses_database && static::$uses_transactions) { 85 DB::connection()->rollBack(); 86 } 87 } 88 89 /** 90 * Create an SQLite in-memory database for testing 91 */ 92 protected static function createTestDatabase(): void 93 { 94 $capsule = new DB(); 95 $capsule->addConnection([ 96 'driver' => 'sqlite', 97 'database' => ':memory:', 98 ]); 99 $capsule->setAsGlobal(); 100 101 // Create tables 102 Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 103 104 // Create config data 105 (new SeedDatabase())->run(); 106 } 107 108 /** 109 * Import a GEDCOM file into the test database. 110 * 111 * @param string $gedcom_file 112 * 113 * @return Tree 114 */ 115 protected function importTree(string $gedcom_file): Tree 116 { 117 $tree = Tree::create(basename($gedcom_file), basename($gedcom_file)); 118 119 DB::table('gedcom_chunk')->insert([ 120 'gedcom_id' => $tree->id(), 121 'chunk_data' => file_get_contents($gedcom_file), 122 ]); 123 124 return $tree; 125 } 126} 127