Skip to content

Commit 357df8b

Browse files
authored
Merge pull request #820 from Geolim4/master
Updating build + upgrading mongodb & predis dependencies
2 parents d9e44f1 + 8a85a14 commit 357df8b

File tree

8 files changed

+126
-111
lines changed

8 files changed

+126
-111
lines changed

.travis.yml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,6 @@ services:
1717
- couchdb
1818
- mongodb
1919

20-
before_script:
21-
- |
22-
if [[ $TRAVIS_PHP_VERSION = "hhv"* ]]; then
23-
cat bin/ci/hhvm_phpfastcache.ini >> /etc/hhvm/php.ini
24-
else
25-
pecl channel-update pecl.php.net;
26-
yes | pecl install -f mongodb-stable;
27-
yes | pecl install -f memcached;
28-
yes | pecl install -f apcu-stable || true;
29-
phpenv config-add bin/ci/php_phpfastcache.ini;
30-
fi
31-
- sleep 15
32-
- mongo pfc_test --eval 'db.createUser({user:"travis",pwd:"test",roles:["readWrite"]});'
33-
3420
php:
3521
- 7.3
3622
- 7.4
@@ -51,6 +37,21 @@ jobs:
5137
php: 7.3
5238
- dist: bionic
5339
php: 7.4
40+
41+
before_install:
42+
- |
43+
if [[ $TRAVIS_PHP_VERSION = "hhv"* ]]; then
44+
cat bin/ci/hhvm_phpfastcache.ini >> /etc/hhvm/php.ini
45+
else
46+
pecl channel-update pecl.php.net;
47+
yes | pecl install -f mongodb-stable;
48+
yes | pecl install -f memcached;
49+
yes | pecl install -f apcu-stable || true;
50+
phpenv config-add bin/ci/php_phpfastcache.ini;
51+
fi
52+
- sleep 15
53+
- mongo pfc_test --eval 'db.createUser({user:"travis",pwd:"test",roles:["readWrite"]});'
54+
5455
install:
5556
- ./bin/ci/install_dependencies.sh
5657

bin/ci/install_dependencies.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
22

33
composer self-update;
4-
composer install --ignore-platform-reqs;
5-
composer require "phpfastcache/couchdb:~1.0.0" "phpfastcache/phpssdb:~1.0.0" "predis/predis:~1.1.0" "mongodb/mongodb:^1.1" --ignore-platform-reqs;
4+
composer install;
5+
composer require "phpfastcache/couchdb:~1.0.0" "phpfastcache/phpssdb:~1.0.0" "predis/predis:~1.1.0" "mongodb/mongodb:^1.9";

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
"ext-wincache": "*",
4444
"ext-leveldb": "*",
4545
"ext-couchbase": "*",
46-
"predis/predis": "~1.1.0",
47-
"mongodb/mongodb": "^1.1",
46+
"predis/predis": "^1.1",
47+
"mongodb/mongodb": "^1.9",
4848
"phpfastcache/phpssdb": "~1.0.0",
4949
"phpfastcache/couchdb": "~1.0.0"
5050
},

lib/Phpfastcache/Drivers/Couchdb/Driver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function getStats(): DriverStatistic
6666
$info = $this->instance->getDatabaseInfo();
6767

6868
return (new DriverStatistic())
69-
->setSize($info['sizes']['active'])
69+
->setSize($info['sizes']['active'] ?? 0)
7070
->setRawData($info)
7171
->setData(implode(', ', array_keys($this->itemInstances)))
7272
->setInfo('Couchdb version ' . $this->instance->getVersion() . "\n For more information see RawData.");

lib/Phpfastcache/Drivers/Mongodb/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Config extends ConfigurationOption
5353
/**
5454
* @var string
5555
*/
56-
protected $collectionName = 'Cache';
56+
protected $collectionName = 'phpfastcache';
5757

