Skip to content

Commit f23b737

Browse files
committed
Fixed Couchdb tests & driver
1 parent 9b10a3d commit f23b737

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

lib/Phpfastcache/Drivers/Couchdb/Config.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@ class Config extends ConfigurationOption
2424
* @var string
2525
*/
2626
protected $host = '127.0.0.1';
27+
2728
/**
2829
* @var int
2930
*/
3031
protected $port = 5984;
3132

32-
/**
33-
* @var string
34-
*/
35-
protected $path = '/';
36-
3733
/**
3834
* @var string
3935
*/
@@ -181,4 +177,4 @@ public function setTimeout(int $timeout): self
181177
$this->timeout = $timeout;
182178
return $this;
183179
}
184-
}
180+
}

lib/Phpfastcache/Drivers/Couchdb/Driver.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected function driverConnect(): bool
9494
}
9595
$url .= $clientConfig->getHost();
9696
$url .= ":{$clientConfig->getPort()}";
97-
$url .= $clientConfig->getPath();
97+
$url .= '/' . $this->getDatabaseName();
9898

9999
$this->instance = CouchDBClient::create(
100100
[
@@ -122,11 +122,16 @@ protected function getDatabaseName(): string
122122
*/
123123
protected function createDatabase()
124124
{
125-
if (!in_array($this->instance->getDatabase(), $this->instance->getAllDatabases(), true)) {
126-
$this->instance->createDatabase($this->instance->getDatabase());
125+
if (!in_array($this->getDatabaseName(), $this->instance->getAllDatabases(), true)) {
126+
$this->instance->createDatabase($this->getDatabaseName());
127127
}
128128
}
129129

130+
protected function getCouchDbItemKey(CacheItemInterface $item)
131+
{
132+
return 'pfc_' . $item->getEncodedKey();
133+
}
134+
130135
/**
131136
* @param CacheItemInterface $item
132137
* @return null|array
@@ -135,7 +140,7 @@ protected function createDatabase()
135140
protected function driverRead(CacheItemInterface $item)
136141
{
137142
try {
138-
$response = $this->instance->findDocument($item->getEncodedKey());
143+
$response = $this->instance->findDocument($this->getCouchDbItemKey($item));
139144
} catch (CouchDBException $e) {
140145
throw new PhpfastcacheDriverException('Got error while trying to get a document: ' . $e->getMessage(), 0, $e);
141146
}
@@ -166,8 +171,8 @@ protected function driverWrite(CacheItemInterface $item): bool
166171
try {
167172
$this->instance->putDocument(
168173
['data' => $this->encode($this->driverPreWrap($item))],
169-
$item->getEncodedKey(),
170-
$this->getLatestDocumentRevision($item->getEncodedKey())
174+
$this->getCouchDbItemKey($item),
175+
$this->getLatestDocumentRevision($this->getCouchDbItemKey($item))
171176
);
172177
} catch (CouchDBException $e) {
173178
throw new PhpfastcacheDriverException('Got error while trying to upsert a document: ' . $e->getMessage(), 0, $e);
@@ -217,7 +222,7 @@ protected function driverDelete(CacheItemInterface $item): bool
217222
*/
218223
if ($item instanceof Item) {
219224
try {
220-
$this->instance->deleteDocument($item->getEncodedKey(), $this->getLatestDocumentRevision($item->getEncodedKey()));
225+
$this->instance->deleteDocument($this->getCouchDbItemKey($item), $this->getLatestDocumentRevision($this->getCouchDbItemKey($item)));
221226
} catch (CouchDBException $e) {
222227
throw new PhpfastcacheDriverException('Got error while trying to delete a document: ' . $e->getMessage(), 0, $e);
223228
}

tests/Couchdb.test.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717
$config = new CouchdbConfig();
1818

1919
try{
20+
$config->setUsername('admin');
21+
$config->setPassword('travis');
2022
$cacheInstance = CacheManager::getInstance('Couchdb', $config);
2123
} catch (PhpfastcacheDriverConnectException $e){
2224
$config->setUsername('admin');
2325
$config->setPassword('travis');
2426
$cacheInstance = CacheManager::getInstance('Couchdb', $config);
27+
} catch(PhpfastcacheDriverConnectException $e){
28+
$testHelper->printSkipText('Couchdb server unavailable: ' . $e->getMessage());
29+
$testHelper->terminateTest();
2530
}
2631

2732
$cacheKey = str_shuffle(uniqid('pfc', true));

0 commit comments

Comments
 (0)