Skip to content

Commit

Permalink
deps: Test on dbal v3 (#87)
Browse files Browse the repository at this point in the history
- Updates dev deps
- Fixes coverage in ci
- Migrate phpunit config to newer schema
- Ignore unmatched errors in phpstan
- Adds asserts to fix phpstan errors
- Fixes methods accessing dbal v3
- Use mysql80/postgressql10 platforms
  • Loading branch information
aszenz authored Jun 8, 2024
1 parent 0c0ab13 commit 37cf04d
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 62 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ jobs:
run: "vendor/bin/phpunit"

- name: "Run Coveralls"
run: "vendor/bin/coveralls -v"
run: "vendor/bin/php-coveralls -v"
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}

phpunit:
name: "PHPUnit"
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor/
composer.lock
phpunit.xml
build/
build/
.phpunit.result.cache
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"satooshi/php-coveralls": "~1.0",
"doctrine/dbal": "~2.5",
"php-coveralls/php-coveralls": "^2.0.0",
"doctrine/dbal": "^3.0",
"phpstan/phpstan": "^0.12.82"
},
"suggest": {
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ parameters:
inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- "#Mouf\\\\MoufManager#"
- "#Mouf\\\\MoufInstanceDescriptor#"
Expand Down
68 changes: 34 additions & 34 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
>

<php>
<var name="db_host" value="127.0.0.1" />
<var name="db_port" value="3306"/>
<var name="db_username" value="root" />
<var name="db_password" value="" />
<var name="db_driver" value="pdo_mysql"/>
</php>

<testsuites>
<testsuite name="Mouf Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="build/coverage" />
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage"/>
</report>
</coverage>
<php>
<var name="db_host" value="127.0.0.1"/>
<var name="db_port" value="3306"/>
<var name="db_username" value="root"/>
<var name="db_password" value=""/>
<var name="db_driver" value="pdo_mysql"/>
</php>
<testsuites>
<testsuite name="Mouf Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<logging/>
</phpunit>
12 changes: 8 additions & 4 deletions src/Mouf/Database/MagicQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function array_filter;
use function array_keys;
use Doctrine\Common\Cache\VoidCache;
use Doctrine\DBAL\Platforms\MySQL80Platform;
use function hash;
use function implode;
use Mouf\Database\MagicQuery\Twig\SqlTwigEnvironmentFactory;
Expand Down Expand Up @@ -54,7 +55,7 @@ class MagicQuery
public function __construct($connection = null, $cache = null, SchemaAnalyzer $schemaAnalyzer = null)
{
$this->connection = $connection;
$this->platform = $connection ? $connection->getDatabasePlatform() : new MySqlPlatform();
$this->platform = $connection ? $connection->getDatabasePlatform() : new MySQL80Platform();
if ($cache) {
$this->cache = $cache;
} else {
Expand Down Expand Up @@ -91,7 +92,7 @@ public function setOutputDialect(?AbstractPlatform $platform): self
if ($platform !== null) {
$this->platform = $platform;
} else {
$this->platform = $this->connection ? $this->connection->getDatabasePlatform() : new MySqlPlatform();
$this->platform = $this->connection ? $this->connection->getDatabasePlatform() : new MySQL80Platform();
}

return $this;
Expand Down Expand Up @@ -304,15 +305,18 @@ private function getSchemaAnalyzer()
throw new MagicQueryMissingConnectionException('In order to use MagicJoin, you need to configure a DBAL connection.');
}

$this->schemaAnalyzer = new SchemaAnalyzer($this->connection->getSchemaManager(), $this->cache, $this->getConnectionUniqueId($this->connection));
$this->schemaAnalyzer = new SchemaAnalyzer($this->connection->createSchemaManager(), $this->cache, $this->getConnectionUniqueId($this->connection));
}

return $this->schemaAnalyzer;
}

private function getConnectionUniqueId(Connection $connection): string
{
return hash('md4', $connection->getHost().'-'.$connection->getPort().'-'.$connection->getDatabase().'-'.$connection->getDriver()->getName());
$connectionParams = $connection->getParams();
\assert(\array_key_exists('host', $connectionParams));
\assert(\array_key_exists('port', $connectionParams));
return hash('md4', $connectionParams['host'].'-'.$connectionParams['port'].'-'.$connection->getDatabase().'-'.$connection->getDriver()->getDatabasePlatform()->getName());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Mouf/Database/QueryWriter/CountNbResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public function val()
{
$sql = 'SELECT count(*) as cnt FROM ('.$this->queryResult->toSql().') tmp';

return $this->connection->fetchColumn($sql);
return $this->connection->fetchOne($sql);
}
}
4 changes: 2 additions & 2 deletions src/Mouf/Database/QueryWriter/QueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public function setParameters($parameters): void
public function val()
{
$parameters = ValueUtils::val($this->parameters);
$pdoStatement = $this->connection->query($this->select->toSql($parameters, $this->connection->getDatabasePlatform()).DbHelper::getFromLimitString($this->offset, $this->limit));
$pdoStatement = $this->connection->prepare($this->select->toSql($parameters, $this->connection->getDatabasePlatform()).DbHelper::getFromLimitString($this->offset, $this->limit));

return new ResultSet($pdoStatement);
return new ResultSet($pdoStatement->getWrappedStatement());
}

