1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees; 21 22use Aura\Router\Route; 23use Aura\Router\RouterContainer; 24use Fig\Http\Message\RequestMethodInterface; 25use Fisharebest\Webtrees\Http\Controllers\GedcomFileController; 26use Fisharebest\Webtrees\Http\Routes\WebRoutes; 27use Fisharebest\Webtrees\Module\ModuleThemeInterface; 28use Fisharebest\Webtrees\Module\WebtreesTheme; 29use Fisharebest\Webtrees\Services\MigrationService; 30use Fisharebest\Webtrees\Services\ModuleService; 31use Fisharebest\Webtrees\Services\TimeoutService; 32use Fisharebest\Webtrees\Services\TreeService; 33use Illuminate\Database\Capsule\Manager as DB; 34use Illuminate\Database\Query\Builder; 35use League\Flysystem\Filesystem; 36use League\Flysystem\Memory\MemoryAdapter; 37use Nyholm\Psr7\Factory\Psr17Factory; 38use Psr\Http\Message\ResponseFactoryInterface; 39use Psr\Http\Message\ServerRequestFactoryInterface; 40use Psr\Http\Message\ServerRequestInterface; 41use Psr\Http\Message\StreamFactoryInterface; 42use Psr\Http\Message\UploadedFileFactoryInterface; 43use Psr\Http\Message\UploadedFileInterface; 44use Psr\Http\Message\UriFactoryInterface; 45use Symfony\Component\Cache\Adapter\NullAdapter; 46 47use function app; 48use function basename; 49use function filesize; 50use function http_build_query; 51use function microtime; 52 53use const UPLOAD_ERR_OK; 54 55/** 56 * Base class for unit tests 57 */ 58class TestCase extends \PHPUnit\Framework\TestCase 59{ 60 /** @var object */ 61 public static $mock_functions; 62 /** @var bool */ 63 protected static $uses_database = false; 64 65 /** 66 * Things to run once, before all the tests. 67 */ 68 public static function setUpBeforeClass() 69 { 70 parent::setUpBeforeClass(); 71 72 // Use nyholm as our PSR7 factory 73 app()->bind(ResponseFactoryInterface::class, Psr17Factory::class); 74 app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class); 75 app()->bind(StreamFactoryInterface::class, Psr17Factory::class); 76 app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class); 77 app()->bind(UriFactoryInterface::class, Psr17Factory::class); 78 79 // Disable the cache. 80 app()->instance('cache.array', new Cache(new NullAdapter())); 81 82 app()->bind(ModuleThemeInterface::class, WebtreesTheme::class); 83 84 // Need the routing table, to generate URLs. 85 $router_container = new RouterContainer('/'); 86 (new WebRoutes())->load($router_container->getMap()); 87 app()->instance(RouterContainer::class, $router_container); 88 89 I18N::init('en-US', true); 90 91 if (static::$uses_database) { 92 static::createTestDatabase(); 93 94 // Boot modules 95 (new ModuleService())->bootModules(new WebtreesTheme()); 96 } 97 } 98 99 /** 100 * Things to run once, AFTER all the tests. 101 */ 102 public static function tearDownAfterClass() 103 { 104 if (static::$uses_database) { 105 $pdo = DB::connection()->getPdo(); 106 unset($pdo); 107 } 108 109 parent::tearDownAfterClass(); 110 } 111 112 /** 113 * Create an SQLite in-memory database for testing 114 */ 115 protected static function createTestDatabase(): void 116 { 117 $capsule = new DB(); 118 $capsule->addConnection([ 119 'driver' => 'sqlite', 120 'database' => ':memory:', 121 ]); 122 $capsule->setAsGlobal(); 123 124 // Migrations create logs, which requires an IP address, which requires a request 125 self::createRequest(); 126 127 // Create tables 128 $migration_service = new MigrationService(); 129 $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 130 131 // Create config data 132 $migration_service->seedDatabase(); 133 } 134 135 /** 136 * Create a request and bind it into the container. 137 * 138 * @param string $method 139 * @param string[] $query 140 * @param string[] $params 141 * @param UploadedFileInterface[] $files 142 * @param string[] $attributes 143 * 144 * @return ServerRequestInterface 145 */ 146 protected static function createRequest( 147 string $method = RequestMethodInterface::METHOD_GET, 148 array $query = [], 149 array $params = [], 150 array $files = [], 151 array $attributes = [] 152 ): ServerRequestInterface { 153 /** @var ServerRequestFactoryInterface */ 154 $server_request_factory = app(ServerRequestFactoryInterface::class); 155 156 $uri = 'https://webtrees.test/index.php?' . http_build_query($query); 157 158 /** @var ServerRequestInterface $request */ 159 $request = $server_request_factory 160 ->createServerRequest($method, $uri) 161 ->withQueryParams($query) 162 ->withParsedBody($params) 163 ->withUploadedFiles($files) 164 ->withAttribute('base_url', 'https://webtrees.test') 165 ->withAttribute('client-ip', '127.0.0.1') 166 ->withAttribute('user', new GuestUser()) 167 ->withAttribute('filesystem.data', new Filesystem(new MemoryAdapter())) 168 ->withAttribute('filesystem.data.name', 'data/') 169 ->withAttribute('route', new Route()); 170 171 foreach ($attributes as $key => $value) { 172 $request = $request->withAttribute($key, $value); 173 174 if ($key === 'tree') { 175 app()->instance(Tree::class, $value); 176 } 177 } 178 179 app()->instance(ServerRequestInterface::class, $request); 180 181 return $request; 182 } 183 184 /** 185 * Things to run before every test. 186 */ 187 protected function setUp(): void 188 { 189 parent::setUp(); 190 191 if (static::$uses_database) { 192 DB::connection()->beginTransaction(); 193 } 194 } 195 196 /** 197 * Things to run after every test 198 */ 199 protected function tearDown() 200 { 201 if (static::$uses_database) { 202 DB::connection()->rollBack(); 203 } 204 205 Site::$preferences = []; 206 GedcomRecord::$gedcom_record_cache = null; 207 GedcomRecord::$pending_record_cache = null; 208 209 Auth::logout(); 210 } 211 212 /** 213 * Import a GEDCOM file into the test database. 214 * 215 * @param string $gedcom_file 216 * 217 * @return Tree 218 */ 219 protected function importTree(string $gedcom_file): Tree 220 { 221 $tree_service = new TreeService(); 222 $tree = $tree_service->create(basename($gedcom_file), basename($gedcom_file)); 223 $stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file); 224 225 $tree->importGedcomFile($stream, $gedcom_file); 226 227 $timeout_service = new TimeoutService(microtime(true)); 228 $controller = new GedcomFileController($timeout_service); 229 $request = self::createRequest()->withAttribute('tree', $tree); 230 231 do { 232 $controller->import($request); 233 234 $imported = $tree->getPreference('imported'); 235 } while (!$imported); 236 237 return $tree; 238 } 239 240 /** 241 * Create an uploaded file for a request. 242 * 243 * @param string $filename 244 * @param string $mime_type 245 * 246 * @return UploadedFileInterface 247 */ 248 protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface 249 { 250 /** @var StreamFactoryInterface */ 251 $stream_factory = app(StreamFactoryInterface::class); 252 253 /** @var UploadedFileFactoryInterface */ 254 $uploaded_file_factory = app(UploadedFileFactoryInterface::class); 255 256 $stream = $stream_factory->createStreamFromFile($filename); 257 $size = filesize($filename); 258 $status = UPLOAD_ERR_OK; 259 $client_name = basename($filename); 260 261 return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type); 262 } 263} 264