From 022a01dd2ffc9408b4305af68b41947e11e29747 Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Sun, 10 Oct 2021 16:10:13 +0200 Subject: [PATCH] Fix tests & Psalm issues --- .github/workflows/ci.yml | 91 ++---------------------------------- composer.json | 1 + phpunit-bootstrap.php | 90 +++++------------------------------ src/Types/GeometryType.php | 4 +- tests/FunctionalTestCase.php | 28 +++++------ tests/TestUtil.php | 44 +++++++++++++++++ 6 files changed, 74 insertions(+), 184 deletions(-) create mode 100644 tests/TestUtil.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 320f5ec..0fa7c56 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,9 +33,6 @@ jobs: php-version: - "7.4" - "8.0" - emulate-prepares: - - "ON" - - "OFF" services: mysql: @@ -64,8 +61,7 @@ jobs: - name: Run PHPUnit run: vendor/bin/phpunit env: - ENGINE: PDO_MYSQL - EMULATE_PREPARES: ${{ matrix.emulate-prepares }} + DRIVER: PDO_MYSQL if: ${{ matrix.php-version != '8.0' }} - name: Run PHPUnit with coverage @@ -73,8 +69,7 @@ jobs: mkdir -p mkdir -p build/logs vendor/bin/phpunit --coverage-clover build/logs/clover.xml env: - ENGINE: PDO_MYSQL - EMULATE_PREPARES: ${{ matrix.emulate-prepares }} + DRIVER: PDO_MYSQL if: ${{ matrix.php-version == '8.0' }} - name: Upload coverage report to Coveralls @@ -91,9 +86,6 @@ jobs: matrix: php-version: - "8.0" - emulate-prepares: - - "ON" - - "OFF" services: mariadb: @@ -124,8 +116,7 @@ jobs: mkdir -p mkdir -p build/logs vendor/bin/phpunit --coverage-clover build/logs/clover.xml env: - ENGINE: PDO_MYSQL - EMULATE_PREPARES: ${{ matrix.emulate-prepares }} + DRIVER: PDO_MYSQL - name: Upload coverage report to Coveralls run: vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v @@ -167,81 +158,7 @@ jobs: mkdir -p mkdir -p build/logs vendor/bin/phpunit --coverage-clover build/logs/clover.xml env: - ENGINE: PDO_PGSQL - - - name: Upload coverage report to Coveralls - run: vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v - env: - COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - phpunit-sqlite: - name: PHPUnit SQLite - runs-on: ubuntu-20.04 - - strategy: - matrix: - php-version: - - "8.0" - - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-version }} - extensions: sqlite3 - ini-values: sqlite3.extension_dir=/usr/lib/x86_64-linux-gnu - coverage: xdebug - - - name: Install SpatiaLite - run: sudo apt install libsqlite3-mod-spatialite - - - name: Install composer dependencies - uses: "ramsey/composer-install@v1" - - - name: Run PHPUnit with coverage - run: | - mkdir -p mkdir -p build/logs - vendor/bin/phpunit --coverage-clover build/logs/clover.xml - env: - ENGINE: SQLite3 - - - name: Upload coverage report to Coveralls - run: vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v - env: - COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - phpunit-geos: - name: PHPUnit GEOS - runs-on: ubuntu-20.04 - - strategy: - matrix: - php-version: - - "7.4" - - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-version }} - extensions: geos - coverage: xdebug - - - name: Install composer dependencies - uses: "ramsey/composer-install@v1" - - - name: Run PHPUnit with coverage - run: | - mkdir -p mkdir -p build/logs - vendor/bin/phpunit --coverage-clover build/logs/clover.xml - env: - ENGINE: GEOS + DRIVER: PDO_PGSQL - name: Upload coverage report to Coveralls run: vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v diff --git a/composer.json b/composer.json index 28b315d..d828f61 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "doctrine/orm": "^2.0" }, "require-dev": { + "doctrine/cache": "^1.11", "doctrine/data-fixtures": "^1.0", "phpunit/phpunit": "^8.0 || ^9.0", "php-coveralls/php-coveralls": "^2.0", diff --git a/phpunit-bootstrap.php b/phpunit-bootstrap.php index 53bc15d..52f76b9 100644 --- a/phpunit-bootstrap.php +++ b/phpunit-bootstrap.php @@ -1,10 +1,6 @@ setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $emulatePrepares); $pdo->exec('DROP DATABASE IF EXISTS geo_tests'); - $pdo->exec('DROP DATABASE IF EXISTS geo_tests_tmp'); $pdo->exec('CREATE DATABASE geo_tests'); - $pdo->exec('CREATE DATABASE geo_tests_tmp'); - $pdo->exec('USE geo_tests'); $statement = $pdo->query('SELECT VERSION()'); $version = $statement->fetchColumn(); echo 'MySQL version: ' . $version . PHP_EOL; - // Connect data for doctrine integration tests $GLOBALS['db_type'] = 'pdo_mysql'; $GLOBALS['db_host'] = '127.0.0.1'; $GLOBALS['db_port'] = 3306; @@ -65,48 +53,32 @@ $GLOBALS['db_password'] = ''; $GLOBALS['db_name'] = 'geo_tests'; - $GLOBALS['tmpdb_type'] = 'pdo_mysql'; - $GLOBALS['tmpdb_host'] = '127.0.0.1'; - $GLOBALS['tmpdb_port'] = 3306; - $GLOBALS['tmpdb_username'] = 'root'; - $GLOBALS['tmpdb_password'] = ''; - $GLOBALS['tmpdb_name'] = 'geo_tests_tmp'; - // doctrine/dbal >= 2.13.0 $GLOBALS['db_driver'] = 'pdo_mysql'; $GLOBALS['db_user'] = 'root'; $GLOBALS['db_dbname'] = 'geo_tests'; - $GLOBALS['tmpdb_driver'] = 'pdo_mysql'; - $GLOBALS['tmpdb_user'] = 'root'; - $GLOBALS['tmpdb_dbname'] = 'geo_tests_tmp'; - - $engine = new PDOEngine($pdo); break; case 'PDO_PGSQL': - echo 'Using PDOEngine for PostgreSQL' . PHP_EOL; + echo 'Using PDO_PGSQL driver' . PHP_EOL; $pdo = new PDO('pgsql:host=localhost', 'postgres', 'postgres'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $pdo->exec('CREATE EXTENSION IF NOT EXISTS postgis;'); $pdo->exec('DROP DATABASE IF EXISTS geo_tests'); - $pdo->exec('DROP DATABASE IF EXISTS geo_tests_tmp'); $pdo->exec('CREATE DATABASE geo_tests'); - $pdo->exec('CREATE DATABASE geo_tests_tmp'); $statement = $pdo->query('SELECT version()'); - $version = $statement->fetchColumn(0); + $version = $statement->fetchColumn(); echo 'PostgreSQL version: ' . $version . PHP_EOL; $statement = $pdo->query('SELECT PostGIS_Version()'); - $version = $statement->fetchColumn(0); + $version = $statement->fetchColumn(); echo 'PostGIS version: ' . $version . PHP_EOL; - // Connect data for doctrine integration tests $GLOBALS['db_type'] = 'pdo_pgsql'; $GLOBALS['db_host'] = 'localhost'; $GLOBALS['db_port'] = 5432; @@ -114,54 +86,16 @@ $GLOBALS['db_password'] = 'postgres'; $GLOBALS['db_name'] = 'geo_tests'; - $GLOBALS['tmpdb_type'] = 'pdo_pgsql'; - $GLOBALS['tmpdb_host'] = 'localhost'; - $GLOBALS['tmpdb_port'] = 5432; - $GLOBALS['tmpdb_username'] = 'postgres'; - $GLOBALS['tmpdb_password'] = 'postgres'; - $GLOBALS['tmpdb_name'] = 'geo_tests_tmp'; - // doctrine/dbal >= 2.13.0 $GLOBALS['db_driver'] = 'pdo_pgsql'; $GLOBALS['db_user'] = 'postgres'; $GLOBALS['db_dbname'] = 'geo_tests'; - $GLOBALS['tmpdb_driver'] = 'pdo_pgsql'; - $GLOBALS['tmpdb_user'] = 'postgres'; - $GLOBALS['tmpdb_dbname'] = 'geo_tests_tmp'; - - $engine = new PDOEngine($pdo); - break; - - case 'SQLite3': - echo 'Using SQLite3Engine' . PHP_EOL; - - $sqlite3 = new SQLite3(':memory:'); - $sqlite3->enableExceptions(true); - - $sqliteVersion = $sqlite3->querySingle('SELECT sqlite_version()'); - echo 'SQLite version: ' . $sqliteVersion . PHP_EOL; - - $sqlite3->loadExtension('mod_spatialite.so'); - - $spatialiteVersion = $sqlite3->querySingle('SELECT spatialite_version()'); - echo 'SpatiaLite version: ' . $spatialiteVersion . PHP_EOL; - - $engine = new SQLite3Engine($sqlite3); - break; - - case 'GEOS': - echo 'Using GEOSEngine' . PHP_EOL; - echo 'GEOS version: ' . GEOSVersion() . PHP_EOL; - - $engine = new GEOSEngine(); break; default: - echo 'Unknown engine: ' . $engine . PHP_EOL; + echo 'Unknown driver: ' . $driver . PHP_EOL; exit(1); } - - GeometryEngineRegistry::set($engine); } })(); diff --git a/src/Types/GeometryType.php b/src/Types/GeometryType.php index 57a2e84..31e82bf 100644 --- a/src/Types/GeometryType.php +++ b/src/Types/GeometryType.php @@ -57,7 +57,7 @@ public function getName() public function getSQLDeclaration(array $column, AbstractPlatform $platform) { - if ($platform instanceof PostgreSqlPlatform) { + if ($platform->getName() === 'postgresql') { return 'GEOMETRY'; } @@ -106,7 +106,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform) { - if ($platform instanceof MySqlPlatform) { + if ($platform->getName() === 'mysql') { $sqlExpr = sprintf('BINARY %s', $sqlExpr); } diff --git a/tests/FunctionalTestCase.php b/tests/FunctionalTestCase.php index c7b7879..5a05557 100644 --- a/tests/FunctionalTestCase.php +++ b/tests/FunctionalTestCase.php @@ -6,23 +6,22 @@ use Brick\Geo\Point; use Brick\Geo\Doctrine\Tests\Fixtures; -use Brick\Geo\Engine\GeometryEngineRegistry; -use Brick\Geo\Engine\PDOEngine; use Doctrine\Common\DataFixtures\Executor\ORMExecutor; use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\DataFixtures\Loader; use Doctrine\Common\DataFixtures\Purger\ORMPurger; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\SchemaTool; use Doctrine\ORM\Tools\Setup; -use Doctrine\Tests\DbalFunctionalTestCase; +use PHPUnit\Framework\TestCase; /** * Base class for Doctrine types functional test cases. */ -abstract class FunctionalTestCase extends DbalFunctionalTestCase +abstract class FunctionalTestCase extends TestCase { private AbstractPlatform $platform; @@ -32,21 +31,15 @@ abstract class FunctionalTestCase extends DbalFunctionalTestCase private ORMExecutor $ormExecutor; + /** @var Connection */ + private $connection; + protected function setUp(): void { - if (! GeometryEngineRegistry::has()) { - self::markTestSkipped('This test requires a connection to a database.'); - } - - $engine = GeometryEngineRegistry::get(); - - if (! $engine instanceof PDOEngine) { - self::markTestSkipped('This test currently only works with a PDO connection.'); - } - parent::setUp(); - $this->platform = $this->_conn->getDatabasePlatform(); + $this->connection = TestUtil::getConnection(); + $this->platform = $this->connection->getDatabasePlatform(); $this->platform->registerDoctrineTypeMapping('geometry', 'binary'); $this->platform->registerDoctrineTypeMapping('linestring', 'binary'); @@ -58,14 +51,15 @@ protected function setUp(): void switch ($this->platform->getName()) { case 'postgresql': - $this->_conn->executeQuery('CREATE EXTENSION IF NOT EXISTS postgis;'); + $this->connection->executeQuery('CREATE EXTENSION IF NOT EXISTS postgis;'); break; } + $this->fixtureLoader = new Loader(); $config = Setup::createAnnotationMetadataConfiguration([__DIR__ . '/Fixtures'], false); - $this->em = EntityManager::create($this->_conn, $config, $this->platform->getEventManager()); + $this->em = EntityManager::create($this->connection, $config, $this->platform->getEventManager()); $schemaTool = new SchemaTool($this->em); $schemaTool->updateSchema([ diff --git a/tests/TestUtil.php b/tests/TestUtil.php new file mode 100644 index 0000000..9f406d9 --- /dev/null +++ b/tests/TestUtil.php @@ -0,0 +1,44 @@ +