1+ <?php
2+
3+ /**
4+ * @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
5+ * @author Georges.L (Geolim4) <contact@geolim4.com>
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