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 Fig\Http\Message\StatusCodeInterface; 21use Fisharebest\Localization\Locale\LocaleEnUs; 22use Fisharebest\Localization\Locale\LocaleInterface; 23use Fisharebest\Webtrees\Contracts\UserInterface; 24use Fisharebest\Webtrees\Http\Controllers\GedcomFileController; 25use Fisharebest\Webtrees\Http\Request; 26use Fisharebest\Webtrees\Module\ModuleThemeInterface; 27use Fisharebest\Webtrees\Module\WebtreesTheme; 28use Fisharebest\Webtrees\Services\MigrationService; 29use Fisharebest\Webtrees\Services\TimeoutService; 30use Fisharebest\Webtrees\Services\UserService; 31use Illuminate\Cache\ArrayStore; 32use Illuminate\Cache\Repository; 33use Illuminate\Database\Capsule\Manager as DB; 34use League\Flysystem\Filesystem; 35use League\Flysystem\Memory\MemoryAdapter; 36use Nyholm\Psr7\Factory\Psr17Factory; 37use Psr\Http\Message\ResponseFactoryInterface; 38use Psr\Http\Message\ServerRequestFactoryInterface; 39use Psr\Http\Message\ServerRequestInterface; 40use Psr\Http\Message\StreamFactoryInterface; 41use Psr\Http\Message\UploadedFileFactoryInterface; 42use Psr\Http\Message\UploadedFileInterface; 43use Psr\Http\Message\UriFactoryInterface; 44use function app; 45use function basename; 46use function define; 47use function defined; 48use function filesize; 49use function http_build_query; 50use const UPLOAD_ERR_OK; 51 52/** 53 * Base class for unit tests 54 */ 55class TestCase extends \PHPUnit\Framework\TestCase implements StatusCodeInterface 56{ 57 /** @var bool */ 58 protected static $uses_database = false; 59 60 /** @var object */ 61 public static $mock_functions; 62 63 /** 64 * Things to run once, before all the tests. 65 */ 66 public static function setUpBeforeClass() 67 { 68 parent::setUpBeforeClass(); 69 70 // Use nyholm as our PSR7 factory 71 app()->bind(ResponseFactoryInterface::class, Psr17Factory::class); 72 app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class); 73 app()->bind(StreamFactoryInterface::class, Psr17Factory::class); 74 app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class); 75 app()->bind(UriFactoryInterface::class, Psr17Factory::class); 76 77 // Use an array cache for database calls, etc. 78 app()->instance('cache.array', new Repository(new ArrayStore())); 79 80 app()->bind(Tree::class, static function () { 81 return null; 82 }); 83 84 app()->instance(UserService::class, new UserService()); 85 app()->instance(UserInterface::class, new GuestUser()); 86 87 app()->instance(ServerRequestInterface::class, Request::create('http://localhost/index.php')); 88 app()->instance(Filesystem::class, new Filesystem(new MemoryAdapter())); 89 90 app()->bind(ModuleThemeInterface::class, WebtreesTheme::class); 91 app()->bind(LocaleInterface::class, LocaleEnUs::class); 92 93 defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/'); 94 defined('WT_DATA_DIR') || define('WT_DATA_DIR', Webtrees::ROOT_DIR . 'data/'); 95 defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US', null, true)); 96 97 if (static::$uses_database) { 98 static::createTestDatabase(); 99 } 100 } 101 102 /** 103 * Create an SQLite in-memory database for testing 104 */ 105 protected static function createTestDatabase(): void 106 { 107 $capsule = new DB(); 108 $capsule->addConnection([ 109 'driver' => 'sqlite', 110 'database' => ':memory:', 111 ]); 112 $capsule->setAsGlobal(); 113 Database::registerMacros(); 114 115 // Migrations create logs, which requires an IP address, which requires a request 116 self::createRequest(); 117 118 // Create tables 119 $migration_service = new MigrationService; 120 $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 121 122 // Create config data 123 $migration_service->seedDatabase(); 124 } 125 126 /** 127 * Things to run once, AFTER all the tests. 128 */ 129 public static function tearDownAfterClass() 130 { 131 if (static::$uses_database) { 132 $pdo = DB::connection()->getPdo(); 133 unset($pdo); 134 } 135 136 parent::tearDownAfterClass(); 137 } 138 139 /** 140 * Things to run before every test. 141 */ 142 protected function setUp() 143 { 144 parent::setUp(); 145 146 if (static::$uses_database) { 147 DB::connection()->beginTransaction(); 148 } 149 } 150 151 /** 152 * Things to run after every test 153 */ 154 protected function tearDown() 155 { 156 if (static::$uses_database) { 157 DB::connection()->rollBack(); 158 } 159 160 app('cache.array')->flush(); 161 162 Site::$preferences = []; 163 Tree::$trees = []; 164 GedcomRecord::$gedcom_record_cache = null; 165 GedcomRecord::$pending_record_cache = null; 166 167 Auth::logout(); 168 } 169 170 /** 171 * Import a GEDCOM file into the test database. 172 * 173 * @param string $gedcom_file 174 * 175 * @return Tree 176 */ 177 protected function importTree(string $gedcom_file): Tree 178 { 179 $tree = Tree::create(basename($gedcom_file), basename($gedcom_file)); 180 181 $stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file); 182 $tree->importGedcomFile($stream, $gedcom_file); 183 184 View::share('tree', $tree); 185 $gedcom_file_controller = new GedcomFileController(); 186 187 do { 188 $gedcom_file_controller->import(new TimeoutService(microtime(true)), $tree); 189 190 $imported = $tree->getPreference('imported'); 191 } while (!$imported); 192 193 return $tree; 194 } 195 196 /** 197 * Create a request and bind it into the container. 198 * 199 * @param string $method 200 * @param string[] $query 201 * @param string[] $params 202 * @param UploadedFileInterface[] $files 203 * 204 * @return ServerRequestInterface 205 */ 206 protected static function createRequest(string $method = 'GET', array $query = [], array $params = [], array $files = []): ServerRequestInterface 207 { 208 /** @var ServerRequestFactoryInterface */ 209 $server_request_factory = app(ServerRequestFactoryInterface::class); 210 211 $uri = 'http://localhost/index.php?' . http_build_query($query); 212 213 /** @var ServerRequestInterface $request */ 214 $request = $server_request_factory 215 ->createServerRequest($method, $uri) 216 ->withQueryParams($query) 217 ->withParsedBody($params) 218 ->withUploadedFiles($files); 219 220 app()->instance(ServerRequestInterface::class, $request); 221 222 return $request; 223 } 224 225 /** 226 * Create an uploaded file for a request. 227 * 228 * @param string $filename 229 * @param string $mime_type 230 * 231 * @return UploadedFileInterface 232 */ 233 protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface 234 { 235 /** @var StreamFactoryInterface */ 236 $stream_factory = app(StreamFactoryInterface::class); 237 238 /** @var UploadedFileFactoryInterface */ 239 $uploaded_file_factory = app(UploadedFileFactoryInterface::class); 240 241 $stream = $stream_factory->createStreamFromFile($filename); 242 243 $size = filesize($filename); 244 245 $status = UPLOAD_ERR_OK; 246 247 $client_name = basename($filename); 248 249 return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type); 250 } 251} 252