Skip to content

Commit 37cf04d

Browse files
authored
deps: Test on dbal v3 (#87)
- 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
1 parent 0c0ab13 commit 37cf04d

File tree

13 files changed

+85
-62
lines changed

13 files changed

+85
-62
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ jobs:
140140
run: "vendor/bin/phpunit"
141141

142142
- name: "Run Coveralls"
143-
run: "vendor/bin/coveralls -v"
143+
run: "vendor/bin/php-coveralls -v"
144+
env:
145+
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
144146

145147
phpunit:
146148
name: "PHPUnit"

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
vendor/
22
composer.lock
33
phpunit.xml
4-
build/
4+
build/
5+
.phpunit.result.cache

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
},
2626
"require-dev": {
2727
"phpunit/phpunit": "^9.5",
28-
"satooshi/php-coveralls": "~1.0",
29-
"doctrine/dbal": "~2.5",
28+
"php-coveralls/php-coveralls": "^2.0.0",
29+
"doctrine/dbal": "^3.0",
3030
"phpstan/phpstan": "^0.12.82"
3131
},
3232
"suggest": {

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ parameters:
88
inferPrivatePropertyTypeFromConstructor: true
99
checkMissingIterableValueType: false
1010
checkGenericClassInNonGenericObjectType: false
11+
reportUnmatchedIgnoredErrors: false
1112
ignoreErrors:
1213
- "#Mouf\\\\MoufManager#"
1314
- "#Mouf\\\\MoufInstanceDescriptor#"

phpunit.xml.dist

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false"
11-
bootstrap="tests/bootstrap.php"
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
colors="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
processIsolation="false"
12+
stopOnFailure="false"
13+
bootstrap="tests/bootstrap.php"
1214
>
13-
14-
<php>
15-
<var name="db_host" value="127.0.0.1" />
16-
<var name="db_port" value="3306"/>
17-
<var name="db_username" value="root" />
18-
<var name="db_password" value="" />
19-
<var name="db_driver" value="pdo_mysql"/>
20-
</php>
21-
22-
<testsuites>
23-
<testsuite name="Mouf Test Suite">
24-
<directory>./tests/</directory>
25-
</testsuite>
26-
</testsuites>
27-
28-
<filter>
29-
<whitelist processUncoveredFilesFromWhitelist="true">
30-
<directory suffix=".php">src/</directory>
31-
</whitelist>
32-
</filter>
33-
<logging>
34-
<log type="coverage-html" target="build/coverage" />
35-
<log type="coverage-clover" target="build/logs/clover.xml"/>
36-
</logging>
15+
<coverage processUncoveredFiles="true">
16+
<include>
17+
<directory suffix=".php">src/</directory>
18+
</include>
19+
<report>
20+
<clover outputFile="build/logs/clover.xml"/>
21+
<html outputDirectory="build/coverage"/>
22+
</report>
23+
</coverage>
24+
<php>
25+
<var name="db_host" value="127.0.0.1"/>
26+
<var name="db_port" value="3306"/>
27+
<var name="db_username" value="root"/>
28+
<var name="db_password" value=""/>
29+
<var name="db_driver" value="pdo_mysql"/>
30+
</php>
31+
<testsuites>
32+
<testsuite name="Mouf Test Suite">
33+
<directory>./tests/</directory>
34+
</testsuite>
35+
</testsuites>
36+
<logging/>
3737
</phpunit>

src/Mouf/Database/MagicQuery.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use function array_filter;
1111
use function array_keys;
1212
use Doctrine\Common\Cache\VoidCache;
13+
use Doctrine\DBAL\Platforms\MySQL80Platform;
1314
use function hash;
1415
use function implode;
1516
use Mouf\Database\MagicQuery\Twig\SqlTwigEnvironmentFactory;
@@ -54,7 +55,7 @@ class MagicQuery
5455
public function __construct($connection = null, $cache = null, SchemaAnalyzer $schemaAnalyzer = null)
5556
{
5657
$this->connection = $connection;
57-
$this->platform = $connection ? $connection->getDatabasePlatform() : new MySqlPlatform();
58+
$this->platform = $connection ? $connection->getDatabasePlatform() : new MySQL80Platform();
5859
if ($cache) {
5960
$this->cache = $cache;
6061
} else {
@@ -91,7 +92,7 @@ public function setOutputDialect(?AbstractPlatform $platform): self
9192
if ($platform !== null) {
9293
$this->platform = $platform;
9394
} else {
94-
$this->platform = $this->connection ? $this->connection->getDatabasePlatform() : new MySqlPlatform();
95+
$this->platform = $this->connection ? $this->connection->getDatabasePlatform() : new MySQL80Platform();
9596
}
9697

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

307-
$this->schemaAnalyzer = new SchemaAnalyzer($this->connection->getSchemaManager(), $this->cache, $this->getConnectionUniqueId($this->connection));
308+
$this->schemaAnalyzer = new SchemaAnalyzer($this->connection->createSchemaManager(), $this->cache, $this->getConnectionUniqueId($this->connection));
308309
}
309310

310311
return $this->schemaAnalyzer;
311312
}
312313

313314
private function getConnectionUniqueId(Connection $connection): string
314315
{
315-
return hash('md4', $connection->getHost().'-'.$connection->getPort().'-'.$connection->getDatabase().'-'.$connection->getDriver()->getName());
316+
$connectionParams = $connection->getParams();
317+
\assert(\array_key_exists('host', $connectionParams));
318+
\assert(\array_key_exists('port', $connectionParams));
319+
return hash('md4', $connectionParams['host'].'-'.$connectionParams['port'].'-'.$connection->getDatabase().'-'.$connection->getDriver()->getDatabasePlatform()->getName());
316320
}
317321

318322
/**

src/Mouf/Database/QueryWriter/CountNbResult.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ public function val()
5050
{
5151
$sql = 'SELECT count(*) as cnt FROM ('.$this->queryResult->toSql().') tmp';
5252

53-
return $this->connection->fetchColumn($sql);
53+
return $this->connection->fetchOne($sql);
5454
}
5555
}

src/Mouf/Database/QueryWriter/QueryResult.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ public function setParameters($parameters): void
7777
public function val()
7878
{
7979
$parameters = ValueUtils::val($this->parameters);
80-
$pdoStatement = $this->connection->query($this->select->toSql($parameters, $this->connection->getDatabasePlatform()).DbHelper::getFromLimitString($this->offset, $this->limit));
80+
$pdoStatement = $this->connection->prepare($this->select->toSql($parameters, $this->connection->getDatabasePlatform()).DbHelper::getFromLimitString($this->offset, $this->limit));
8181

82-
return new ResultSet($pdoStatement);
82+
return new ResultSet($pdoStatement->getWrappedStatement());
8383
}
8484

8585
/**

src/Mouf/Database/QueryWriter/ResultSet.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Mouf\Database\QueryWriter;
44

5-
use Doctrine\DBAL\FetchMode;
65
use Doctrine\DBAL\Driver\Statement;
76

87
/**
@@ -16,7 +15,7 @@ class ResultSet implements \Iterator
1615
private $statement;
1716
/** @var int */
1817
private $key = 0;
19-
/** @var array|false */
18+
/** @var array */
2019
private $result;
2120
/** @var bool */
2221
private $fetched = false;
@@ -28,18 +27,15 @@ public function __construct(Statement $statement)
2827
$this->statement = $statement;
2928
}
3029

