diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 71c3188..ae01319 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -69,7 +69,10 @@ 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 @@ -77,7 +80,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: "" if: ${{ matrix.php-version == '8.1' }} - name: Upload coverage report to Coveralls @@ -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 @@ -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 diff --git a/phpunit-bootstrap.php b/phpunit-bootstrap.php index 3eca6ee..33beddf 100644 --- a/phpunit-bootstrap.php +++ b/phpunit-bootstrap.php @@ -1,101 +1,113 @@ 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); })(); diff --git a/tests/FunctionalTestCase.php b/tests/FunctionalTestCase.php index 54c271a..1b47997 100644 --- a/tests/FunctionalTestCase.php +++ b/tests/FunctionalTestCase.php @@ -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; @@ -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'); @@ -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']); diff --git a/tests/TestUtil.php b/tests/TestUtil.php deleted file mode 100644 index 9f406d9..0000000 --- a/tests/TestUtil.php +++ /dev/null @@ -1,44 +0,0 @@ -