Skip to content

Commit 3b180d2

Browse files
committed
Testing with php8 and DBAL^3
1 parent 2625da9 commit 3b180d2

17 files changed

+154
-118
lines changed

.circleci/config.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
version: 2
22
jobs:
3-
test-php73:
3+
test-php74:
44
docker:
5-
- image: circleci/php:7.3-cli
5+
- image: circleci/php:7.4-cli
66
- image: postgres:alpine
77
environment:
88
POSTGRES_PASSWORD: root
@@ -20,12 +20,12 @@ jobs:
2020
- run:
2121
name: Run tests
2222
command: |
23-
composer update -n --prefer-dist --prefer-lowest --no-suggest
24-
php vendor/bin/phpunit
23+
composer update -n --prefer-dist
24+
.circleci/wait-and-run-phpunit.sh
2525
26-
test-php74:
26+
test-php80:
2727
docker:
28-
- image: circleci/php:7.4-cli
28+
- image: circleci/php:8.0-cli
2929
- image: postgres:alpine
3030
environment:
3131
POSTGRES_PASSWORD: root
@@ -43,13 +43,13 @@ jobs:
4343
- run:
4444
name: Run tests
4545
command: |
46-
composer update -n --prefer-dist --no-suggest
47-
php vendor/bin/phpunit
46+
composer update -n --prefer-dist
47+
.circleci/wait-and-run-phpunit.sh
4848
4949
5050
workflows:
5151
version: 2
5252
test:
5353
jobs:
54-
- test-php73
55-
- test-php74
54+
- test-php74
55+
- test-php80

.circleci/wait-and-run-phpunit.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
while ! nc -z localhost 3306;
4+
do
5+
echo "Waiting for mysql. Slepping";
6+
sleep 1;
7+
done;
8+
echo "Connected to mysql!";
9+
10+
while ! nc -z localhost 5432;
11+
do
12+
echo "Waiting for Postgresql. Slepping";
13+
sleep 1;
14+
done;
15+
echo "Connected to Postgresql!";
16+
17+
php vendor/bin/phpunit

.gitignore

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

.php_cs.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^7.3",
14-
"doctrine/dbal": "^2.5",
13+
"php": "^7.4 || ^8.0",
14+
"doctrine/dbal": "^3",
1515
"react/event-loop": "^1"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit": "7.5.17",
19-
"clue/block-react": "*",
18+
"phpunit/phpunit": "^9",
19+
"clue/block-react": "^1",
2020
"react/mysql": "^0.5",
2121
"clue/reactphp-sqlite": "^1",
2222
"voryx/pgasync": "^2"

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
convertErrorsToExceptions="false"
77
convertNoticesToExceptions="false"
88
convertWarningsToExceptions="false"
9+
convertDeprecationsToExceptions="false"
910
processIsolation="false"
1011
stopOnFailure="true"
1112
bootstrap="vendor/autoload.php"

src/Credentials.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function toString(): string
138138
$this->dbName
139139
);
140140

