184e2cf4eSGreg Roach<?php 284e2cf4eSGreg Roach/** 384e2cf4eSGreg Roach * webtrees: online genealogy 484e2cf4eSGreg Roach * Copyright (C) 2018 webtrees development team 584e2cf4eSGreg Roach * This program is free software: you can redistribute it and/or modify 684e2cf4eSGreg Roach * it under the terms of the GNU General Public License as published by 784e2cf4eSGreg Roach * the Free Software Foundation, either version 3 of the License, or 884e2cf4eSGreg Roach * (at your option) any later version. 984e2cf4eSGreg Roach * This program is distributed in the hope that it will be useful, 1084e2cf4eSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1184e2cf4eSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1284e2cf4eSGreg Roach * GNU General Public License for more details. 1384e2cf4eSGreg Roach * You should have received a copy of the GNU General Public License 1484e2cf4eSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 1584e2cf4eSGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1884e2cf4eSGreg Roachnamespace Fisharebest\Webtrees; 1984e2cf4eSGreg Roach 200115bc16SGreg Roachuse Fisharebest\Webtrees\Schema\SeedDatabase; 210115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 22061b43d7SGreg Roachuse function basename; 230115bc16SGreg Roachuse function file_get_contents; 240115bc16SGreg Roach 2584e2cf4eSGreg Roach/** 2684e2cf4eSGreg Roach * Base class for unit tests 2784e2cf4eSGreg Roach */ 2884e2cf4eSGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase 2984e2cf4eSGreg Roach{ 30061b43d7SGreg Roach protected static $uses_database = false; 31061b43d7SGreg Roach 32061b43d7SGreg Roach protected static $uses_transactions = false; 33061b43d7SGreg Roach 34061b43d7SGreg Roach /** 35061b43d7SGreg Roach * Things to run once, before all the tests. 36061b43d7SGreg Roach */ 37061b43d7SGreg Roach public static function setUpBeforeClass() 38061b43d7SGreg Roach { 39061b43d7SGreg Roach parent::setUpBeforeClass(); 40061b43d7SGreg Roach 41061b43d7SGreg Roach defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/'); 42061b43d7SGreg Roach defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/'); 43061b43d7SGreg Roach defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/'); 44061b43d7SGreg Roach 45061b43d7SGreg Roach if (static::$uses_database) { 46061b43d7SGreg Roach static::createTestDatabase(); 47061b43d7SGreg Roach } 48061b43d7SGreg Roach } 49061b43d7SGreg Roach 50061b43d7SGreg Roach /** 51061b43d7SGreg Roach * Things to run once, AFTER all the tests. 52061b43d7SGreg Roach */ 53061b43d7SGreg Roach public static function tearDownAfterClass() 54061b43d7SGreg Roach { 55061b43d7SGreg Roach if (static::$uses_database) { 56061b43d7SGreg Roach $pdo = DB::connection()->getPdo(); 57061b43d7SGreg Roach $pdo = null; 58061b43d7SGreg Roach } 59061b43d7SGreg Roach 60061b43d7SGreg Roach parent::tearDownAfterClass(); 61061b43d7SGreg Roach } 62061b43d7SGreg Roach 63061b43d7SGreg Roach /** 64061b43d7SGreg Roach * Things to run before every test. 65061b43d7SGreg Roach */ 660115bc16SGreg Roach protected function setUp() 670115bc16SGreg Roach { 680115bc16SGreg Roach parent::setUp(); 690115bc16SGreg Roach 700115bc16SGreg Roach defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/'); 71061b43d7SGreg Roach defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/'); 72061b43d7SGreg Roach defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/'); 73061b43d7SGreg Roach defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US')); 74061b43d7SGreg Roach 75061b43d7SGreg Roach if (static::$uses_database) { 76061b43d7SGreg Roach DB::connection()->beginTransaction(); 77061b43d7SGreg Roach } 78061b43d7SGreg Roach } 79061b43d7SGreg Roach 80061b43d7SGreg Roach /** 81061b43d7SGreg Roach * Things to run after every test 82061b43d7SGreg Roach */ 83*a49feabaSGreg Roach protected function tearDown() 84*a49feabaSGreg Roach { 85061b43d7SGreg Roach if (static::$uses_database && static::$uses_transactions) { 86061b43d7SGreg Roach DB::connection()->rollBack(); 87061b43d7SGreg Roach } 880115bc16SGreg Roach } 890115bc16SGreg Roach 900115bc16SGreg Roach /** 910115bc16SGreg Roach * Create an SQLite in-memory database for testing 920115bc16SGreg Roach */ 93061b43d7SGreg Roach protected static function createTestDatabase(): void 940115bc16SGreg Roach { 950115bc16SGreg Roach $capsule = new DB(); 960115bc16SGreg Roach $capsule->addConnection([ 970115bc16SGreg Roach 'driver' => 'sqlite', 980115bc16SGreg Roach 'database' => ':memory:', 990115bc16SGreg Roach ]); 1000115bc16SGreg Roach $capsule->setAsGlobal(); 1010115bc16SGreg Roach 1020115bc16SGreg Roach // Create tables 1030115bc16SGreg Roach Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 1040115bc16SGreg Roach 1050115bc16SGreg Roach // Create config data 1060115bc16SGreg Roach (new SeedDatabase())->run(); 1070115bc16SGreg Roach } 1080115bc16SGreg Roach 1090115bc16SGreg Roach /** 1100115bc16SGreg Roach * Import a GEDCOM file into the test database. 1110115bc16SGreg Roach * 1120115bc16SGreg Roach * @param string $gedcom_file 113061b43d7SGreg Roach * 114061b43d7SGreg Roach * @return Tree 1150115bc16SGreg Roach */ 116061b43d7SGreg Roach protected function importTree(string $gedcom_file): Tree 1170115bc16SGreg Roach { 118061b43d7SGreg Roach $tree = Tree::create(basename($gedcom_file), basename($gedcom_file)); 1190115bc16SGreg Roach 1200115bc16SGreg Roach DB::table('gedcom_chunk')->insert([ 121061b43d7SGreg Roach 'gedcom_id' => $tree->id(), 1220115bc16SGreg Roach 'chunk_data' => file_get_contents($gedcom_file), 1230115bc16SGreg Roach ]); 124061b43d7SGreg Roach 125061b43d7SGreg Roach return $tree; 1260115bc16SGreg Roach } 12784e2cf4eSGreg Roach} 128