5858
/**
5959
* @var string
@@ -273,4 +273,4 @@ public function setProtocol(string $protocol): self
273273
$this->protocol = $protocol;
274274
return $this;
275275
}
276-
}
276+
}

lib/Phpfastcache/Drivers/Mongodb/Driver.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace Phpfastcache\Drivers\Mongodb;
1919

2020
use DateTime;
21+
use Exception;
2122
use LogicException;
2223
use MongoClient;
2324
use MongoDB\{BSON\Binary, BSON\UTCDateTime, Client, Collection, Database, DeleteResult, Driver\Command, Driver\Exception\Exception as MongoDBException, Driver\Manager};
@@ -75,7 +76,7 @@ public function driverCheck(): bool
7576
public function getStats(): DriverStatistic
7677
{
7778
$serverStats = $this->instance->getManager()->executeCommand(
78-
'phpFastCache',
79+
$this->getConfig()->getDatabaseName(),
7980
new Command(
8081
[
8182
'serverStatus' => 1,
@@ -87,10 +88,10 @@ public function getStats(): DriverStatistic
8788
)->toArray()[0];
8889

8990
$collectionStats = $this->instance->getManager()->executeCommand(
90-
'phpFastCache',
91+
$this->getConfig()->getDatabaseName(),
9192
new Command(
9293
[
93-
'collStats' => (isset($this->getConfig()['collectionName']) ? $this->getConfig()['collectionName'] : 'Cache'),
94+
'collStats' => $this->getConfig()->getCollectionName(),
9495
'verbose' => true,
9596
]
9697
)
@@ -146,25 +147,19 @@ public function getStats(): DriverStatistic
146147
*/
147148
protected function driverRead(CacheItemInterface $item)
148149
{
149-
$document = $this->getCollection()->findOne(['_id' => $item->getEncodedKey()]);
150+
$document = $this->getCollection()->findOne(['_id' => $this->getMongoDbItemKey($item)]);
150151

151152
if ($document) {
152153
$return = [
153154
self::DRIVER_DATA_WRAPPER_INDEX => $this->decode($document[self::DRIVER_DATA_WRAPPER_INDEX]->getData()),
154155
self::DRIVER_TAGS_WRAPPER_INDEX => $this->decode($document[self::DRIVER_TAGS_WRAPPER_INDEX]->getData()),
155-
self::DRIVER_EDATE_WRAPPER_INDEX => (new DateTime())->setTimestamp($document[self::DRIVER_EDATE_WRAPPER_INDEX]->toDateTime()->getTimestamp()),
156+
self::DRIVER_EDATE_WRAPPER_INDEX => $document[self::DRIVER_EDATE_WRAPPER_INDEX]->toDateTime(),
156157
];
157158

158159
if (!empty($this->getConfig()->isItemDetailedDate())) {
159160
$return += [
160-
self::DRIVER_MDATE_WRAPPER_INDEX => (new DateTime())->setTimestamp(
161-
$document[self::DRIVER_MDATE_WRAPPER_INDEX]->toDateTime()
162-
->getTimestamp()
163-
),
164-
self::DRIVER_CDATE_WRAPPER_INDEX => (new DateTime())->setTimestamp(
165-
$document[self::DRIVER_CDATE_WRAPPER_INDEX]->toDateTime()
166-
->getTimestamp()
167-
),
161+
self::DRIVER_MDATE_WRAPPER_INDEX => $document[self::DRIVER_MDATE_WRAPPER_INDEX]->toDateTime(),
162+
self::DRIVER_CDATE_WRAPPER_INDEX => $document[self::DRIVER_CDATE_WRAPPER_INDEX]->toDateTime(),
168163
];
169164
}
170165

@@ -196,6 +191,7 @@ protected function driverWrite(CacheItemInterface $item): bool
196191
if ($item instanceof Item) {
197192
try {
198193
$set = [
194+
self::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(),
199195
self::DRIVER_DATA_WRAPPER_INDEX => new Binary($this->encode($item->get()), Binary::TYPE_GENERIC),
200196
self::DRIVER_TAGS_WRAPPER_INDEX => new Binary($this->encode($item->getTags()), Binary::TYPE_GENERIC),
201197
self::DRIVER_EDATE_WRAPPER_INDEX => ($item->getTtl() > 0 ? new UTCDateTime((time() + $item->getTtl()) * 1000) : new UTCDateTime(time() * 1000)),
@@ -214,7 +210,7 @@ protected function driverWrite(CacheItemInterface $item): bool
214210
];
215211
}
216212
$result = (array)$this->getCollection()->updateOne(
217-
['_id' => $item->getEncodedKey()],
213+
['_id' => $this->getMongoDbItemKey($item)],
218214
[
219215
'$set' => $set,
220216
],
@@ -244,7 +240,7 @@ protected function driverDelete(CacheItemInterface $item): bool
244240
/**
245241
* @var DeleteResult $deletionResult
246242
*/
247-
$deletionResult = $this->getCollection()->deleteOne(['_id' => $item->getEncodedKey()]);
243+
$deletionResult = $this->getCollection()->deleteOne(['_id' => $this->getMongoDbItemKey($item)]);
248244

249245
return $deletionResult->isAcknowledged();
250246
}
@@ -257,7 +253,11 @@ protected function driverDelete(CacheItemInterface $item): bool
257253
*/
258254
protected function driverClear(): bool
259255
{
260-
return $this->collection->deleteMany([])->isAcknowledged();
256+
try {
257+
return $this->collection->deleteMany([])->isAcknowledged();
258+
} catch (MongoDBException $e) {
259+
throw new PhpfastcacheDriverException('Got error while trying to empty the collection: ' . $e->getMessage(), 0, $e);
260+
}
261261
}
262262

263263
/**
@@ -294,8 +294,9 @@ protected function driverConnect(): bool
294294
* @param string $databaseName
295295
* @return string The connection URI.
296296
*/
297-
protected function buildConnectionURI($databaseName = ''): string
297+
protected function buildConnectionURI(string $databaseName): string
298298
{
299+
$databaseName = \urlencode($databaseName);
299300
$servers = $this->getConfig()->getServers();
300301
$options = $this->getConfig()->getOptions();
301302

@@ -332,6 +333,11 @@ static function ($carry, $data) {
332333
);
333334
}
334335

336+
protected function getMongoDbItemKey(CacheItemInterface $item)
337+
{
338+
return 'pfc_' . $item->getEncodedKey();
339+
}
340+
335341
/********************
336342
*
337343
* PSR-6 Extended Methods

tests/Mongodb.test.php

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,78 +8,24 @@
88
use Phpfastcache\CacheManager;
99
use Phpfastcache\Drivers\Mongodb\Config;
1010
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
11-
use Phpfastcache\Exceptions\phpFastCacheDriverException;
1211
use Phpfastcache\Tests\Helper\TestHelper;
1312

1413
chdir(__DIR__);
1514
require_once __DIR__ . '/../vendor/autoload.php';
1615
$testHelper = new TestHelper('Mongodb driver');
16+
$config = new Config();
17+
$config->setItemDetailedDate(true)
18+
->setDatabaseName('pfc_test')
19+
->setCollectionName('pfc_' . str_pad('0', 3, random_int(1, 100)))
20+
->setUsername('travis')
21+
->setPassword('test');
1722

1823
try{
19-
$cacheInstance = CacheManager::getInstance('Mongodb', new Config([
20-
'databaseName' => 'pfc_test',
21-
'username' => 'travis',
22-
'password' => 'test',
23-
]));
24+
$cacheInstance = CacheManager::getInstance('Mongodb', $config);
2425
}catch(PhpfastcacheDriverCheckException $exception){
2526
$testHelper->exceptionHandler($exception);
2627
$testHelper->terminateTest();
2728
}
2829

29-
30-
$cacheKey = str_shuffle(uniqid('pfc', true));
31-
$cacheValue = str_shuffle(uniqid('pfc', true));
32-
33-
try{
34-
$item = $cacheInstance->getItem($cacheKey);
35-
$item->set($cacheValue)->expiresAfter(300);
36-
$cacheInstance->save($item);
37-
$testHelper->assertPass('Successfully saved a new cache item into Mongodb server');
38-
}catch(phpFastCacheDriverException $e){
39-
$testHelper->assertFail('Failed to save a new cache item into Mongodb server with exception: ' . $e->getMessage());
40-
}
41-
42-
try{
43-
unset($item);
44-
$cacheInstance->detachAllItems();
45-
$item = $cacheInstance->getItem($cacheKey);
46-
47-
if($item->get() === $cacheValue){
48-
$testHelper->assertPass('Getter returned expected value: ' . $cacheValue);
49-
}else{
50-
$testHelper->assertFail('Getter returned unexpected value, expecting "' . $cacheValue . '", got "' . $item->get() . '"');
51-
}
52-
}catch(phpFastCacheDriverException $e){
53-
$testHelper->assertFail('Failed to save a new cache item into Mongodb server with exception: ' . $e->getMessage());
54-
}
55-
56-
try{
57-
unset($item);
58-
$cacheInstance->detachAllItems();
59-
$cacheInstance->clear();
60-
$item = $cacheInstance->getItem($cacheKey);
61-
62-
if(!$item->isHit()){
63-
$testHelper->assertPass('Successfully cleared the Mongodb server, no cache item found');
64-
}else{
65-
$testHelper->assertFail('Failed to clear the Mongodb server, a cache item has been found');
66-
}
67-
}catch(phpFastCacheDriverException $e){
68-
$testHelper->assertFail('Failed to clear the Mongodb server with exception: ' . $e->getMessage());
69-
}
70-
71-
try{
72-
$item = $cacheInstance->getItem($cacheKey);
73-
$item->set($cacheValue)->expiresAfter(300);
74-
$cacheInstance->save($item);
75-
76-
if($cacheInstance->deleteItem($item->getKey())){
77-
$testHelper->assertPass('Deleter successfully removed the item from cache');
78-
}else{
79-
$testHelper->assertFail('Deleter failed to remove the item from cache');
80-
}
81-
}catch(phpFastCacheDriverException $e){
82-
$testHelper->assertFail('Failed to remove a cache item from Mongodb server with exception: ' . $e->getMessage());
83-
}
84-
30+
$testHelper->runCRUDTests($cacheInstance);
8531
$testHelper->terminateTest();

0 commit comments

Comments
 (0)