141-
if (strpos($asString, ':@') === 0) {
141+
if (0 === strpos($asString, ':@')) {
142142
return rawurldecode(
143143
substr($asString, 2)
144144
);

src/Driver/Exception.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DriftPHP Project
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <[email protected]>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Drift\DBAL\Driver;
17+
18+
use Doctrine\DBAL\Driver\AbstractException;
19+
20+
/**
21+
* Class Exception.
22+
*/
23+
class Exception extends AbstractException
24+
{
25+
}

src/Driver/Mysql/MysqlDriver.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515

1616
namespace Drift\DBAL\Driver\Mysql;
1717

18+
use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
19+
use Doctrine\DBAL\Driver\API\MySQL\ExceptionConverter;
20+
use Doctrine\DBAL\Query;
1821
use Drift\DBAL\Credentials;
1922
use Drift\DBAL\Driver\AbstractDriver;
20-
use Drift\DBAL\Driver\PlainDriverException;
23+
use Drift\DBAL\Driver\Exception as DoctrineException;
2124
use Drift\DBAL\Result;
2225
use React\EventLoop\LoopInterface;
2326
use React\MySQL\ConnectionInterface;
@@ -32,20 +35,10 @@
3235
*/
3336
class MysqlDriver extends AbstractDriver
3437
{
35-
/**
36-
* @var Factory
37-
*/
38-
private $factory;
39-
40-
/**
41-
* @var ConnectionInterface
42-
*/
43-
private $connection;
44-
45-
/**
46-
* @var EmptyDoctrineMysqlDriver
47-
*/
48-
private $doctrineDriver;
38+
private Factory $factory;
39+
private ConnectionInterface $connection;
40+
private EmptyDoctrineMysqlDriver $doctrineDriver;
41+
private ExceptionConverterInterface $exceptionConverter;
4942

5043
/**
5144
* MysqlDriver constructor.
@@ -59,6 +52,7 @@ public function __construct(LoopInterface $loop, ConnectorInterface $connector =
5952
$this->factory = is_null($connector)
6053
? new Factory($loop)
6154
: new Factory($loop, $connector);
55+
$this->exceptionConverter = new ExceptionConverter();
6256
}
6357

6458
/**
@@ -88,10 +82,8 @@ public function query(
8882
$queryResult->affectedRows
8983
);
9084
})
91-
->otherwise(function (Exception $exception) {
92-
$message = $exception->getMessage();
93-
94-
throw $this->doctrineDriver->convertException($message, PlainDriverException::createFromMessageAndErrorCode($message, (string) $exception->getCode()));
85+
->otherwise(function (Exception $exception) use (&$sql, &$parameters) {
86+
throw $this->exceptionConverter->convert(new DoctrineException($exception->getMessage(), null, $exception->getCode()), new Query($sql, $parameters, []));
9587
});
9688
}
9789
}

src/Driver/PostgreSQL/PostgreSQLDriver.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515

1616
namespace Drift\DBAL\Driver\PostgreSQL;
1717

18+
use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
19+
use Doctrine\DBAL\Driver\API\PostgreSQL\ExceptionConverter;
20+
use Doctrine\DBAL\Query;
1821
use Doctrine\DBAL\Query\QueryBuilder;
1922
use Drift\DBAL\Credentials;
2023
use Drift\DBAL\Driver\AbstractDriver;
21-
use Drift\DBAL\Driver\PlainDriverException;
24+
use Drift\DBAL\Driver\Exception as DoctrineException;
2225
use Drift\DBAL\Result;
2326
use PgAsync\Client;
2427
use PgAsync\ErrorException;
@@ -31,20 +34,10 @@
3134
*/
3235
class PostgreSQLDriver extends AbstractDriver
3336
{
34-
/**
35-
* @var Client
36-
*/
37-
private $client;
38-
39-
/**
40-
* @var LoopInterface
41-
*/
42-
private $loop;
43-
44-
/**
45-
* @var EmptyDoctrinePostgreSQLDriver
46-
*/
47-
private $doctrineDriver;
37+
private Client $client;
38+
private LoopInterface $loop;
39+
private EmptyDoctrinePostgreSQLDriver $doctrineDriver;
40+
private ExceptionConverterInterface $exceptionConverter;
4841

4942
/**
5043
* @param LoopInterface $loop
@@ -53,6 +46,7 @@ public function __construct(LoopInterface $loop)
5346
{
5447
$this->doctrineDriver = new EmptyDoctrinePostgreSQLDriver();
5548
$this->loop = $loop;
49+
$this->exceptionConverter = new ExceptionConverter();
5650
}
5751

5852
/**
@@ -92,24 +86,21 @@ public function query(
9286
->executeStatement($sql, $parameters)
9387
->subscribe(function ($row) use (&$results) {
9488
$results[] = $row;
95-
}, function (ErrorException $exception) use ($deferred) {
89+
}, function (ErrorException $exception) use ($deferred, &$sql, &$parameters) {
9690
$errorResponse = $exception->getErrorResponse();
97-
$message = $exception->getMessage();
9891
$code = 0;
9992
foreach ($errorResponse->getErrorMessages() as $messageLine) {
10093
if ('C' === $messageLine['type']) {
10194
$code = $messageLine['message'];
10295
}
10396
}
10497

105-
$exception = $this
106-
->doctrineDriver
107-
->convertException(
108-
$message,
109-
PlainDriverException::createFromMessageAndErrorCode(
110-
$message,
111-
(string) $code
112-
));
98+
$exception = $this->exceptionConverter->convert(
99+
new DoctrineException($exception->getMessage(), \strval($code)),
100+
new Query(
101+
$sql, $parameters, []
102+
)
103+
);
113104

114105
$deferred->reject($exception);
115106
}, function () use (&$results, $deferred) {

0 commit comments

Comments
 (0)