‪TYPO3CMS  ‪main
InstallMysqlCoreEnvironment.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Codeception\Event\TestEvent;
21 use Codeception\Events;
22 use Codeception\Extension;
23 use Doctrine\DBAL\DriverManager;
24 use TYPO3\TestingFramework\Core\Testbase;
25 
33 final class ‪InstallMysqlCoreEnvironment extends Extension
34 {
38  protected array ‪$config = [
39  'typo3InstallMysqlDatabaseHost' => '127.0.0.1',
40  'typo3InstallMysqlDatabasePassword' => '',
41  'typo3InstallMysqlDatabaseUsername' => 'root',
42  'typo3InstallMysqlDatabaseName' => 'core_install',
43  ];
44 
48  public function ‪_initialize(): void
49  {
50  $env = getenv('typo3InstallMysqlDatabaseHost');
51  $this->config['typo3InstallMysqlDatabaseHost'] = is_string($env)
52  ? trim($env)
53  : trim($this->config['typo3InstallMysqlDatabaseHost']);
54 
55  $env = getenv('typo3InstallMysqlDatabasePassword');
56  $this->config['typo3InstallMysqlDatabasePassword'] = is_string($env)
57  ? trim($env)
58  : trim($this->config['typo3InstallMysqlDatabasePassword']);
59 
60  $env = getenv('typo3InstallMysqlDatabaseUsername');
61  $this->config['typo3InstallMysqlDatabaseUsername'] = is_string($env)
62  ? trim($env)
63  : $this->config['typo3InstallMysqlDatabaseUsername'];
64 
65  $env = getenv('typo3InstallMysqlDatabaseName');
66  $this->config['typo3InstallMysqlDatabaseName'] = (is_string($env) && !empty($env))
67  ? mb_strtolower(trim($env))
68  : mb_strtolower(trim($this->config['typo3InstallMysqlDatabaseName']));
69 
70  if (empty($this->config['typo3InstallMysqlDatabaseName'])) {
71  throw new \RuntimeException('No database name given', 1530827194);
72  }
73  }
74 
78  public static ‪$events = [
79  Events::TEST_BEFORE => 'bootstrapTypo3Environment',
80  ];
81 
88  public function ‪bootstrapTypo3Environment(TestEvent $event)
89  {
90  $testbase = new Testbase();
91  $testbase->defineOriginalRootPath();
92 
93  $instancePath = ‪ORIGINAL_ROOT . 'typo3temp/var/tests/acceptance';
94  $testbase->removeOldInstanceIfExists($instancePath);
95  putenv('TYPO3_PATH_ROOT=' . $instancePath);
96  putenv('TYPO3_PATH_APP=' . $instancePath);
97  $testbase->setTypo3TestingContext();
98 
99  // Drop db from a previous run if exists
100  $connectionParameters = [
101  'driver' => 'mysqli',
102  'host' => $this->config['typo3InstallMysqlDatabaseHost'],
103  'port' => 3306,
104  'password' => $this->config['typo3InstallMysqlDatabasePassword'],
105  'user' => $this->config['typo3InstallMysqlDatabaseUsername'],
106  ];
107  $this->output->debug('Connecting to MySQL: ' . json_encode($connectionParameters));
108  $databaseName = $this->config['typo3InstallMysqlDatabaseName'];
109  $schemaManager = DriverManager::getConnection($connectionParameters)->createSchemaManager();
110  $this->output->debug("Database: $databaseName");
111  if (in_array($databaseName, $schemaManager->listDatabases(), true)) {
112  $this->output->debug("Dropping database $databaseName");
113  $schemaManager->dropDatabase($databaseName);
114  }
115 
116  $testbase->createDirectory($instancePath);
117  $testbase->setUpInstanceCoreLinks($instancePath);
118  touch($instancePath . '/FIRST_INSTALL');
119 
120  // Have config available in test
121  $event->getTest()->getMetadata()->setCurrent($this->config);
122  }
123 }
‪ORIGINAL_ROOT
‪const ORIGINAL_ROOT
Definition: phpstan-constants.php:4
‪TYPO3\CMS\Core\Tests\Acceptance\Support\Extension\InstallMysqlCoreEnvironment\_initialize
‪_initialize()
Definition: InstallMysqlCoreEnvironment.php:48
‪TYPO3\CMS\Core\Tests\Acceptance\Support\Extension
Definition: ApplicationComposerEnvironment.php:18
‪TYPO3\CMS\Core\Tests\Acceptance\Support\Extension\InstallMysqlCoreEnvironment\$config
‪array $config
Definition: InstallMysqlCoreEnvironment.php:38
‪TYPO3\CMS\Core\Tests\Acceptance\Support\Extension\InstallMysqlCoreEnvironment
Definition: InstallMysqlCoreEnvironment.php:34
‪TYPO3\CMS\Core\Tests\Acceptance\Support\Extension\InstallMysqlCoreEnvironment\bootstrapTypo3Environment
‪bootstrapTypo3Environment(TestEvent $event)
Definition: InstallMysqlCoreEnvironment.php:88
‪TYPO3\CMS\Core\Tests\Acceptance\Support\Extension\InstallMysqlCoreEnvironment\$events
‪static $events
Definition: InstallMysqlCoreEnvironment.php:78