Skip to content

Commit

Permalink
Testing with php8 and DBAL^3
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoreram committed Mar 4, 2021
1 parent 2625da9 commit 3b180d2
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 118 deletions.
20 changes: 10 additions & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: 2
jobs:
test-php73:
test-php74:
docker:
- image: circleci/php:7.3-cli
- image: circleci/php:7.4-cli
- image: postgres:alpine
environment:
POSTGRES_PASSWORD: root
Expand All @@ -20,12 +20,12 @@ jobs:
- run:
name: Run tests
command: |
composer update -n --prefer-dist --prefer-lowest --no-suggest
php vendor/bin/phpunit
composer update -n --prefer-dist
.circleci/wait-and-run-phpunit.sh
test-php74:
test-php80:
docker:
- image: circleci/php:7.4-cli
- image: circleci/php:8.0-cli
- image: postgres:alpine
environment:
POSTGRES_PASSWORD: root
Expand All @@ -43,13 +43,13 @@ jobs:
- run:
name: Run tests
command: |
composer update -n --prefer-dist --no-suggest
php vendor/bin/phpunit
composer update -n --prefer-dist
.circleci/wait-and-run-phpunit.sh
workflows:
version: 2
test:
jobs:
- test-php73
- test-php74
- test-php74
- test-php80
17 changes: 17 additions & 0 deletions .circleci/wait-and-run-phpunit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

while ! nc -z localhost 3306;
do
echo "Waiting for mysql. Slepping";
sleep 1;
done;
echo "Connected to mysql!";

while ! nc -z localhost 5432;
do
echo "Waiting for Postgresql. Slepping";
sleep 1;
done;
echo "Connected to Postgresql!";

php vendor/bin/phpunit
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor
/var
/composer.lock
/.php_cs.cache
/.php_cs.cache
/.phpunit.result.cache
1 change: 0 additions & 1 deletion .php_cs.cache

This file was deleted.

8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
}
],
"require": {
"php": "^7.3",
"doctrine/dbal": "^2.5",
"php": "^7.4 || ^8.0",
"doctrine/dbal": "^3",
"react/event-loop": "^1"
},
"require-dev": {
"phpunit/phpunit": "7.5.17",
"clue/block-react": "*",
"phpunit/phpunit": "^9",
"clue/block-react": "^1",
"react/mysql": "^0.5",
"clue/reactphp-sqlite": "^1",
"voryx/pgasync": "^2"
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
convertErrorsToExceptions="false"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false"
convertDeprecationsToExceptions="false"
processIsolation="false"
stopOnFailure="true"
bootstrap="vendor/autoload.php"
Expand Down
2 changes: 1 addition & 1 deletion src/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function toString(): string
$this->dbName
);

