Skip to content

Commit

Permalink
Merge pull request #6 from brick/orm3
Browse files Browse the repository at this point in the history
Upgrade to doctrine/orm v3 & doctrine/dbal v4
  • Loading branch information
BenMorel authored Dec 19, 2024
2 parents 1d22337 + 97a5cde commit 33b2867
Show file tree
Hide file tree
Showing 36 changed files with 223 additions and 349 deletions.
24 changes: 19 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: CI

on:
push:
branches:
- master
pull_request:

env:
Expand Down Expand Up @@ -74,15 +76,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 != env.COVERAGE_PHP_VERSION }}

- 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 == env.COVERAGE_PHP_VERSION }}

- name: Upload coverage report to Coveralls
Expand All @@ -102,7 +110,7 @@ jobs:

services:
mariadb:
image: "mariadb:10.1"
image: "mariadb:10.11"
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
options: >-
Expand All @@ -129,7 +137,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 @@ -171,7 +182,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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [0.4.0](https://github.com/brick/geo-doctrine/releases/tag/0.4.0) - 2024-12-19

💥 **Breaking changes**

- compatibility with `doctrine/orm` `3.x` (`2.x` is no longer supported)
- compatibility with `doctrine/dbal` `4.x` (`2.x` and `3.x` are no longer supported)

🐛 **Bug fixes**

- `GeometryType::convertToDatabaseValue()` now properly throws `ConversionException` as it should

## [0.3.1](https://github.com/brick/geo-doctrine/releases/tag/0.3.1) - 2024-06-07

**Upgrades**
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@
"require": {
"php": "^8.1",
"brick/geo": "~0.10.0 || ~0.11.0",
"doctrine/orm": "^2.8"
"doctrine/dbal": "^4.0",
"doctrine/orm": "^3.0"
},
"require-dev": {
"doctrine/annotations": "^1.0",
"ext-pdo": "*",
"doctrine/cache": "^1.11",
"doctrine/data-fixtures": "^1.0",
"doctrine/data-fixtures": "^1.7",
"guzzlehttp/guzzle": "^7.0",
"phpunit/phpunit": "^10.5",
"php-coveralls/php-coveralls": "^2.7",
"vimeo/psalm": "5.21.1"
"vimeo/psalm": "5.21.1",
"symfony/cache": "^5.0 || ^6.0 || ^7.0"
},
"autoload": {
"psr-4": {
Expand Down
158 changes: 85 additions & 73 deletions phpunit-bootstrap.php
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);
})();
10 changes: 5 additions & 5 deletions src/Functions/AbstractFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Brick\Geo\Doctrine\Functions;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\TokenType;

/**
* Base class for Doctrine functions.
Expand Down Expand Up @@ -44,20 +44,20 @@ public function parse(Parser $parser) : void
{
$this->args = [];

$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$parser->match(TokenType::T_IDENTIFIER);
$parser->match(TokenType::T_OPEN_PARENTHESIS);

$parameterCount = $this->getParameterCount();

for ($i = 0; $i < $parameterCount; $i++) {
if ($i !== 0) {
$parser->match(Lexer::T_COMMA);
$parser->match(TokenType::T_COMMA);
}

/** @psalm-suppress InvalidPropertyAssignmentValue */
$this->args[] = $parser->ArithmeticPrimary();
}

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
}
}
2 changes: 1 addition & 1 deletion src/Types/GeometryCollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
class GeometryCollectionType extends GeometryType
{
public function getName() : string
protected function getGeometryName() : string
{
return 'GeometryCollection';
}
Expand Down
Loading

0 comments on commit 33b2867

Please sign in to comment.