xref: /webtrees/tests/TestCase.php (revision 8c3e1068086a769c57e858396c1b6af7d1e52f6c)
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
20e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
21126654d7SGreg Roachuse Fisharebest\Webtrees\Http\Controllers\GedcomFileController;
228136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
238136679eSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme;
240115bc16SGreg Roachuse Fisharebest\Webtrees\Schema\SeedDatabase;
25*8c3e1068SGreg Roachuse Fisharebest\Webtrees\Services\MigrationService;
26126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService;
27e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
288b67c11aSGreg Roachuse Illuminate\Cache\ArrayStore;
298b67c11aSGreg Roachuse Illuminate\Cache\Repository;
300115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
317def76c7SGreg Roachuse League\Flysystem\Filesystem;
327def76c7SGreg Roachuse League\Flysystem\Memory\MemoryAdapter;
33d4c04956SGreg Roachuse Symfony\Component\HttpFoundation\Request;
347def76c7SGreg Roachuse function basename;
350115bc16SGreg Roach
3684e2cf4eSGreg Roach/**
3784e2cf4eSGreg Roach * Base class for unit tests
3884e2cf4eSGreg Roach */
3984e2cf4eSGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase
4084e2cf4eSGreg Roach{
41061b43d7SGreg Roach    protected static $uses_database = false;
42061b43d7SGreg Roach
43061b43d7SGreg Roach    /**
44061b43d7SGreg Roach     * Things to run once, before all the tests.
45061b43d7SGreg Roach     */
46061b43d7SGreg Roach    public static function setUpBeforeClass()
47061b43d7SGreg Roach    {
48061b43d7SGreg Roach        parent::setUpBeforeClass();
49061b43d7SGreg Roach
50061b43d7SGreg Roach        if (static::$uses_database) {
5132f20c14SGreg Roach            defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/');
5232f20c14SGreg Roach
53061b43d7SGreg Roach            static::createTestDatabase();
54061b43d7SGreg Roach        }
55061b43d7SGreg Roach    }
56061b43d7SGreg Roach
57061b43d7SGreg Roach    /**
58061b43d7SGreg Roach     * Things to run once, AFTER all the tests.
59061b43d7SGreg Roach     */
60061b43d7SGreg Roach    public static function tearDownAfterClass()
61061b43d7SGreg Roach    {
62061b43d7SGreg Roach        if (static::$uses_database) {
63061b43d7SGreg Roach            $pdo = DB::connection()->getPdo();
6432f20c14SGreg Roach            unset($pdo);
65061b43d7SGreg Roach        }
66061b43d7SGreg Roach
67061b43d7SGreg Roach        parent::tearDownAfterClass();
68061b43d7SGreg Roach    }
69061b43d7SGreg Roach
70061b43d7SGreg Roach    /**
71061b43d7SGreg Roach     * Things to run before every test.
72061b43d7SGreg Roach     */
730115bc16SGreg Roach    protected function setUp()
740115bc16SGreg Roach    {
750115bc16SGreg Roach        parent::setUp();
760115bc16SGreg Roach
778b67c11aSGreg Roach        // Use an array cache for database calls, etc.
788b67c11aSGreg Roach        app()->instance('cache.array', new Repository(new ArrayStore()));
798b67c11aSGreg Roach
80d4c04956SGreg Roach        app()->bind(Tree::class, function () {
81d4c04956SGreg Roach            return null;
82d4c04956SGreg Roach        });
83d4c04956SGreg Roach
84e5a6b4d4SGreg Roach        app()->instance(UserService::class, new UserService());
85e5a6b4d4SGreg Roach        app()->instance(UserInterface::class, new GuestUser());
86d4c04956SGreg Roach
87d4c04956SGreg Roach        app()->instance(Request::class, Request::createFromGlobals());
887def76c7SGreg Roach        app()->instance(Filesystem::class, new Filesystem(new MemoryAdapter()));
89d4c04956SGreg Roach
908136679eSGreg Roach        app()->bind(ModuleThemeInterface::class, WebtreesTheme::class);
918136679eSGreg Roach
92061b43d7SGreg Roach        defined('WT_ROOT') || define('WT_ROOT', dirname(__DIR__) . '/');
9332f20c14SGreg Roach        defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/');
94061b43d7SGreg Roach        defined('WT_DATA_DIR') || define('WT_DATA_DIR', WT_ROOT . 'data/');
95061b43d7SGreg Roach        defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US'));
96061b43d7SGreg Roach
97061b43d7SGreg Roach        if (static::$uses_database) {
98061b43d7SGreg Roach            DB::connection()->beginTransaction();
99061b43d7SGreg Roach        }
100061b43d7SGreg Roach    }
101061b43d7SGreg Roach
102061b43d7SGreg Roach    /**
103061b43d7SGreg Roach     * Things to run after every test
104061b43d7SGreg Roach     */
105a49feabaSGreg Roach    protected function tearDown()
106a49feabaSGreg Roach    {
10732f20c14SGreg Roach        if (static::$uses_database) {
108061b43d7SGreg Roach            DB::connection()->rollBack();
109061b43d7SGreg Roach        }
11032f20c14SGreg Roach
1118b67c11aSGreg Roach        app('cache.array')->flush();
1128b67c11aSGreg Roach
11332f20c14SGreg Roach        Site::$preferences                  = [];
11432f20c14SGreg Roach        Tree::$trees                        = [];
115bec87e94SGreg Roach        GedcomRecord::$gedcom_record_cache  = null;
116bec87e94SGreg Roach        GedcomRecord::$pending_record_cache = null;
11732f20c14SGreg Roach
11832f20c14SGreg Roach        Auth::logout();
1190115bc16SGreg Roach    }
1200115bc16SGreg Roach
1210115bc16SGreg Roach    /**
1220115bc16SGreg Roach     * Create an SQLite in-memory database for testing
1230115bc16SGreg Roach     */
124061b43d7SGreg Roach    protected static function createTestDatabase(): void
1250115bc16SGreg Roach    {
1260115bc16SGreg Roach        $capsule = new DB();
1270115bc16SGreg Roach        $capsule->addConnection([
1280115bc16SGreg Roach            'driver'   => 'sqlite',
1290115bc16SGreg Roach            'database' => ':memory:',
1300115bc16SGreg Roach        ]);
1310115bc16SGreg Roach        $capsule->setAsGlobal();
132c88a5019SGreg Roach        Database::registerMacros();
1330115bc16SGreg Roach
1340115bc16SGreg Roach        // Create tables
135*8c3e1068SGreg Roach        $migration_service = new MigrationService;
136*8c3e1068SGreg Roach        $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
1370115bc16SGreg Roach
1380115bc16SGreg Roach        // Create config data
139*8c3e1068SGreg Roach        $migration_service->seedDatabase();
1400115bc16SGreg Roach    }
1410115bc16SGreg Roach
1420115bc16SGreg Roach    /**
1430115bc16SGreg Roach     * Import a GEDCOM file into the test database.
1440115bc16SGreg Roach     *
1450115bc16SGreg Roach     * @param string $gedcom_file
146061b43d7SGreg Roach     *
147061b43d7SGreg Roach     * @return Tree
1480115bc16SGreg Roach     */
149061b43d7SGreg Roach    protected function importTree(string $gedcom_file): Tree
1500115bc16SGreg Roach    {
151061b43d7SGreg Roach        $tree = Tree::create(basename($gedcom_file), basename($gedcom_file));
15269b58ac8SGreg Roach        $tree->importGedcomFile(__DIR__ . '/data/' . $gedcom_file, $gedcom_file);
1530115bc16SGreg Roach
1541ad2dde6SGreg Roach        View::share('tree', $tree);
155126654d7SGreg Roach        $gedcom_file_controller = new GedcomFileController();
156126654d7SGreg Roach
157126654d7SGreg Roach        do {
1581ad2dde6SGreg Roach            $gedcom_file_controller->import(new TimeoutService(microtime(true)), $tree);
159126654d7SGreg Roach
160126654d7SGreg Roach            $imported = $tree->getPreference('imported');
161126654d7SGreg Roach        } while (!$imported);
162061b43d7SGreg Roach
163061b43d7SGreg Roach        return $tree;
1640115bc16SGreg Roach    }
16584e2cf4eSGreg Roach}
166