Skip to content

Commit

Permalink
Allows URL DSN in Lock and Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jderusse committed Oct 22, 2019
1 parent 3f759d7 commit 3824bae
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
29 changes: 29 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/PdoAdapterTest.php
Expand Up @@ -71,4 +71,33 @@ public function testCleanupExpiredItems()
$this->assertFalse($newItem->isHit());
$this->assertSame(0, $getCacheItemCount(), 'PDOAdapter must clean up expired items');
}

/**
* @dataProvider provideDsn
*/
public function testDsn(string $dsn, string $file = null)
{
try {
$pool = new PdoAdapter($dsn);
$pool->createTable();

$item = $pool->getItem('key');
$item->set('value');
$this->assertTrue($pool->save($item));
} finally {
if (null !== $file) {
@unlink($file);
}
}
}

public function provideDsn()
{
$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
yield ['sqlite://localhost/'.$dbFile, ''.$dbFile];
yield ['sqlite:'.$dbFile, ''.$dbFile];
yield ['sqlite3:///'.$dbFile, ''.$dbFile];
yield ['sqlite://localhost/:memory:'];
yield ['sqlite::memory:'];
}
}
12 changes: 10 additions & 2 deletions src/Symfony/Component/Cache/Traits/PdoTrait.php
Expand Up @@ -13,6 +13,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Schema\Schema;
Expand Down Expand Up @@ -370,8 +371,15 @@ protected function doSave(array $values, $lifetime)
private function getConnection()
{
if (null === $this->conn) {
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
if (strpos($this->dsn, '://')) {
if (!class_exists(DriverManager::class)) {
throw new InvalidArgumentException(sprintf('Fail to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));
}
$this->conn = DriverManager::getConnection(['url' => $this->dsn]);
} else {
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
}
if (null === $this->driver) {
if ($this->conn instanceof \PDO) {
Expand Down
12 changes: 10 additions & 2 deletions src/Symfony/Component/Lock/Store/PdoStore.php
Expand Up @@ -13,6 +13,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Exception\InvalidTtlException;
Expand Down Expand Up @@ -229,8 +230,15 @@ private function getUniqueToken(Key $key): string
private function getConnection()
{
if (null === $this->conn) {
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
if (strpos($this->dsn, '://')) {
if (!class_exists(DriverManager::class)) {
throw new InvalidArgumentException(sprintf('Fail to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));
}
$this->conn = DriverManager::getConnection(['url' => $this->dsn]);
} else {
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
}

return $this->conn;
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/Lock/Tests/Store/PdoStoreTest.php
Expand Up @@ -73,4 +73,34 @@ public function testInvalidTtlConstruct()

return new PdoStore('sqlite:'.self::$dbFile, [], 0.1, 0.1);
}

/**
* @dataProvider provideDsn
*/
public function testDsn(string $dsn, string $file = null)
{
$key = new Key(uniqid(__METHOD__, true));

try {
$store = new PdoStore($dsn);
$store->createTable();

$store->save($key);
$this->assertTrue($store->exists($key));
} finally {
if (null !== $file) {
@unlink($file);
}
}
}

public function provideDsn()
{
$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
yield ['sqlite://localhost/'.$dbFile, ''.$dbFile];
yield ['sqlite:'.$dbFile, ''.$dbFile];
yield ['sqlite3:///'.$dbFile, ''.$dbFile];
yield ['sqlite://localhost/:memory:'];
yield ['sqlite::memory:'];
}
}

0 comments on commit 3824bae

Please sign in to comment.