-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use env vars for connection params in phpunit-bootstrap.php
- Loading branch information
Showing
4 changed files
with
102 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,113 @@ | ||
<?php | ||
|
||
use Brick\Geo\Doctrine\Types; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\DriverManager; | ||
use Doctrine\DBAL\Exception\DatabaseDoesNotExist; | ||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
use Doctrine\DBAL\Types\Type; | ||
|
||
(function() { | ||
/** @var \Composer\Autoload\ClassLoader $classLoader */ | ||
$classLoader = require 'vendor/autoload.php'; | ||
const TEST_DATABASE = 'geo_tests'; | ||
|
||
// Add namespace for doctrine base tests | ||
$classLoader->addPsr4('Doctrine\\Tests\\', [ | ||
__DIR__ . '/vendor/doctrine/orm/tests/Doctrine/Tests', | ||
__DIR__ . '/vendor/doctrine/dbal/tests/Doctrine/Tests' | ||
]); | ||
$classLoader->loadClass('Doctrine\Tests\DbalFunctionalTestCase'); | ||
$classLoader->loadClass('Doctrine\Tests\DBAL\Mocks\MockPlatform'); | ||
function createDoctrineConnection(bool $selectDatabase): Connection | ||
{ | ||
$env = getenv(); | ||
|
||
Type::addType('Geometry', Types\GeometryType::class); | ||
Type::addType('LineString', Types\LineStringType::class); | ||
Type::addType('MultiLineString', Types\MultiLineStringType::class); | ||
Type::addType('MultiPoint', Types\MultiPointType::class); | ||
Type::addType('MultiPolygon', Types\MultiPolygonType::class); | ||
Type::addType('Point', Types\PointType::class); | ||
Type::addType('Polygon', Types\PolygonType::class); | ||
$requiredEnv = [ | ||
'DB_DRIVER', | ||
'DB_HOST', | ||
'DB_USER', | ||
'DB_PASSWORD', | ||
]; | ||
|
||
$driver = getenv('DRIVER'); | ||
$driver = $env['DB_DRIVER'] ?? null; | ||
$host = $env['DB_HOST'] ?? null; | ||
$port = $env['DB_PORT'] ?? null; | ||
$user = $env['DB_USER'] ?? null; | ||
$password = $env['DB_PASSWORD'] ?? null; | ||
|
||
if ($driver === false) { | ||
echo 'Please set the database driver to use:' . PHP_EOL; | ||
echo 'DRIVER={driver} vendor/bin/phpunit' . PHP_EOL; | ||
echo 'Available drivers: PDO_MYSQL, PDO_PGSQL' . PHP_EOL; | ||
exit(1); | ||
} else { | ||
switch ($driver) { | ||
case 'PDO_MYSQL': | ||
echo 'Using PDO_MYSQL driver' . PHP_EOL; | ||
if ($driver === null || $host === null || $user === null || $password === null) { | ||
$missingEnv = array_diff($requiredEnv, array_keys($env)); | ||
|
||
$pdo = new PDO('mysql:host=127.0.0.1;port=3306', 'root', ''); | ||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
echo "Missing environment variables: ", PHP_EOL; | ||
foreach ($missingEnv as $key) { | ||
echo " - $key", PHP_EOL; | ||
} | ||
echo PHP_EOL; | ||
|
||
$pdo->exec('DROP DATABASE IF EXISTS geo_tests'); | ||
$pdo->exec('CREATE DATABASE geo_tests'); | ||
echo "Example:", PHP_EOL; | ||
echo 'DB_DRIVER=pdo_mysql DB_HOST=localhost DB_PORT=3306 DB_USER=root DB_PASSWORD=password vendor/bin/phpunit' , PHP_EOL; | ||
echo PHP_EOL; | ||
|
||
$statement = $pdo->query('SELECT VERSION()'); | ||
$version = $statement->fetchColumn(); | ||
echo 'Available drivers:', PHP_EOL; | ||
echo 'https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#driver', PHP_EOL; | ||
|
||
echo 'MySQL version: ' . $version . PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$GLOBALS['db_type'] = 'pdo_mysql'; | ||
$GLOBALS['db_host'] = '127.0.0.1'; | ||
$GLOBALS['db_port'] = 3306; | ||
$GLOBALS['db_username'] = 'root'; | ||
$GLOBALS['db_password'] = ''; | ||
$GLOBALS['db_name'] = 'geo_tests'; | ||
$params = [ | ||
'driver' => $driver, | ||
'host' => $host, | ||
'user' => $user, | ||
'password' => $password, | ||
]; | ||
|
||
// doctrine/dbal >= 2.13.0 | ||
$GLOBALS['db_driver'] = 'pdo_mysql'; | ||
$GLOBALS['db_user'] = 'root'; | ||
$GLOBALS['db_dbname'] = 'geo_tests'; | ||
if ($port !== null) { | ||
$params['port'] = (int) $port; | ||
} | ||
|
||
break; | ||
if ($selectDatabase) { | ||
$params['dbname'] = TEST_DATABASE; | ||
} | ||
|
||
case 'PDO_PGSQL': | ||
echo 'Using PDO_PGSQL driver' . PHP_EOL; | ||
$connection = DriverManager::getConnection($params); | ||
|
||
$pdo = new PDO('pgsql:host=localhost', 'postgres', 'postgres'); | ||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
if ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) { | ||
$connection->executeStatement('CREATE EXTENSION IF NOT EXISTS postgis'); | ||
} | ||
|
||
$pdo->exec('DROP DATABASE IF EXISTS geo_tests'); | ||
$pdo->exec('CREATE DATABASE geo_tests'); | ||
return $connection; | ||
} | ||
|
||
$statement = $pdo->query('SELECT version()'); | ||
$version = $statement->fetchColumn(); | ||
(function() { | ||
$connection = createDoctrineConnection(selectDatabase: false); | ||
|
||
echo 'PostgreSQL version: ' . $version . PHP_EOL; | ||
echo "Database version: ", $connection->getServerVersion(), PHP_EOL; | ||
echo "Database platform: ", get_class($connection->getDatabasePlatform()), PHP_EOL; | ||
|
||
$statement = $pdo->query('SELECT PostGIS_Version()'); | ||
$version = $statement->fetchColumn(); | ||
if ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) { | ||
$version = $connection->executeQuery('SELECT PostGIS_Version()')->fetchOne(); | ||
echo 'PostGIS version: ', $version, PHP_EOL; | ||
} | ||
|
||
echo 'PostGIS version: ' . $version . PHP_EOL; | ||
$schemaManager = $connection->createSchemaManager(); | ||
|
||
$GLOBALS['db_type'] = 'pdo_pgsql'; | ||
$GLOBALS['db_host'] = 'localhost'; | ||
$GLOBALS['db_port'] = 5432; | ||
$GLOBALS['db_username'] = 'postgres'; | ||
$GLOBALS['db_password'] = 'postgres'; | ||
$GLOBALS['db_name'] = 'geo_tests'; | ||
try { | ||
$schemaManager->dropDatabase('geo_tests'); | ||
} catch (DatabaseDoesNotExist) { | ||
// not an error! | ||
} | ||
|
||
// doctrine/dbal >= 2.13.0 | ||
$GLOBALS['db_driver'] = 'pdo_pgsql'; | ||
$GLOBALS['db_user'] = 'postgres'; | ||
$GLOBALS['db_dbname'] = 'geo_tests'; | ||
$schemaManager->createDatabase('geo_tests'); | ||
|
||
break; | ||
/** @var \Composer\Autoload\ClassLoader $classLoader */ | ||
$classLoader = require 'vendor/autoload.php'; | ||
|
||
default: | ||
echo 'Unknown driver: ' . $driver . PHP_EOL; | ||
exit(1); | ||
} | ||
} | ||
// Add namespace for doctrine base tests | ||
$classLoader->addPsr4('Doctrine\\Tests\\', [ | ||
__DIR__ . '/vendor/doctrine/orm/tests/Doctrine/Tests', | ||
__DIR__ . '/vendor/doctrine/dbal/tests/Doctrine/Tests' | ||
]); | ||
|
||
$classLoader->loadClass('Doctrine\Tests\DbalFunctionalTestCase'); | ||
$classLoader->loadClass('Doctrine\Tests\DBAL\Mocks\MockPlatform'); | ||
|
||
// Register Doctrine types | ||
Type::addType('Geometry', Types\GeometryType::class); | ||
Type::addType('LineString', Types\LineStringType::class); | ||
Type::addType('MultiLineString', Types\MultiLineStringType::class); | ||
Type::addType('MultiPoint', Types\MultiPointType::class); | ||
Type::addType('MultiPolygon', Types\MultiPolygonType::class); | ||
Type::addType('Point', Types\PointType::class); | ||
Type::addType('Polygon', Types\PolygonType::class); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.