Skip to content

Commit

Permalink
Use env vars for connection params in phpunit-bootstrap.php
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Feb 7, 2024
1 parent 45599f1 commit 7a3b6fb
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 83 deletions.
20 changes: 16 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,21 @@ jobs:
- name: Run PHPUnit
run: vendor/bin/phpunit
env:
DRIVER: PDO_MYSQL
DB_DRIVER: pdo_mysql
DB_HOST: 127.0.0.1
DB_USER: root
DB_PASSWORD: ""
if: ${{ matrix.php-version != '8.1' }}

- name: Run PHPUnit with coverage
run: |
mkdir -p mkdir -p build/logs
vendor/bin/phpunit --coverage-clover build/logs/clover.xml
env:
DRIVER: PDO_MYSQL
DB_DRIVER: pdo_mysql
DB_HOST: 127.0.0.1
DB_USER: root
DB_PASSWORD: ""
if: ${{ matrix.php-version == '8.1' }}

- name: Upload coverage report to Coveralls
Expand Down Expand Up @@ -124,7 +130,10 @@ jobs:
mkdir -p mkdir -p build/logs
vendor/bin/phpunit --coverage-clover build/logs/clover.xml
env:
DRIVER: PDO_MYSQL
DB_DRIVER: pdo_mysql
DB_HOST: 127.0.0.1
DB_USER: root
DB_PASSWORD: ""

- name: Upload coverage report to Coveralls
run: vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v
Expand Down Expand Up @@ -166,7 +175,10 @@ jobs:
mkdir -p mkdir -p build/logs
vendor/bin/phpunit --coverage-clover build/logs/clover.xml
env:
DRIVER: PDO_PGSQL
DB_DRIVER: pdo_pgsql
DB_HOST: localhost
DB_USER: postgres
DB_PASSWORD: postgres

- name: Upload coverage report to Coveralls
run: vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v
Expand Down
155 changes: 82 additions & 73 deletions phpunit-bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,101 +1,110 @@
<?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;
return DriverManager::getConnection($params);
}

$pdo = new PDO('pgsql:host=localhost', 'postgres', 'postgres');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
(function() {
$connection = createDoctrineConnection(selectDatabase: false);

$pdo->exec('DROP DATABASE IF EXISTS geo_tests');
$pdo->exec('CREATE DATABASE geo_tests');
echo "Database version: ", $connection->getServerVersion(), PHP_EOL;
echo "Database platform: ", get_class($connection->getDatabasePlatform()), PHP_EOL;

$statement = $pdo->query('SELECT version()');
$version = $statement->fetchColumn();
// Load PostGIS extension
if ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
$this->connection->executeQuery('CREATE EXTENSION IF NOT EXISTS postgis;');
$version = $connection->executeQuery('SELECT PostGIS_Version()')->fetchOne();

echo 'PostgreSQL version: ' . $version . PHP_EOL;
echo 'PostGIS version: ', $version, PHP_EOL;
}

$statement = $pdo->query('SELECT PostGIS_Version()');
$version = $statement->fetchColumn();
$schemaManager = $connection->createSchemaManager();

echo 'PostGIS version: ' . $version . PHP_EOL;
try {
$schemaManager->dropDatabase('geo_tests');
} catch (DatabaseDoesNotExist) {
// not an error!
}

$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';
$schemaManager->createDatabase('geo_tests');

// doctrine/dbal >= 2.13.0
$GLOBALS['db_driver'] = 'pdo_pgsql';
$GLOBALS['db_user'] = 'postgres';
$GLOBALS['db_dbname'] = 'geo_tests';
/** @var \Composer\Autoload\ClassLoader $classLoader */
$classLoader = require 'vendor/autoload.php';

break;
// Add namespace for doctrine base tests
$classLoader->addPsr4('Doctrine\\Tests\\', [
__DIR__ . '/vendor/doctrine/orm/tests/Doctrine/Tests',
__DIR__ . '/vendor/doctrine/dbal/tests/Doctrine/Tests'
]);

default:
echo 'Unknown driver: ' . $driver . PHP_EOL;
exit(1);
}
}
$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);
})();
7 changes: 1 addition & 6 deletions tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Brick\Geo\Doctrine\Tests;

use Brick\Geo\Point;
use Brick\Geo\Doctrine\Tests\Fixtures;

use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\FixtureInterface;
Expand Down Expand Up @@ -38,7 +37,7 @@ protected function setUp(): void
{
parent::setUp();

$this->connection = TestUtil::getConnection();
$this->connection = createDoctrineConnection(selectDatabase: true);
$this->platform = $this->connection->getDatabasePlatform();

$this->platform->registerDoctrineTypeMapping('geometry', 'binary');
Expand All @@ -49,10 +48,6 @@ protected function setUp(): void
$this->platform->registerDoctrineTypeMapping('point', 'binary');
$this->platform->registerDoctrineTypeMapping('polygon', 'binary');

if ($this->platform instanceof PostgreSQLPlatform) {
$this->connection->executeQuery('CREATE EXTENSION IF NOT EXISTS postgis;');
}

$this->fixtureLoader = new Loader();

$config = ORMSetup::createAttributeMetadataConfiguration([__DIR__ . '/Fixtures']);
Expand Down

0 comments on commit 7a3b6fb

Please sign in to comment.