Skip to content

Commit

Permalink
Merge pull request #14 from dragosprotung/pdo_connection_with_err_mod…
Browse files Browse the repository at this point in the history
…e_exception

Catch PDOExceptions and check error codes
  • Loading branch information
prolic authored May 30, 2017
2 parents 7e23241 + 04deb46 commit 788210f
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 55 deletions.
20 changes: 19 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ matrix:
- DEPENDENCIES=""
- DRIVER="pdo_mysql"
- DB=mysql_5.7
- php: 7.1
sudo: true
env:
- DEPENDENCIES=""
- DRIVER="pdo_mysql"
- DB=mysql_5.7
- DB_ATTR_ERRMODE=2 # \PDO::ERRMODE_EXCEPTION
- php: 7.1
sudo: true
env:
Expand Down Expand Up @@ -63,14 +70,25 @@ matrix:
- DB=postgres_9.5
addons:
postgresql: '9.5'
- php: 7.1
dist: trusty
env:
- DEPENDENCIES=""
- EXECUTE_CS_CHECK=false
- TEST_COVERAGE=true
- DRIVER="pdo_pgsql"
- DB=postgres_9.5
- DB_ATTR_ERRMODE=2 # \PDO::ERRMODE_EXCEPTION
addons:
postgresql: '9.5'
- php: 7.1
dist: trusty
env:
- DEPENDENCIES="--prefer-lowest --prefer-stable"
- DRIVER="pdo_pgsql"
- DB=postgres_9.5
addons:
postgresql: '9.5'
postgresql: '9.5'

cache:
directories:
Expand Down
25 changes: 25 additions & 0 deletions docker-compose-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: '2'
services:
composer:
image: prooph/composer:7.1
volumes:
- .:/app
links:
- postgres:postgres
- mysql:mysql

postgres:
image: postgres:alpine
ports:
- 5432:5432
environment:
- POSTGRES_DB=snapshot_tests

mysql:
image: mysql
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_DATABASE=snapshot_tests
36 changes: 20 additions & 16 deletions phpunit.xml.pdo_mysql
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuite name="Prooph PDO Snapshot Store Test Suite">
<directory>./tests</directory>
Expand All @@ -22,11 +24,13 @@
</filter>

<php>
<var name="db_type" value="pdo_mysql"/>
<var name="db_host" value="127.0.0.1" />
<var name="db_username" value="root" />
<var name="db_password" value="" />
<var name="db_name" value="snapshot_tests" />
<var name="db_port" value="3306"/>
<env name="db_type" value="pdo_mysql"/>
<env name="db_host" value="127.0.0.1"/>
<env name="db_username" value="root"/>
<env name="db_password" value=""/>
<env name="db_name" value="snapshot_tests"/>
<env name="db_port" value="3306"/>
<!-- \PDO::ERRMODE_SILENT -->
<env name="DB_ATTR_ERRMODE" value="0"/>
</php>
</phpunit>
36 changes: 20 additions & 16 deletions phpunit.xml.pdo_pgsql
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuite name="Prooph PDO Snapshot Store Test Suite">
<directory>./tests</directory>
Expand All @@ -22,11 +24,13 @@
</filter>

<php>
<var name="db_type" value="pdo_pgsql"/>
<var name="db_host" value="localhost" />
<var name="db_username" value="postgres" />
<var name="db_password" value="" />
<var name="db_name" value="snapshot_tests" />
<var name="db_port" value="5432"/>
<env name="db_type" value="pdo_pgsql"/>
<env name="db_host" value="localhost"/>
<env name="db_username" value="postgres"/>
<env name="db_password" value=""/>
<env name="db_name" value="snapshot_tests"/>
<env name="db_port" value="5432"/>
<!-- \PDO::ERRMODE_SILENT -->
<env name="DB_ATTR_ERRMODE" value="0"/>
</php>
</phpunit>
29 changes: 29 additions & 0 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* This file is part of the prooph/pdo-snapshot-store.
* (c) 2016-2017 prooph software GmbH <contact@prooph.de>
* (c) 2016-2017 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Prooph\SnapshotStore\Pdo\Exception;

use RuntimeException as PHPRuntimeException;

class RuntimeException extends PHPRuntimeException
{
public static function fromStatementErrorInfo(array $errorInfo): RuntimeException
{
return new self(
sprintf(
"Error %s. \nError-Info: %s",
$errorInfo[0],
$errorInfo[2]
)
);
}
}
32 changes: 28 additions & 4 deletions src/PdoSnapshotStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
namespace Prooph\SnapshotStore\Pdo;

