‪TYPO3CMS  ‪main
InstallPostgresqlCoreEnvironment.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 ‪InstallPostgresqlCoreEnvironment extends Extension
34 {
38  protected array ‪$config = [
39  'typo3InstallPostgresqlDatabaseHost' => '127.0.0.1',
40  'typo3InstallPostgresqlDatabasePort' => 5432,
41  'typo3InstallPostgresqlDatabasePassword' => '',
42  'typo3InstallPostgresqlDatabaseUsername' => '',
43  'typo3InstallPostgresqlDatabaseName' => 'core_install',
44  ];
45 
49  public function ‪_initialize(): void
50  {
51  $env = getenv('typo3InstallPostgresqlDatabaseHost');
52  $this->config['typo3InstallPostgresqlDatabaseHost'] = is_string($env)
53  ? trim($env)
54  : trim($this->config['typo3InstallPostgresqlDatabaseHost']);
55 
56  $env = getenv('typo3InstallPostgresqlDatabasePort');
57  $this->config['typo3InstallPostgresqlDatabasePort'] = is_string($env)
58  ? (int)$env
59  : (int)$this->config['typo3InstallPostgresqlDatabasePort'];
60 
61  $env = getenv('typo3InstallPostgresqlDatabasePassword');
62  $this->config['typo3InstallPostgresqlDatabasePassword'] = is_string($env)
63  ? trim($env)
64  : trim($this->config['typo3InstallPostgresqlDatabasePassword']);
65 
66  $env = getenv('typo3InstallPostgresqlDatabaseUsername');
67  $this->config['typo3InstallPostgresqlDatabaseUsername'] = is_string($env)
68  ? trim($env)
69  : $this->config['typo3InstallPostgresqlDatabaseUsername'];
70 
71  $env = getenv('typo3InstallPostgresqlDatabaseName');
72  $this->config['typo3InstallPostgresqlDatabaseName'] = (is_string($env) && !empty($env))
73  ? mb_strtolower(trim($env))
74  : mb_strtolower(trim($this->config['typo3InstallPostgresqlDatabaseName']));
75 
76  if (empty($this->config['typo3InstallPostgresqlDatabaseName'])) {
77  throw new \RuntimeException('No database name given', 1530827195);
78  }
79  }
80 
84  public static ‪$events = [
85  Events::TEST_BEFORE => 'bootstrapTypo3Environment',
86  ];
87 
94  public function ‪bootstrapTypo3Environment(TestEvent $event)
95  {
96  $testbase = new Testbase();
97  $testbase->defineOriginalRootPath();
98 
99  $instancePath = ‪ORIGINAL_ROOT . 'typo3temp/var/tests/acceptance';
100  $testbase->removeOldInstanceIfExists($instancePath);
101  putenv('TYPO3_PATH_ROOT=' . $instancePath);
102  putenv('TYPO3_PATH_APP=' . $instancePath);
103  $testbase->setTypo3TestingContext();
104 
105  // Drop db from a previous run if exists
106  $connectionParameters = [
107  'driver' => 'pdo_pgsql',
108  'host' => $this->config['typo3InstallPostgresqlDatabaseHost'],
109  'port' => $this->config['typo3InstallPostgresqlDatabasePort'],
110  'password' => $this->config['typo3InstallPostgresqlDatabasePassword'],
111  'user' => $this->config['typo3InstallPostgresqlDatabaseUsername'],
112  ];
113  $this->output->debug('Connecting to PgSQL: ' . json_encode($connectionParameters));
114  $schemaManager = DriverManager::getConnection($connectionParameters)->createSchemaManager();
115  $databaseName = $this->config['typo3InstallPostgresqlDatabaseName'];
116  $this->output->debug("Database: $databaseName");
117  if (in_array($databaseName, $schemaManager->listDatabases(), true)) {
118  $this->output->debug("Dropping database $databaseName");
119  $schemaManager->dropDatabase($databaseName);
120  }
121  $schemaManager->createDatabase($databaseName);
122 
123  $testbase->createDirectory($instancePath);
124  $testbase->setUpInstanceCoreLinks($instancePath);
125  touch($instancePath . '/FIRST_INSTALL');
126 
127  // Have config available in test
128  $event->getTest()->getMetadata()->setCurrent($this->config);
129  }
130 }
‪ORIGINAL_ROOT
‪const ORIGINAL_ROOT
Definition: phpstan-constants.php:4
‪TYPO3\CMS\Core\Tests\Acceptance\Support\Extension\InstallPostgresqlCoreEnvironment\_initialize
‪_initialize()
Definition: InstallPostgresqlCoreEnvironment.php:49
‪TYPO3\CMS\Core\Tests\Acceptance\Support\Extension\InstallPostgresqlCoreEnvironment\$config
‪array $config
Definition: InstallPostgresqlCoreEnvironment.php:38
‪TYPO3\CMS\Core\Tests\Acceptance\Support\Extension
Definition: ApplicationComposerEnvironment.php:18
‪TYPO3\CMS\Core\Tests\Acceptance\Support\Extension\InstallPostgresqlCoreEnvironment\$events
‪static $events
Definition: InstallPostgresqlCoreEnvironment.php:84
‪TYPO3\CMS\Core\Tests\Acceptance\Support\Extension\InstallPostgresqlCoreEnvironment\bootstrapTypo3Environment
‪bootstrapTypo3Environment(TestEvent $event)
Definition: InstallPostgresqlCoreEnvironment.php:94
‪TYPO3\CMS\Core\Tests\Acceptance\Support\Extension\InstallPostgresqlCoreEnvironment
Definition: InstallPostgresqlCoreEnvironment.php:34