if (strpos($asString, ':@') === 0) {
if (0 === strpos($asString, ':@')) {
return rawurldecode(
substr($asString, 2)
);
Expand Down
25 changes: 25 additions & 0 deletions src/Driver/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the DriftPHP Project
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
*/

declare(strict_types=1);

namespace Drift\DBAL\Driver;

use Doctrine\DBAL\Driver\AbstractException;

/**
* Class Exception.
*/
class Exception extends AbstractException
{
}
30 changes: 11 additions & 19 deletions src/Driver/Mysql/MysqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

namespace Drift\DBAL\Driver\Mysql;

use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
use Doctrine\DBAL\Driver\API\MySQL\ExceptionConverter;
use Doctrine\DBAL\Query;
use Drift\DBAL\Credentials;
use Drift\DBAL\Driver\AbstractDriver;
use Drift\DBAL\Driver\PlainDriverException;
use Drift\DBAL\Driver\Exception as DoctrineException;
use Drift\DBAL\Result;
use React\EventLoop\LoopInterface;
use React\MySQL\ConnectionInterface;
Expand All @@ -32,20 +35,10 @@
*/
class MysqlDriver extends AbstractDriver
{
/**
* @var Factory
*/
private $factory;

/**
* @var ConnectionInterface
*/
private $connection;

/**
* @var EmptyDoctrineMysqlDriver
*/
private $doctrineDriver;
private Factory $factory;
private ConnectionInterface $connection;
private EmptyDoctrineMysqlDriver $doctrineDriver;
private ExceptionConverterInterface $exceptionConverter;

/**
* MysqlDriver constructor.
Expand All @@ -59,6 +52,7 @@ public function __construct(LoopInterface $loop, ConnectorInterface $connector =
$this->factory = is_null($connector)
? new Factory($loop)
: new Factory($loop, $connector);
$this->exceptionConverter = new ExceptionConverter();
}

/**
Expand Down Expand Up @@ -88,10 +82,8 @@ public function query(
$queryResult->affectedRows
);
})
->otherwise(function (Exception $exception) {
$message = $exception->getMessage();

throw $this->doctrineDriver->convertException($message, PlainDriverException::createFromMessageAndErrorCode($message, (string) $exception->getCode()));
->otherwise(function (Exception $exception) use (&$sql, &$parameters) {
throw $this->exceptionConverter->convert(new DoctrineException($exception->getMessage(), null, $exception->getCode()), new Query($sql, $parameters, []));
});
}
}
41 changes: 16 additions & 25 deletions src/Driver/PostgreSQL/PostgreSQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@

namespace Drift\DBAL\Driver\PostgreSQL;

use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
use Doctrine\DBAL\Driver\API\PostgreSQL\ExceptionConverter;
use Doctrine\DBAL\Query;
use Doctrine\DBAL\Query\QueryBuilder;
use Drift\DBAL\Credentials;
use Drift\DBAL\Driver\AbstractDriver;
use Drift\DBAL\Driver\PlainDriverException;
use Drift\DBAL\Driver\Exception as DoctrineException;
use Drift\DBAL\Result;
use PgAsync\Client;
use PgAsync\ErrorException;
Expand All @@ -31,20 +34,10 @@
*/
class PostgreSQLDriver extends AbstractDriver
{
/**
* @var Client
*/
private $client;

/**
* @var LoopInterface
*/
private $loop;

/**
* @var EmptyDoctrinePostgreSQLDriver
*/
private $doctrineDriver;
private Client $client;
private LoopInterface $loop;
private EmptyDoctrinePostgreSQLDriver $doctrineDriver;
private ExceptionConverterInterface $exceptionConverter;

/**
* @param LoopInterface $loop
Expand All @@ -53,6 +46,7 @@ public function __construct(LoopInterface $loop)
{
$this->doctrineDriver = new EmptyDoctrinePostgreSQLDriver();
$this->loop = $loop;
$this->exceptionConverter = new ExceptionConverter();
}

/**
Expand Down Expand Up @@ -92,24 +86,21 @@ public function query(
->executeStatement($sql, $parameters)
->subscribe(function ($row) use (&$results) {
$results[] = $row;
}, function (ErrorException $exception) use ($deferred) {
}, function (ErrorException $exception) use ($deferred, &$sql, &$parameters) {
$errorResponse = $exception->getErrorResponse();
$message = $exception->getMessage();
$code = 0;
foreach ($errorResponse->getErrorMessages() as $messageLine) {
if ('C' === $messageLine['type']) {
$code = $messageLine['message'];
}
}

$exception = $this
->doctrineDriver
->convertException(
$message,
PlainDriverException::createFromMessageAndErrorCode(
$message,
(string) $code
));
$exception = $this->exceptionConverter->convert(
new DoctrineException($exception->getMessage(), \strval($code)),
new Query(
$sql, $parameters, []
)
);

$deferred->reject($exception);
}, function () use (&$results, $deferred) {
Expand Down
30 changes: 11 additions & 19 deletions src/Driver/SQLite/SQLiteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
use Clue\React\SQLite\DatabaseInterface;
use Clue\React\SQLite\Factory;
use Clue\React\SQLite\Result as SQLiteResult;
use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
use Doctrine\DBAL\Driver\API\SQLite\ExceptionConverter;
use Doctrine\DBAL\Query;
use Drift\DBAL\Credentials;
use Drift\DBAL\Driver\AbstractDriver;
use Drift\DBAL\Driver\PlainDriverException;
use Drift\DBAL\Driver\Exception as DoctrineException;
use Drift\DBAL\Result;
use React\EventLoop\LoopInterface;
use React\Promise\PromiseInterface;
Expand All @@ -31,20 +34,10 @@
*/
class SQLiteDriver extends AbstractDriver
{
/**
* @var Factory
*/
private $factory;

/**
* @var DatabaseInterface
*/
private $database;

/**
* @var EmptyDoctrineSQLiteDriver
*/
private $doctrineDriver;
private Factory $factory;
private DatabaseInterface $database;
private EmptyDoctrineSQLiteDriver $doctrineDriver;
private ExceptionConverterInterface $exceptionConverter;

/**
* SQLiteDriver constructor.
Expand All @@ -55,6 +48,7 @@ public function __construct(LoopInterface $loop)
{
$this->doctrineDriver = new EmptyDoctrineSQLiteDriver();
$this->factory = new Factory($loop);
$this->exceptionConverter = new ExceptionConverter();
}

/**
Expand Down Expand Up @@ -84,10 +78,8 @@ public function query(
$sqliteResult->changed
);
})
->otherwise(function (RuntimeException $exception) {
$message = $exception->getMessage();

throw $this->doctrineDriver->convertException($message, PlainDriverException::createFromMessageAndErrorCode($message, (string) $exception->getCode()));
->otherwise(function (RuntimeException $exception) use (&$sql, &$parameters) {
throw $this->exceptionConverter->convert(new DoctrineException($exception->getMessage()), new Query($sql, $parameters, []));
});
}
}
Loading

0 comments on commit 3b180d2

Please sign in to comment.