xref: /webtrees/tests/TestCase.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
184e2cf4eSGreg Roach<?php
284e2cf4eSGreg Roach/**
384e2cf4eSGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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
20*6ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
21bd1e4e13SGreg Roachuse Fisharebest\Localization\Locale\LocaleEnUs;
22bd1e4e13SGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface;
23e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24126654d7SGreg Roachuse Fisharebest\Webtrees\Http\Controllers\GedcomFileController;
25*6ccdf4f0SGreg Roachuse Fisharebest\Webtrees\Http\Request;
268136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
278136679eSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme;
288c3e1068SGreg Roachuse Fisharebest\Webtrees\Services\MigrationService;
29126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService;
30e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
318b67c11aSGreg Roachuse Illuminate\Cache\ArrayStore;
328b67c11aSGreg Roachuse Illuminate\Cache\Repository;
330115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
347def76c7SGreg Roachuse League\Flysystem\Filesystem;
357def76c7SGreg Roachuse League\Flysystem\Memory\MemoryAdapter;
36*6ccdf4f0SGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory;
37*6ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
38*6ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface;
39*6ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
40*6ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
41*6ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface;
42*6ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileInterface;
43*6ccdf4f0SGreg Roachuse Psr\Http\Message\UriFactoryInterface;
44*6ccdf4f0SGreg Roachuse function app;
457def76c7SGreg Roachuse function basename;
46*6ccdf4f0SGreg Roachuse function define;
47*6ccdf4f0SGreg Roachuse function defined;
48*6ccdf4f0SGreg Roachuse function dirname;
49*6ccdf4f0SGreg Roachuse function filesize;
50*6ccdf4f0SGreg Roachuse function http_build_query;
51*6ccdf4f0SGreg Roachuse function rawurlencode;
52*6ccdf4f0SGreg Roachuse const UPLOAD_ERR_OK;
530115bc16SGreg Roach
5484e2cf4eSGreg Roach/**
5584e2cf4eSGreg Roach * Base class for unit tests
5684e2cf4eSGreg Roach */
57*6ccdf4f0SGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase implements StatusCodeInterface
5884e2cf4eSGreg Roach{
59061b43d7SGreg Roach    protected static $uses_database = false;
60061b43d7SGreg Roach
61061b43d7SGreg Roach    /**
62061b43d7SGreg Roach     * Things to run once, before all the tests.
63061b43d7SGreg Roach     */
64061b43d7SGreg Roach    public static function setUpBeforeClass()
65061b43d7SGreg Roach    {
66061b43d7SGreg Roach        parent::setUpBeforeClass();
67061b43d7SGreg Roach
68*6ccdf4f0SGreg Roach        // Use nyholm as our PSR7 factory
69*6ccdf4f0SGreg Roach        app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
70*6ccdf4f0SGreg Roach        app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
71*6ccdf4f0SGreg Roach        app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
72*6ccdf4f0SGreg Roach        app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
73*6ccdf4f0SGreg Roach        app()->bind(UriFactoryInterface::class, Psr17Factory::class);
74*6ccdf4f0SGreg Roach
75*6ccdf4f0SGreg Roach        // Use an array cache for database calls, etc.
76*6ccdf4f0SGreg Roach        app()->instance('cache.array', new Repository(new ArrayStore()));
77*6ccdf4f0SGreg Roach
78*6ccdf4f0SGreg Roach        app()->bind(Tree::class, static function () {
79*6ccdf4f0SGreg Roach            return null;
80*6ccdf4f0SGreg Roach        });
81*6ccdf4f0SGreg Roach
82*6ccdf4f0SGreg Roach        app()->instance(UserService::class, new UserService());
83*6ccdf4f0SGreg Roach        app()->instance(UserInterface::class, new GuestUser());
84*6ccdf4f0SGreg Roach
85*6ccdf4f0SGreg Roach        app()->instance(ServerRequestInterface::class, Request::create('http://localhost/index.php'));
86*6ccdf4f0SGreg Roach        app()->instance(Filesystem::class, new Filesystem(new MemoryAdapter()));
87*6ccdf4f0SGreg Roach
88*6ccdf4f0SGreg Roach        app()->bind(ModuleThemeInterface::class, WebtreesTheme::class);
89*6ccdf4f0SGreg Roach        app()->bind(LocaleInterface::class, LocaleEnUs::class);
90*6ccdf4f0SGreg Roach
91*6ccdf4f0SGreg Roach        defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/');
92*6ccdf4f0SGreg Roach        defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/');
93*6ccdf4f0SGreg Roach        defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/');
94*6ccdf4f0SGreg Roach        defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US', null, true));
95*6ccdf4f0SGreg Roach
96061b43d7SGreg Roach        if (static::$uses_database) {
9732f20c14SGreg Roach            defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/');
9832f20c14SGreg Roach
99061b43d7SGreg Roach            static::createTestDatabase();
100061b43d7SGreg Roach        }
101061b43d7SGreg Roach    }
102061b43d7SGreg Roach
103061b43d7SGreg Roach    /**
104*6ccdf4f0SGreg Roach     * Create an SQLite in-memory database for testing
105*6ccdf4f0SGreg Roach     */
106*6ccdf4f0SGreg Roach    protected static function createTestDatabase(): void
107*6ccdf4f0SGreg Roach    {
108*6ccdf4f0SGreg Roach        $capsule = new DB();
109*6ccdf4f0SGreg Roach        $capsule->addConnection([
110*6ccdf4f0SGreg Roach            'driver'   => 'sqlite',
111*6ccdf4f0SGreg Roach            'database' => ':memory:',
112*6ccdf4f0SGreg Roach        ]);
113*6ccdf4f0SGreg Roach        $capsule->setAsGlobal();
114*6ccdf4f0SGreg Roach        Database::registerMacros();
115*6ccdf4f0SGreg Roach
116*6ccdf4f0SGreg Roach        // Migrations create logs, which requires an IP address, which requires a request
117*6ccdf4f0SGreg Roach        self::createRequest();
118*6ccdf4f0SGreg Roach
119*6ccdf4f0SGreg Roach        // Create tables
120*6ccdf4f0SGreg Roach        $migration_service = new MigrationService;
121*6ccdf4f0SGreg Roach        $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
122*6ccdf4f0SGreg Roach
123*6ccdf4f0SGreg Roach        // Create config data
124*6ccdf4f0SGreg Roach        $migration_service->seedDatabase();
125*6ccdf4f0SGreg Roach    }
126*6ccdf4f0SGreg Roach
127*6ccdf4f0SGreg Roach    /**
128061b43d7SGreg Roach     * Things to run once, AFTER all the tests.
129061b43d7SGreg Roach     */
130061b43d7SGreg Roach    public static function tearDownAfterClass()
131061b43d7SGreg Roach    {
132061b43d7SGreg Roach        if (static::$uses_database) {
133061b43d7SGreg Roach            $pdo = DB::connection()->getPdo();
13432f20c14SGreg Roach            unset($pdo);
135061b43d7SGreg Roach        }
136061b43d7SGreg Roach
137061b43d7SGreg Roach        parent::tearDownAfterClass();
138061b43d7SGreg Roach    }
139061b43d7SGreg Roach
140061b43d7SGreg Roach    /**
141061b43d7SGreg Roach     * Things to run before every test.
142061b43d7SGreg Roach     */
1430115bc16SGreg Roach    protected function setUp()
1440115bc16SGreg Roach    {
1450115bc16SGreg Roach        parent::setUp();
1460115bc16SGreg Roach
147061b43d7SGreg Roach        if (static::$uses_database) {
148061b43d7SGreg Roach            DB::connection()->beginTransaction();
149061b43d7SGreg Roach        }
150061b43d7SGreg Roach    }
151061b43d7SGreg Roach
152061b43d7SGreg Roach    /**
153061b43d7SGreg Roach     * Things to run after every test
154061b43d7SGreg Roach     */
155a49feabaSGreg Roach    protected function tearDown()
156a49feabaSGreg Roach    {
15732f20c14SGreg Roach        if (static::$uses_database) {
158061b43d7SGreg Roach            DB::connection()->rollBack();
159061b43d7SGreg Roach        }
16032f20c14SGreg Roach
1618b67c11aSGreg Roach        app('cache.array')->flush();
1628b67c11aSGreg Roach
16332f20c14SGreg Roach        Site::$preferences                  = [];
16432f20c14SGreg Roach        Tree::$trees                        = [];
165bec87e94SGreg Roach        GedcomRecord::$gedcom_record_cache  = null;
166bec87e94SGreg Roach        GedcomRecord::$pending_record_cache = null;
16732f20c14SGreg Roach
16832f20c14SGreg Roach        Auth::logout();
1690115bc16SGreg Roach    }
1700115bc16SGreg Roach
1710115bc16SGreg Roach    /**
1720115bc16SGreg Roach     * Import a GEDCOM file into the test database.
1730115bc16SGreg Roach     *
1740115bc16SGreg Roach     * @param string $gedcom_file
175061b43d7SGreg Roach     *
176061b43d7SGreg Roach     * @return Tree
1770115bc16SGreg Roach     */
178061b43d7SGreg Roach    protected function importTree(string $gedcom_file): Tree
1790115bc16SGreg Roach    {
180061b43d7SGreg Roach        $tree = Tree::create(basename($gedcom_file), basename($gedcom_file));
181*6ccdf4f0SGreg Roach
182*6ccdf4f0SGreg Roach        $stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file);
183*6ccdf4f0SGreg Roach        $tree->importGedcomFile($stream, $gedcom_file);
1840115bc16SGreg Roach
1851ad2dde6SGreg Roach        View::share('tree', $tree);
186126654d7SGreg Roach        $gedcom_file_controller = new GedcomFileController();
187126654d7SGreg Roach
188126654d7SGreg Roach        do {
1891ad2dde6SGreg Roach            $gedcom_file_controller->import(new TimeoutService(microtime(true)), $tree);
190126654d7SGreg Roach
191126654d7SGreg Roach            $imported = $tree->getPreference('imported');
192126654d7SGreg Roach        } while (!$imported);
193061b43d7SGreg Roach
194061b43d7SGreg Roach        return $tree;
1950115bc16SGreg Roach    }
196*6ccdf4f0SGreg Roach
197*6ccdf4f0SGreg Roach    /**
198*6ccdf4f0SGreg Roach     * Create a request and bind it into the container.
199*6ccdf4f0SGreg Roach     *
200*6ccdf4f0SGreg Roach     * @param string                  $method
201*6ccdf4f0SGreg Roach     * @param string[]                $query
202*6ccdf4f0SGreg Roach     * @param string[]                $params
203*6ccdf4f0SGreg Roach     * @param UploadedFileInterface[] $files
204*6ccdf4f0SGreg Roach     *
205*6ccdf4f0SGreg Roach     * @return ServerRequestInterface
206*6ccdf4f0SGreg Roach     */
207*6ccdf4f0SGreg Roach    protected static function createRequest(string $method = 'GET', array $query = [], array $params = [], array $files = []): ServerRequestInterface
208*6ccdf4f0SGreg Roach    {
209*6ccdf4f0SGreg Roach        /** @var ServerRequestFactoryInterface */
210*6ccdf4f0SGreg Roach        $server_request_factory = app(ServerRequestFactoryInterface::class);
211*6ccdf4f0SGreg Roach
212*6ccdf4f0SGreg Roach        $uri = 'http://localhost/index.php?' . http_build_query($query);
213*6ccdf4f0SGreg Roach
214*6ccdf4f0SGreg Roach        /** @var ServerRequestInterface $request */
215*6ccdf4f0SGreg Roach        $request =  $server_request_factory
216*6ccdf4f0SGreg Roach            ->createServerRequest($method, $uri)
217*6ccdf4f0SGreg Roach            ->withQueryParams($query)
218*6ccdf4f0SGreg Roach            ->withParsedBody($params)
219*6ccdf4f0SGreg Roach            ->withUploadedFiles($files);
220*6ccdf4f0SGreg Roach
221*6ccdf4f0SGreg Roach        app()->instance(ServerRequestInterface::class, $request);
222*6ccdf4f0SGreg Roach
223*6ccdf4f0SGreg Roach        return $request;
224*6ccdf4f0SGreg Roach    }
225*6ccdf4f0SGreg Roach
226*6ccdf4f0SGreg Roach    /**
227*6ccdf4f0SGreg Roach     * Create an uploaded file for a request.
228*6ccdf4f0SGreg Roach     *
229*6ccdf4f0SGreg Roach     * @param string $filename
230*6ccdf4f0SGreg Roach     * @param string $mime_type
231*6ccdf4f0SGreg Roach     *
232*6ccdf4f0SGreg Roach     * @return UploadedFileInterface
233*6ccdf4f0SGreg Roach     */
234*6ccdf4f0SGreg Roach    protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface
235*6ccdf4f0SGreg Roach    {
236*6ccdf4f0SGreg Roach        /** @var StreamFactoryInterface */
237*6ccdf4f0SGreg Roach        $stream_factory = app(StreamFactoryInterface::class);
238*6ccdf4f0SGreg Roach
239*6ccdf4f0SGreg Roach        /** @var UploadedFileFactoryInterface */
240*6ccdf4f0SGreg Roach        $uploaded_file_factory = app(UploadedFileFactoryInterface::class);
241*6ccdf4f0SGreg Roach
242*6ccdf4f0SGreg Roach        $stream = $stream_factory->createStreamFromFile($filename);
243*6ccdf4f0SGreg Roach
244*6ccdf4f0SGreg Roach        $size = filesize($filename);
245*6ccdf4f0SGreg Roach
246*6ccdf4f0SGreg Roach        $status = UPLOAD_ERR_OK;
247*6ccdf4f0SGreg Roach
248*6ccdf4f0SGreg Roach        $client_name = basename($filename);
249*6ccdf4f0SGreg Roach
250*6ccdf4f0SGreg Roach        return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type);
251*6ccdf4f0SGreg Roach    }
25284e2cf4eSGreg Roach}
253