31-
public function rewind()
30+
public function rewind(): void
3231
{
3332
++$this->rewindCalls;
3433
if ($this->rewindCalls == 2) {
3534
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");
3635
}
3736
}
3837

39-
/**
40-
* @return array|false
41-
*/
42-
public function current()
38+
public function current(): array
4339
{
4440
if (!$this->fetched) {
4541
$this->fetch();
@@ -48,15 +44,12 @@ public function current()
4844
return $this->result;
4945
}
5046

51-
/**
52-
* @return int
53-
*/
54-
public function key()
47+
public function key(): int
5548
{
5649
return $this->key;
5750
}
5851

59-
public function next()
52+
public function next(): void
6053
{
6154
++$this->key;
6255
$this->fetched = false;
@@ -65,11 +58,11 @@ public function next()
6558

6659
private function fetch(): void
6760
{
68-
$this->result = $this->statement->fetch(FetchMode::ASSOCIATIVE);
61+
$this->result = $this->statement->execute()->fetchAllAssociative();
6962
$this->fetched = true;
7063
}
7164

72-
public function valid()
65+
public function valid(): bool
7366
{
7467
if (!$this->fetched) {
7568
$this->fetch();

src/SQLParser/Node/NodeFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,9 @@ public static function simplify($nodes)
653653
if (empty($operand->getBaseExpression())) {
654654
$subTree = $operand->getSubTree();
655655
if (is_array($subTree) && count($subTree) === 1) {
656-
$newNodes = array_merge($newNodes, self::simplify($subTree));
656+
$simplifiedSubTree = self::simplify($subTree);
657+
\assert(is_array($simplifiedSubTree));
658+
$newNodes = array_merge($newNodes, $simplifiedSubTree);
657659
} else {
658660
$newNodes[] = $operand;
659661
}

tests/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Mouf\Database;
3+
namespace Mouf\Database\MagicQuery\Twig;
44

55
use Mouf\Database\MagicQuery\Twig\SqlTwigEnvironmentFactory;
66
use PHPUnit\Framework\TestCase;

tests/Mouf/Database/MagicQueryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Mouf\Database;
44

55
use Doctrine\Common\Cache\ArrayCache;
6+
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
67
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
78
use Doctrine\DBAL\Schema\Schema;
89
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
@@ -459,7 +460,7 @@ public function testBuildPreparedStatement()
459460
public function testSetOutputDialect()
460461
{
461462
$magicQuery = new MagicQuery(null, new ArrayCache());
462-
$magicQuery->setOutputDialect(new PostgreSqlPlatform());
463+
$magicQuery->setOutputDialect(new PostgreSQL100Platform());
463464

464465
$sql = 'SELECT id FROM users';
465466
$this->assertEquals('SELECT "id" FROM "users"', self::simplifySql($magicQuery->buildPreparedStatement($sql)));

tests/phpunit-mysql8.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
# Use this file to start a MySQL8 database using Docker and then run the test suite on the MySQL8 database.
4+
5+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6+
cd $DIR
7+
cd ..
8+
9+
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
10+
11+
# Let's wait for MySQL 8 to start
12+
sleep 20
13+
14+
vendor/bin/phpunit -c phpunit.xml.dist $NO_COVERAGE
15+
RESULT_CODE=$?
16+
17+
docker stop mysql8-magic-query-test
18+
19+
exit $RESULT_CODE

0 commit comments

Comments
 (0)