/**
Expand Down
21 changes: 7 additions & 14 deletions src/Mouf/Database/QueryWriter/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Mouf\Database\QueryWriter;

use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Driver\Statement;

/**
Expand All @@ -16,7 +15,7 @@ class ResultSet implements \Iterator
private $statement;
/** @var int */
private $key = 0;
/** @var array|false */
/** @var array */
private $result;
/** @var bool */
private $fetched = false;
Expand All @@ -28,18 +27,15 @@ public function __construct(Statement $statement)
$this->statement = $statement;
}

public function rewind()
public function rewind(): void
{
++$this->rewindCalls;
if ($this->rewindCalls == 2) {
throw new \Exception("Error: rewind is not possible in a database rowset. You can call 'foreach' on the rowset only once. Use CachedResultSet to be able to call the result several times. TODO: develop CachedResultSet");
}
}

/**
* @return array|false
*/
public function current()
public function current(): array
{
if (!$this->fetched) {
$this->fetch();
Expand All @@ -48,15 +44,12 @@ public function current()
return $this->result;
}

/**
* @return int
*/
public function key()
public function key(): int
{
return $this->key;
}

public function next()
public function next(): void
{
++$this->key;
$this->fetched = false;
Expand All @@ -65,11 +58,11 @@ public function next()

private function fetch(): void
{
$this->result = $this->statement->fetch(FetchMode::ASSOCIATIVE);
$this->result = $this->statement->execute()->fetchAllAssociative();
$this->fetched = true;
}

public function valid()
public function valid(): bool
{
if (!$this->fetched) {
$this->fetch();
Expand Down
4 changes: 3 additions & 1 deletion src/SQLParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,9 @@ public static function simplify($nodes)
if (empty($operand->getBaseExpression())) {
$subTree = $operand->getSubTree();
if (is_array($subTree) && count($subTree) === 1) {
$newNodes = array_merge($newNodes, self::simplify($subTree));
$simplifiedSubTree = self::simplify($subTree);
\assert(is_array($simplifiedSubTree));
$newNodes = array_merge($newNodes, $simplifiedSubTree);
} else {
$newNodes[] = $operand;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Mouf\Database;
namespace Mouf\Database\MagicQuery\Twig;

use Mouf\Database\MagicQuery\Twig\SqlTwigEnvironmentFactory;
use PHPUnit\Framework\TestCase;
Expand Down
3 changes: 2 additions & 1 deletion tests/Mouf/Database/MagicQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Mouf\Database;

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Schema\Schema;
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
Expand Down Expand Up @@ -459,7 +460,7 @@ public function testBuildPreparedStatement()
public function testSetOutputDialect()
{
$magicQuery = new MagicQuery(null, new ArrayCache());
$magicQuery->setOutputDialect(new PostgreSqlPlatform());
$magicQuery->setOutputDialect(new PostgreSQL100Platform());

$sql = 'SELECT id FROM users';
$this->assertEquals('SELECT "id" FROM "users"', self::simplifySql($magicQuery->buildPreparedStatement($sql)));
Expand Down
19 changes: 19 additions & 0 deletions tests/phpunit-mysql8.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

# Use this file to start a MySQL8 database using Docker and then run the test suite on the MySQL8 database.

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR
cd ..

docker run --rm --name mysql8-magic-query-test -p 3306:3306 -p 33060:33060 -e MYSQL_ROOT_PASSWORD= -e MYSQL_ALLOW_EMPTY_PASSWORD=1 -d mysql:8 mysqld --default-authentication-plugin=mysql_native_password

# Let's wait for MySQL 8 to start
sleep 20

vendor/bin/phpunit -c phpunit.xml.dist $NO_COVERAGE
RESULT_CODE=$?

docker stop mysql8-magic-query-test

exit $RESULT_CODE

0 comments on commit 37cf04d

Please sign in to comment.