use PDO;
use PDOException;
use Prooph\SnapshotStore\CallbackSerializer;
use Prooph\SnapshotStore\Pdo\Exception\RuntimeException;
use Prooph\SnapshotStore\Serializer;
use Prooph\SnapshotStore\Snapshot;
use Prooph\SnapshotStore\SnapshotStore;
Expand Down Expand Up @@ -63,7 +65,15 @@ public function get(string $aggregateType, string $aggregateId): ?Snapshot
EOT;

$statement = $this->connection->prepare($query);
$statement->execute([$aggregateId]);
try {
$statement->execute([$aggregateId]);
} catch (PDOException $exception) {
// ignore and check error code
}

if ($statement->errorCode() !== '00000') {
throw RuntimeException::fromStatementErrorInfo($statement->errorInfo());
}

$result = $statement->fetch(\PDO::FETCH_OBJ);

Expand Down Expand Up @@ -129,8 +139,13 @@ public function save(Snapshot ...$snapshots): void

$this->connection->beginTransaction();

foreach ($statements as $statement) {
$statement->execute();
try {
foreach ($statements as $statement) {
$statement->execute();
}
} catch (PDOException $exception) {
$this->connection->rollBack();
throw RuntimeException::fromStatementErrorInfo($statement->errorInfo());
}

$this->connection->commit();
Expand All @@ -148,7 +163,16 @@ public function removeAll(string $aggregateType): void

$this->connection->beginTransaction();

$statement->execute([$aggregateType]);
try {
$statement->execute([$aggregateType]);
} catch (PDOException $exception) {
// ignore and check error code
}

if ($statement->errorCode() !== '00000') {
$this->connection->rollBack();
throw RuntimeException::fromStatementErrorInfo($statement->errorInfo());
}

$this->connection->commit();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/PdoSnapshotStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function it_uses_custom_snapshot_table_map()
protected function setUp(): void
{
$this->connection = TestUtil::getConnection();

$this->connection->exec('DROP TABLE IF EXISTS snapshots');
switch (TestUtil::getDatabaseVendor()) {
case 'pdo_mysql':
$this->connection->exec(file_get_contents(__DIR__ . '/../scripts/mysql_snapshot_table.sql'));
Expand All @@ -168,9 +168,9 @@ protected function setUp(): void

protected function tearDown(): void
{
$statement = $this->connection->prepare('TRUNCATE snapshots');
$statement = $this->connection->prepare('DROP TABLE IF EXISTS snapshots');
$statement->execute();
$statement = $this->connection->prepare('TRUNCATE bar');
$statement = $this->connection->prepare('DROP TABLE IF EXISTS bar');
$statement->execute();
}

Expand Down
33 changes: 18 additions & 15 deletions tests/TestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function getConnection(): PDO
$dsn .= 'dbname=' . $connectionParams['dbname'] . $separator;
$dsn = rtrim($dsn);

return new PDO($dsn, $connectionParams['user'], $connectionParams['password']);
return new PDO($dsn, $connectionParams['user'], $connectionParams['password'], $connectionParams['options']);
}

public static function getDatabaseName(): string
Expand All @@ -50,7 +50,7 @@ public static function getDatabaseName(): string
throw new \RuntimeException('No connection params given');
}

return $GLOBALS['db_name'];
return getenv('db_name');
}

public static function getDatabaseVendor(): string
Expand All @@ -59,7 +59,7 @@ public static function getDatabaseVendor(): string
throw new \RuntimeException('No connection params given');
}

return $GLOBALS['db_type'];
return getenv('db_type');
}

public static function getConnectionParams(): array
Expand All @@ -73,25 +73,28 @@ public static function getConnectionParams(): array

private static function hasRequiredConnectionParams(): bool
{
$env = getenv();

return isset(
$GLOBALS['db_type'],
$GLOBALS['db_username'],
$GLOBALS['db_password'],
$GLOBALS['db_host'],
$GLOBALS['db_name'],
$GLOBALS['db_port']
$env['db_type'],
$env['db_username'],
$env['db_password'],
$env['db_host'],
$env['db_name'],
$env['db_port']
);
}

private static function getSpecifiedConnectionParams(): array
{
return [
'driver' => $GLOBALS['db_type'],
'user' => $GLOBALS['db_username'],
'password' => $GLOBALS['db_password'],
'host' => $GLOBALS['db_host'],
'dbname' => $GLOBALS['db_name'],
'port' => $GLOBALS['db_port'],
'driver' => getenv('db_type'),
'user' => getenv('db_username'),
'password' => getenv('db_password'),
'host' => getenv('db_host'),
'dbname' => getenv('db_name'),
'port' => getenv('db_port'),
'options' => [PDO::ATTR_ERRMODE => (int) getenv('DB_ATTR_ERRMODE')],
];
}
}

0 comments on commit 788210f

Please sign in to comment.