[ticket/16223] Ensure memcached tests are only run when available

PHPBB3-16223
This commit is contained in:
Marc Alexander 2019-11-25 21:27:43 +01:00
parent fd7524858c
commit c7d47e34e8
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
4 changed files with 77 additions and 4 deletions

View file

@ -56,6 +56,15 @@ class memcached extends \phpbb\cache\driver\memory
// Call the parent constructor
parent::__construct();
$memcached_servers = PHPBB_ACM_MEMCACHED;
// Allow overwriting PHPBB_ACM_MEMCACHED via constructor arguments
$args = func_get_args();
if (count($args) >= 2)
{
$memcached_servers = $args[0] . '/' . $args[1];
}
$this->memcached = new \Memcached();
$this->memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
// Memcached defaults to using compression, disable if we don't want
@ -65,10 +74,20 @@ class memcached extends \phpbb\cache\driver\memory
$this->memcached->setOption(\Memcached::OPT_COMPRESSION, false);
}
foreach (explode(',', PHPBB_ACM_MEMCACHED) as $u)
$server_list = [];
foreach (explode(',', $memcached_servers) as $u)
{
preg_match('#(.*)/(\d+)#', $u, $parts);
$this->memcached->addServer(trim($parts[1]), (int) trim($parts[2]));
if (preg_match('#(.*)/(\d+)#', $u, $parts))
{
$server_list[] = [trim($parts[1]), (int) trim($parts[2])];
}
}
$this->memcached->addServers($server_list);
if (empty($server_list) || empty($this->memcached->getStats()))
{
trigger_error('Could not connect to memcached server(s).');
}
}

View file

@ -109,6 +109,21 @@ Or via environment variables as follows:
$ PHPBB_TEST_REDIS_HOST=localhost PHPBB_TEST_REDIS_PORT=6379 phpunit
Memcached
---------
In order to run tests for the memcached cache driver, at least one of memcached
host or port must be specified in the test configuration. This can be done via
test_config.php as follows:
<?php
$phpbb_memcached_host = 'localhost';
$phpbb_memcached_port = '11211';
Or via environment variables as follows:
$ PHPBB_TEST_MEMCACHED_HOST=localhost PHPBB_TEST_MEMCACHED_PORT=11211 phpunit
Running
=======

View file

@ -29,6 +29,25 @@ class phpbb_cache_memcached_driver_test extends \phpbb_cache_common_test_case
self::markTestSkipped('memcached extension is not loaded');
}
$config = phpbb_test_case_helpers::get_test_config();
if (isset($config['memcached_host']) || isset($config['memcached_port']))
{
$host = isset($config['memcached_host']) ? $config['memcached_host'] : 'localhost';
$port = isset($config['memcached_port']) ? $config['memcached_port'] : 11211;
self::$config = array('host' => $host, 'port' => $port);
}
else
{
self::markTestSkipped('Test memcached host/port is not specified');
}
$memcached = new \Memcached();
$memcached->addServer(self::$config['host'], self::$config['port']);
if (empty($memcached->getStats()))
{
self::markTestSkipped('Test memcached server is not available');
}
parent::setUpBeforeClass();
}
@ -40,7 +59,7 @@ class phpbb_cache_memcached_driver_test extends \phpbb_cache_common_test_case
$phpbb_container = new phpbb_mock_container_builder();
$phpbb_container->setParameter('core.cache_dir', $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/');
$this->driver = new \phpbb\cache\driver\memcached();
$this->driver = new \phpbb\cache\driver\memcached(self::$config['host'], self::$config['port']);
$this->driver->purge();
}
}

View file

@ -173,6 +173,16 @@ class phpbb_test_case_helpers
{
$config['fulltext_sphinx_id'] = $fulltext_sphinx_id;
}
if (isset($phpbb_memcached_host))
{
$config['memcached_host'] = $phpbb_memcached_host;
}
if (isset($phpbb_memcached_port))
{
$config['memcached_port'] = $phpbb_memcached_port;
}
}
if (isset($_SERVER['PHPBB_TEST_DBMS']))
@ -205,6 +215,16 @@ class phpbb_test_case_helpers
$config['redis_port'] = $_SERVER['PHPBB_TEST_REDIS_PORT'];
}
if (isset($_SERVER['PHPBB_TEST_MEMCACHED_HOST']))
{
$config['memcached_host'] = $_SERVER['PHPBB_TEST_MEMCACHED_HOST'];
}
if (isset($_SERVER['PHPBB_TEST_MEMCACHED_PORT']))
{
$config['memcached_port'] = $_SERVER['PHPBB_TEST_MEMCACHED_PORT'];
}
return $config;
}