Skip to content

Commit 8147354

Browse files
authored
Merge pull request #677 from Geolim4/master
Fixed #675
2 parents f50545a + 598ecb8 commit 8147354

File tree

5 files changed

+154
-40
lines changed

5 files changed

+154
-40
lines changed

lib/Phpfastcache/Drivers/Memcache/Config.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@
1717
namespace Phpfastcache\Drivers\Memcache;
1818

1919
use Phpfastcache\Config\ConfigurationOption;
20+
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
2021

2122
class Config extends ConfigurationOption
2223
{
2324
/**
2425
* @var array
26+
*
27+
* Multiple server can be added this way:
28+
* $cfg->setServers([
29+
* [
30+
* 'host' => '127.0.0.1',
31+
* 'port' => 11211,
32+
* 'saslUser' => false,
33+
* 'saslPassword' => false,
34+
* ]
35+
* ]);
2536
*/
26-
protected $servers = [
27-
[
28-
'host' => '127.0.0.1',
29-
'port' => 11211,
30-
'saslUser' => false,
31-
'saslPassword' => false,
32-
],
33-
];
37+
protected $servers = [];
3438

3539
/**
3640
* @var string
@@ -43,12 +47,12 @@ class Config extends ConfigurationOption
4347
protected $port = 11211;
4448

4549
/**
46-
* @var bool
50+
* @var string
4751
*/
4852
protected $saslUser = '';
4953

5054
/**
51-
* @var bool
55+
* @var string
5256
*/
5357
protected $saslPassword = '';
5458

@@ -98,10 +102,25 @@ public function getServers(): array
98102

99103
/**
100104
* @param array $servers
105+
* @throws PhpfastcacheInvalidConfigurationException
101106
* @return self
102107
*/
103108
public function setServers(array $servers): self
104109
{
110+
foreach ($servers as $server) {
111+
if($diff = \array_diff(['host', 'port', 'saslUser', 'saslPassword'], \array_keys($server))){
112+
throw new PhpfastcacheInvalidConfigurationException('Missing keys for memcached server: '. \implode(', ', $diff));
113+
}
114+
if($diff = \array_diff( \array_keys($server), ['host', 'port', 'saslUser', 'saslPassword'])){
115+
throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: '. \implode(', ', $diff));
116+
}
117+
if(!\is_string($server['host'])){
118+
throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array');
119+
}
120+
if(!\is_int($server['port'])){
121+
throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array');
122+
}
123+
}
105124
$this->servers = $servers;
106125
return $this;
107126
}

lib/Phpfastcache/Drivers/Memcached/Config.php

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@
1717
namespace Phpfastcache\Drivers\Memcached;
1818

1919
use Phpfastcache\Config\ConfigurationOption;
20+
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
2021

2122
class Config extends ConfigurationOption
2223
{
2324
/**
2425
* @var array
26+
*
27+
* Multiple server can be added this way:
28+
* $cfg->setServers([
29+
* [
30+
* 'host' => '127.0.0.1',
31+
* 'port' => 11211,
32+
* 'saslUser' => false,
33+
* 'saslPassword' => false,
34+
* ]
35+
* ]);
2536
*/
26-
protected $servers = [
27-
[
28-
'host' => '127.0.0.1',
29-
'port' => 11211,
30-
'saslUser' => false,
31-
'saslPassword' => false,
32-
],
33-
];
37+
protected $servers = [];
3438

3539
/**
3640
* @var string
@@ -43,17 +47,17 @@ class Config extends ConfigurationOption
4347
protected $port = 11211;
4448

4549
/**
46-
* @var bool
50+
* @var string
4751
*/
4852
protected $saslUser = '';
4953

5054
/**
51-
* @var bool
55+
* @var string
5256
*/
5357
protected $saslPassword = '';
5458

5559
/**
56-
* @return bool
60+
* @return string
5761
*/
5862
public function getSaslUser(): string
5963
{
@@ -98,10 +102,25 @@ public function getServers(): array
98102

99103
/**
100104
* @param array $servers
105+
* @throws PhpfastcacheInvalidConfigurationException
101106
* @return self
102107
*/
103108
public function setServers(array $servers): self
104109
{
110+
foreach ($servers as $server) {
111+
if($diff = \array_diff(['host', 'port', 'saslUser', 'saslPassword'], \array_keys($server))){
112+
throw new PhpfastcacheInvalidConfigurationException('Missing keys for memcached server: '. \implode(', ', $diff));
113+
}
114+
if($diff = \array_diff( \array_keys($server), ['host', 'port', 'saslUser', 'saslPassword'])){
115+
throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: '. \implode(', ', $diff));
116+
}
117+
if(!\is_string($server['host'])){
118+
throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array');
119+
}
120+
if(!\is_int($server['port'])){
121+
throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array');
122+
}
123+
}
105124
$this->servers = $servers;
106125
return $this;
107126
}

lib/Phpfastcache/Helper/TestHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function terminateTest()
224224
{
225225
$execTime = \round(\microtime(true) - $this->timestamp, 3);
226226

227-
$this->printText('Test finished in ' . $execTime . 's');
227+
$this->printText('Test duration: ' . $execTime . 's');
228228
exit($this->exitCode);
229229
}
230230

tests/MemcachedAlternativeConfigurationSynax.test.php renamed to tests/MemcachedAlternativeConfigurationSyntax.test.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
*/
77

88
use Phpfastcache\CacheManager;
9-
use Phpfastcache\Helper\TestHelper;
109
use Phpfastcache\Drivers\Memcached\Config as MemcachedConfig;
11-
use Phpfastcache\Drivers\Memcache\Config as MemcacheConfig;
10+
use Phpfastcache\Helper\TestHelper;
1211

1312
chdir(__DIR__);
1413
require_once __DIR__ . '/../vendor/autoload.php';
@@ -17,57 +16,59 @@
1716
$cacheInstanceDefSyntax = CacheManager::getInstance('Memcached');
1817

1918
$cacheInstanceOldSyntax = CacheManager::getInstance('Memcached', new MemcachedConfig([
20-
'servers' => [
21-
[
22-
'host' => '127.0.0.1',
23-
'port' => 11211,
19+
'servers' => [
20+
[
21+
'host' => '127.0.0.1',
22+
'port' => 11211,
23+
'saslUser' => null,
24+
'saslPassword' => null,
25+
]
2426
]
25-
]
2627
]));
2728

2829
$cacheInstanceNewSyntax = CacheManager::getInstance('Memcached', new MemcachedConfig([
29-
'host' => '127.0.0.1',
30-
'port' => 11211,
30+
'host' => '127.0.0.1',
31+
'port' => 11211,
3132
]));
3233

3334
$cacheKey = 'cacheKey';
3435
$RandomCacheValue = str_shuffle(uniqid('pfc', true));
3536

36-
$cacheItem = $cacheInstanceDefSyntax->getItem($cacheKey);
37+
$cacheItem = $cacheInstanceDefSyntax->getItem($cacheKey);
3738
$cacheItem->set($RandomCacheValue)->expiresAfter(600);
3839
$cacheInstanceDefSyntax->save($cacheItem);
3940
unset($cacheItem);
4041
$cacheInstanceDefSyntax->detachAllItems();
4142

4243

43-
$cacheItem = $cacheInstanceOldSyntax->getItem($cacheKey);
44+
$cacheItem = $cacheInstanceOldSyntax->getItem($cacheKey);
4445
$cacheItem->set($RandomCacheValue)->expiresAfter(600);
4546
$cacheInstanceOldSyntax->save($cacheItem);
4647
unset($cacheItem);
4748
$cacheInstanceOldSyntax->detachAllItems();
4849

49-
$cacheItem = $cacheInstanceNewSyntax->getItem($cacheKey);
50+
$cacheItem = $cacheInstanceNewSyntax->getItem($cacheKey);
5051
$cacheItem->set($RandomCacheValue)->expiresAfter(600);
5152
$cacheInstanceNewSyntax->save($cacheItem);
5253
unset($cacheItem);
5354
$cacheInstanceNewSyntax->detachAllItems();
5455

5556

56-
if($cacheInstanceDefSyntax->getItem($cacheKey)->isHit()){
57+
if ($cacheInstanceDefSyntax->getItem($cacheKey)->isHit()) {
5758
$testHelper->printPassText('The default Memcached syntax is working well');
58-
}else{
59+
} else {
5960
$testHelper->printFailText('The default Memcached syntax is not working');
6061
}
6162

62-
if($cacheInstanceOldSyntax->getItem($cacheKey)->isHit()){
63+
if ($cacheInstanceOldSyntax->getItem($cacheKey)->isHit()) {
6364
$testHelper->printPassText('The old Memcached syntax is working well');
64-
}else{
65+
} else {
6566
$testHelper->printFailText('The old Memcached syntax is not working');
6667
}
6768

68-
if($cacheInstanceNewSyntax->getItem($cacheKey)->isHit()){
69+
if ($cacheInstanceNewSyntax->getItem($cacheKey)->isHit()) {
6970
$testHelper->printPassText('The new Memcached syntax is working well');
70-
}else{
71+
} else {
7172
$testHelper->printFailText('The new Memcached syntax is not working');
7273
}
7374

tests/issues/Github-675.test.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/**
4+
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com
5+
* @author Georges.L (Geolim4) <[email protected]>
6+
*/
7+
8+
use Phpfastcache\CacheManager;
9+
use Phpfastcache\Drivers\Memcached\Config;
10+
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
11+
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
12+
use Phpfastcache\Helper\TestHelper;
13+
14+
chdir(__DIR__);
15+
require_once __DIR__ . '/../../vendor/autoload.php';
16+
$testHelper = new TestHelper('Github issue #675 - Memcached ignores custom host/port configurations');
17+
18+
try {
19+
$config = new Config();
20+
$config->setServers([
21+
[
22+
'invalidHostKey' => '120.0.13.37',
23+
'invalidPortKey' => 8080,
24+
'saslUser' => false,
25+
'saslPassword' => false,
26+
]
27+
]);
28+
$testHelper->printFailText('1/4 Memcached config accepted unknown key(s) from $servers array.');
29+
} catch (PhpfastcacheInvalidConfigurationException $e) {
30+
$testHelper->printPassText('1/4 Memcached config detected unknown key(s) from $servers array: ' . $e->getMessage());
31+
}
32+
33+
try {
34+
$config = new Config();
35+
$config->setServers([
36+
[
37+
'host' => '120.0.13.37',
38+
'unwantedKey' => '120.0.13.37',
39+
'unwantedKey2' => '120.0.13.37',
40+
'port' => 8080,
41+
'saslUser' => false,
42+
'saslPassword' => false,
43+
]
44+
]);
45+
$testHelper->printFailText('2/4 Memcached config accepted unwanted key(s) from $servers array.');
46+
} catch (PhpfastcacheInvalidConfigurationException $e) {
47+
$testHelper->printPassText('2/4 Memcached config detected unwanted key(s) from $servers array: ' . $e->getMessage());
48+
}
49+
50+
try {
51+
$config = new Config();
52+
$config->setServers([
53+
[
54+
'host' => '120.0.13.37',
55+
'port' => '8080',
56+
'saslUser' => false,
57+
'saslPassword' => false,
58+
]
59+
]);
60+
$testHelper->printFailText('3/4 Memcached config does not detected invalid types fort host and port');
61+
} catch (PhpfastcacheInvalidConfigurationException $e) {
62+
$testHelper->printPassText('3/4 Memcached config detected invalid types fort host and port: ' . $e->getMessage());
63+
}
64+
65+
try {
66+
$config = new Config();
67+
$config->setHost('255.255.255.255');
68+
$config->setPort(1337);
69+
$cacheInstance = CacheManager::getInstance('Memcached', $config);
70+
$testHelper->printFailText('4/4 Memcached succeeded to connect with invalid host/port specified (used default combination of "$config->servers")');
71+
} catch (PhpfastcacheDriverException $e) {
72+
$testHelper->printPassText('4/4 Memcached failed to connect with invalid host/port specified');
73+
}
74+
75+
$testHelper->terminateTest();

0 commit comments

Comments
 (0)