diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index d1ce87e..249f802 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -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" diff --git a/.gitignore b/.gitignore index 8fe7799..a008000 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ vendor/ composer.lock phpunit.xml -build/ \ No newline at end of file +build/ +.phpunit.result.cache \ No newline at end of file diff --git a/composer.json b/composer.json index 9e42897..5a62b47 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/phpstan.neon b/phpstan.neon index c85f7c3..f7010a7 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -8,6 +8,7 @@ parameters: inferPrivatePropertyTypeFromConstructor: true checkMissingIterableValueType: false checkGenericClassInNonGenericObjectType: false + reportUnmatchedIgnoredErrors: false ignoreErrors: - "#Mouf\\\\MoufManager#" - "#Mouf\\\\MoufInstanceDescriptor#" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 61da686..e7699e8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,37 +1,37 @@ - - - - - - - - - - - - - - ./tests/ - - - - - - src/ - - - - - - + + + src/ + + + + + + + + + + + + + + + + ./tests/ + + + diff --git a/src/Mouf/Database/MagicQuery.php b/src/Mouf/Database/MagicQuery.php index 7dbdb8e..a9c04f6 100644 --- a/src/Mouf/Database/MagicQuery.php +++ b/src/Mouf/Database/MagicQuery.php @@ -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; @@ -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 { @@ -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; @@ -304,7 +305,7 @@ 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; @@ -312,7 +313,10 @@ private function getSchemaAnalyzer() 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()); } /** diff --git a/src/Mouf/Database/QueryWriter/CountNbResult.php b/src/Mouf/Database/QueryWriter/CountNbResult.php index 0de2238..106425d 100644 --- a/src/Mouf/Database/QueryWriter/CountNbResult.php +++ b/src/Mouf/Database/QueryWriter/CountNbResult.php @@ -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); } } diff --git a/src/Mouf/Database/QueryWriter/QueryResult.php b/src/Mouf/Database/QueryWriter/QueryResult.php index d0287b3..3eab17a 100644 --- a/src/Mouf/Database/QueryWriter/QueryResult.php +++ b/src/Mouf/Database/QueryWriter/QueryResult.php @@ -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()); } /** diff --git a/src/Mouf/Database/QueryWriter/ResultSet.php b/src/Mouf/Database/QueryWriter/ResultSet.php index 5cffdec..85c41ca 100644 --- a/src/Mouf/Database/QueryWriter/ResultSet.php +++ b/src/Mouf/Database/QueryWriter/ResultSet.php @@ -2,7 +2,6 @@ namespace Mouf\Database\QueryWriter; -use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\Driver\Statement; /** @@ -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; @@ -28,7 +27,7 @@ public function __construct(Statement $statement) $this->statement = $statement; } - public function rewind() + public function rewind(): void { ++$this->rewindCalls; if ($this->rewindCalls == 2) { @@ -36,10 +35,7 @@ public function rewind() } } - /** - * @return array|false - */ - public function current() + public function current(): array { if (!$this->fetched) { $this->fetch(); @@ -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; @@ -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(); diff --git a/src/SQLParser/Node/NodeFactory.php b/src/SQLParser/Node/NodeFactory.php index 54df8f9..ecb37f1 100644 --- a/src/SQLParser/Node/NodeFactory.php +++ b/src/SQLParser/Node/NodeFactory.php @@ -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; } diff --git a/tests/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactoryTest.php b/tests/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactoryTest.php index 7d2fdc5..c72bba2 100644 --- a/tests/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactoryTest.php +++ b/tests/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactoryTest.php @@ -1,6 +1,6 @@ setOutputDialect(new PostgreSqlPlatform()); + $magicQuery->setOutputDialect(new PostgreSQL100Platform()); $sql = 'SELECT id FROM users'; $this->assertEquals('SELECT "id" FROM "users"', self::simplifySql($magicQuery->buildPreparedStatement($sql))); diff --git a/tests/phpunit-mysql8.sh b/tests/phpunit-mysql8.sh new file mode 100755 index 0000000..cd41775 --- /dev/null +++ b/tests/phpunit-mysql8.sh @@ -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