From 7129531bc0884a710e5d61ced2e0a54538f64266 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Fri, 7 Jan 2011 22:45:39 +0100 Subject: [PATCH 1/4] [ticket/9981] Fix unit test dependencies PHPBB3-9981 --- tests/request/request_var.php | 1 + tests/template/template.php | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/request/request_var.php b/tests/request/request_var.php index 64cdd9bc75..e452aefea9 100644 --- a/tests/request/request_var.php +++ b/tests/request/request_var.php @@ -13,6 +13,7 @@ require_once __DIR__ . '/../../phpBB/includes/request/deactivated_super_global.p require_once __DIR__ . '/../../phpBB/includes/request/interface.php'; require_once __DIR__ . '/../../phpBB/includes/request/request.php'; require_once __DIR__ . '/../../phpBB/includes/functions.php'; +require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php'; class phpbb_request_var_test extends phpbb_test_case { diff --git a/tests/template/template.php b/tests/template/template.php index a58a0a4e0f..14a5a3c752 100644 --- a/tests/template/template.php +++ b/tests/template/template.php @@ -7,6 +7,7 @@ * */ +require_once __DIR__ . '/../../phpBB/includes/functions.php'; require_once __DIR__ . '/../../phpBB/includes/template.php'; class phpbb_template_template_test extends phpbb_test_case From 36e95f939db9b88b8519d956120d161102184ccb Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Fri, 7 Jan 2011 18:03:00 +0100 Subject: [PATCH 2/4] [ticket/9979] Support autoloading in unit tests PHPBB-9979 --- tests/bootstrap.php | 4 ++++ tests/class_loader/class_loader_test.php | 12 ++++++++++++ tests/request/deactivated_super_global.php | 3 --- tests/request/request.php | 5 ----- tests/request/request_var.php | 5 ----- tests/request/type_cast_helper.php | 2 -- tests/security/redirect.php | 1 - 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 99f145e427..161bd83a3d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -26,6 +26,10 @@ else } require_once $phpbb_root_path . 'includes/constants.php'; +require_once $phpbb_root_path . 'includes/class_loader.' . $phpEx; + +$class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx); +$class_loader->register(); require_once 'test_framework/phpbb_test_case_helpers.php'; require_once 'test_framework/phpbb_test_case.php'; diff --git a/tests/class_loader/class_loader_test.php b/tests/class_loader/class_loader_test.php index aef4f1de07..3eb3c915a1 100644 --- a/tests/class_loader/class_loader_test.php +++ b/tests/class_loader/class_loader_test.php @@ -14,6 +14,18 @@ require_once __DIR__ . '/../../phpBB/includes/class_loader.php'; class phpbb_class_loader_test extends PHPUnit_Framework_TestCase { + public function setUp() + { + global $class_loader; + $class_loader->unregister(); + } + + public function tearDown() + { + global $class_loader; + $class_loader->register(); + } + public function test_resolve_path() { $prefix = __DIR__ . '/'; diff --git a/tests/request/deactivated_super_global.php b/tests/request/deactivated_super_global.php index ea385831c9..995f93443d 100644 --- a/tests/request/deactivated_super_global.php +++ b/tests/request/deactivated_super_global.php @@ -8,9 +8,6 @@ * */ -require_once __DIR__ . '/../../phpBB/includes/request/interface.php'; -require_once __DIR__ . '/../../phpBB/includes/request/deactivated_super_global.php'; - class phpbb_deactivated_super_global_test extends phpbb_test_case { /** diff --git a/tests/request/request.php b/tests/request/request.php index 7cec70b0d4..203c9fd880 100644 --- a/tests/request/request.php +++ b/tests/request/request.php @@ -8,11 +8,6 @@ * */ -require_once __DIR__ . '/../../phpBB/includes/request/type_cast_helper_interface.php'; -require_once __DIR__ . '/../../phpBB/includes/request/interface.php'; -require_once __DIR__ . '/../../phpBB/includes/request/deactivated_super_global.php'; -require_once __DIR__ . '/../../phpBB/includes/request/request.php'; - class phpbb_request_test extends phpbb_test_case { private $type_cast_helper; diff --git a/tests/request/request_var.php b/tests/request/request_var.php index e452aefea9..8848e388bf 100644 --- a/tests/request/request_var.php +++ b/tests/request/request_var.php @@ -7,11 +7,6 @@ * */ -require_once __DIR__ . '/../../phpBB/includes/request/type_cast_helper_interface.php'; -require_once __DIR__ . '/../../phpBB/includes/request/type_cast_helper.php'; -require_once __DIR__ . '/../../phpBB/includes/request/deactivated_super_global.php'; -require_once __DIR__ . '/../../phpBB/includes/request/interface.php'; -require_once __DIR__ . '/../../phpBB/includes/request/request.php'; require_once __DIR__ . '/../../phpBB/includes/functions.php'; require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php'; diff --git a/tests/request/type_cast_helper.php b/tests/request/type_cast_helper.php index 3f27269acb..6d4e7055d5 100644 --- a/tests/request/type_cast_helper.php +++ b/tests/request/type_cast_helper.php @@ -9,8 +9,6 @@ */ require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php'; -require_once __DIR__ . '/../../phpBB/includes/request/type_cast_helper_interface.php'; -require_once __DIR__ . '/../../phpBB/includes/request/type_cast_helper.php'; class phpbb_type_cast_helper_test extends phpbb_test_case { diff --git a/tests/security/redirect.php b/tests/security/redirect.php index c53414e7df..1557384507 100644 --- a/tests/security/redirect.php +++ b/tests/security/redirect.php @@ -8,7 +8,6 @@ */ require_once __DIR__ . '/base.php'; - require_once __DIR__ . '/../../phpBB/includes/functions.php'; require_once __DIR__ . '/../../phpBB/includes/session.php'; From 9329b16ab13f3a4caf107df358c3c58bda2dcd8a Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Wed, 3 Nov 2010 18:35:31 +0100 Subject: [PATCH 3/4] [task/acm-refactor] Refactor the ACM classes to have a common interface. They are now refered to as cache drivers rather than ACM classes. The additional utility functions from the original cache class have been moved to the cache_service. The class loader is now instantiated without a cache instance and passed one as soon as it is constructed to allow autoloading the cache classes. PHPBB3-9983 --- phpBB/common.php | 12 +- phpBB/docs/coding-guidelines.html | 2 +- phpBB/download/file.php | 12 +- .../{acm/acm_apc.php => cache/driver/apc.php} | 8 +- phpBB/includes/cache/driver/base.php | 24 ++++ .../driver/eaccelerator.php} | 8 +- .../acm_file.php => cache/driver/file.php} | 6 +- phpBB/includes/cache/driver/interface.php | 104 ++++++++++++++++++ .../driver/memcache.php} | 12 +- .../driver/memory.php} | 4 +- .../acm_null.php => cache/driver/null.php} | 4 +- .../driver/xcache.php} | 14 +-- phpBB/includes/cache/factory.php | 52 +++++++++ .../includes/{cache.php => cache/service.php} | 57 ++++++---- phpBB/includes/class_loader.php | 21 ++-- phpBB/install/database_update.php | 11 +- phpBB/install/index.php | 7 +- phpBB/style.php | 15 ++- tests/bootstrap.php | 2 +- tests/cache/all_tests.php | 40 +++++++ tests/cache/cache_test.php | 39 +++++++ tests/class_loader/cache_mock.php | 49 ++++++++- tests/class_loader/class_loader_test.php | 14 ++- 23 files changed, 415 insertions(+), 102 deletions(-) rename phpBB/includes/{acm/acm_apc.php => cache/driver/apc.php} (88%) create mode 100644 phpBB/includes/cache/driver/base.php rename phpBB/includes/{acm/acm_eaccelerator.php => cache/driver/eaccelerator.php} (92%) rename phpBB/includes/{acm/acm_file.php => cache/driver/file.php} (98%) create mode 100644 phpBB/includes/cache/driver/interface.php rename phpBB/includes/{acm/acm_memcache.php => cache/driver/memcache.php} (92%) rename phpBB/includes/{acm/acm_memory.php => cache/driver/memory.php} (98%) rename phpBB/includes/{acm/acm_null.php => cache/driver/null.php} (95%) rename phpBB/includes/{acm/acm_xcache.php => cache/driver/xcache.php} (91%) create mode 100644 phpBB/includes/cache/factory.php rename phpBB/includes/{cache.php => cache/service.php} (85%) create mode 100644 tests/cache/all_tests.php create mode 100644 tests/cache/cache_test.php diff --git a/phpBB/common.php b/phpBB/common.php index 96b782a229..fc6009eb21 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -187,8 +187,6 @@ if (!empty($load_extensions) && function_exists('dl')) // Include files require($phpbb_root_path . 'includes/class_loader.' . $phpEx); -require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx); -require($phpbb_root_path . 'includes/cache.' . $phpEx); require($phpbb_root_path . 'includes/template.' . $phpEx); require($phpbb_root_path . 'includes/session.' . $phpEx); require($phpbb_root_path . 'includes/auth.' . $phpEx); @@ -203,13 +201,15 @@ require($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx); // Set PHP error handler to ours set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler'); -// Cache must be loaded before class loader -$cache = new cache(); - // Setup class loader first -$class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx, $cache); +$class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx); $class_loader->register(); +// set up caching +$acm = phpbb_cache_factory::create($acm_type)->get_acm(); +$class_loader->set_acm($acm); +$cache = new phpbb_cache_service($acm); + // Instantiate some basic classes $request = new phpbb_request(); $user = new user(); diff --git a/phpBB/docs/coding-guidelines.html b/phpBB/docs/coding-guidelines.html index 766242ab83..c0ebb7450e 100644 --- a/phpBB/docs/coding-guidelines.html +++ b/phpBB/docs/coding-guidelines.html @@ -203,7 +203,7 @@ class ...
  • phpBB3
    Core files and all files not assigned to a separate package
  • -
  • acm
    /includes/acm, /includes/cache.php
    Cache System
  • +
  • acm
    /includes/cache, /includes/cache.php
    Cache System
  • acp
    /adm, /includes/acp, /includes/functions_admin.php
    Administration Control Panel
  • dbal
    /includes/db
    Database Abstraction Layer.
    Base class is dbal
      diff --git a/phpBB/download/file.php b/phpBB/download/file.php index 46ddbb9916..74925fb447 100644 --- a/phpBB/download/file.php +++ b/phpBB/download/file.php @@ -44,15 +44,21 @@ if (isset($_GET['avatar'])) exit; } - require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx); - require($phpbb_root_path . 'includes/cache.' . $phpEx); + require($phpbb_root_path . 'includes/class_loader.' . $phpEx); require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx); require($phpbb_root_path . 'includes/constants.' . $phpEx); require($phpbb_root_path . 'includes/functions.' . $phpEx); require($phpbb_root_path . 'includes/functions_download' . '.' . $phpEx); + $class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx); + $class_loader->register(); + + // set up caching + $acm = phpbb_cache_factory::create($acm_type)->get_acm(); + $class_loader->set_acm($acm); + $cache = new phpbb_cache_service($acm); + $db = new $sql_db(); - $cache = new cache(); // Connect to DB if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false)) diff --git a/phpBB/includes/acm/acm_apc.php b/phpBB/includes/cache/driver/apc.php similarity index 88% rename from phpBB/includes/acm/acm_apc.php rename to phpBB/includes/cache/driver/apc.php index ab00b43e60..a97cbe4dd1 100644 --- a/phpBB/includes/acm/acm_apc.php +++ b/phpBB/includes/cache/driver/apc.php @@ -16,17 +16,11 @@ if (!defined('IN_PHPBB')) exit; } -// Include the abstract base -if (!class_exists('acm_memory')) -{ - require("{$phpbb_root_path}includes/acm/acm_memory.$phpEx"); -} - /** * ACM for APC * @package acm */ -class acm extends acm_memory +class phpbb_cache_driver_apc extends phpbb_cache_driver_memory { var $extension = 'apc'; diff --git a/phpBB/includes/cache/driver/base.php b/phpBB/includes/cache/driver/base.php new file mode 100644 index 0000000000..a71eca45d7 --- /dev/null +++ b/phpBB/includes/cache/driver/base.php @@ -0,0 +1,24 @@ +cache_dir = $phpbb_root_path . 'cache/'; + $this->cache_dir = !is_null($cache_dir) ? $cache_dir : $phpbb_root_path . 'cache/'; } /** diff --git a/phpBB/includes/cache/driver/interface.php b/phpBB/includes/cache/driver/interface.php new file mode 100644 index 0000000000..91d364abf6 --- /dev/null +++ b/phpBB/includes/cache/driver/interface.php @@ -0,0 +1,104 @@ +memcache = new Memcache; foreach(explode(',', PHPBB_ACM_MEMCACHE) as $u) diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/cache/driver/memory.php similarity index 98% rename from phpBB/includes/acm/acm_memory.php rename to phpBB/includes/cache/driver/memory.php index d248487846..633a0fe699 100644 --- a/phpBB/includes/acm/acm_memory.php +++ b/phpBB/includes/cache/driver/memory.php @@ -20,7 +20,7 @@ if (!defined('IN_PHPBB')) * ACM Abstract Memory Class * @package acm */ -class acm_memory +class phpbb_cache_driver_memory extends phpbb_cache_driver_base { var $key_prefix; @@ -34,7 +34,7 @@ class acm_memory /** * Set cache path */ - function acm_memory() + function __construct() { global $phpbb_root_path, $dbname, $table_prefix; diff --git a/phpBB/includes/acm/acm_null.php b/phpBB/includes/cache/driver/null.php similarity index 95% rename from phpBB/includes/acm/acm_null.php rename to phpBB/includes/cache/driver/null.php index 79f2b59c0f..0a520b572e 100644 --- a/phpBB/includes/acm/acm_null.php +++ b/phpBB/includes/cache/driver/null.php @@ -20,12 +20,12 @@ if (!defined('IN_PHPBB')) * ACM Null Caching * @package acm */ -class acm +class phpbb_cache_driver_null extends phpbb_cache_driver_base { /** * Set cache path */ - function acm() + function __construct() { } diff --git a/phpBB/includes/acm/acm_xcache.php b/phpBB/includes/cache/driver/xcache.php similarity index 91% rename from phpBB/includes/acm/acm_xcache.php rename to phpBB/includes/cache/driver/xcache.php index 8be0ad0c48..9363ff501d 100644 --- a/phpBB/includes/acm/acm_xcache.php +++ b/phpBB/includes/cache/driver/xcache.php @@ -16,12 +16,6 @@ if (!defined('IN_PHPBB')) exit; } -// Include the abstract base -if (!class_exists('acm_memory')) -{ - require("{$phpbb_root_path}includes/acm/acm_memory.$phpEx"); -} - /** * ACM for XCache * @package acm @@ -31,13 +25,13 @@ if (!class_exists('acm_memory')) * - xcache.admin.enable_auth = off (or xcache.admin.user and xcache.admin.password set) * */ -class acm extends acm_memory +class phpbb_cache_driver_xcache extends phpbb_cache_driver_memory { var $extension = 'XCache'; - function acm() + function __construct() { - parent::acm_memory(); + parent::__construct(); if (!function_exists('ini_get') || (int) ini_get('xcache.var_size') <= 0) { @@ -111,7 +105,7 @@ class acm extends acm_memory * @access protected * @param string $var Cache key * @return bool True if it exists, otherwise false - */ + */ function _isset($var) { return xcache_isset($this->key_prefix . $var); diff --git a/phpBB/includes/cache/factory.php b/phpBB/includes/cache/factory.php new file mode 100644 index 0000000000..cc88780bf2 --- /dev/null +++ b/phpBB/includes/cache/factory.php @@ -0,0 +1,52 @@ +acm_type = $acm_type; + } + + public function get_acm() + { + $class_name = 'phpbb_cache_driver_' . $this->acm_type; + return new $class_name(); + } + + public function get_service() + { + $acm = $this->get_acm(); + $service = new phpbb_cache_service($acm); + return $service; + } + + /** + * for convenience to allow: + * $cache = phpbb_cache_factory::create('file')->get_service(); + */ + public static function create($acm_type) + { + return new self($acm_type); + } +} diff --git a/phpBB/includes/cache.php b/phpBB/includes/cache/service.php similarity index 85% rename from phpBB/includes/cache.php rename to phpBB/includes/cache/service.php index 49b690f1a4..424606bfc8 100644 --- a/phpBB/includes/cache.php +++ b/phpBB/includes/cache/service.php @@ -17,11 +17,28 @@ if (!defined('IN_PHPBB')) } /** -* Class for grabbing/handling cached entries, extends acm_file or acm_db depending on the setup +* Class for grabbing/handling cached entries * @package acm */ -class cache extends acm +class phpbb_cache_service { + private $acm; + + public function __construct(phpbb_cache_driver_interface $acm = null) + { + $this->set_acm($acm); + } + + public function set_acm(phpbb_cache_driver_interface $acm) + { + $this->acm = $acm; + } + + public function __call($method, $arguments) + { + return call_user_func_array(array($this->acm, $method), $arguments); + } + /** * Get config values */ @@ -29,7 +46,7 @@ class cache extends acm { global $db; - if (($config = $this->get('config')) !== false) + if (($config = $this->acm->get('config')) !== false) { $sql = 'SELECT config_name, config_value FROM ' . CONFIG_TABLE . ' @@ -61,7 +78,7 @@ class cache extends acm } $db->sql_freeresult($result); - $this->put('config', $cached_config); + $this->acm->put('config', $cached_config); } return $config; @@ -75,7 +92,7 @@ class cache extends acm { global $db; - if (($censors = $this->get('_word_censors')) === false) + if (($censors = $this->acm->get('_word_censors')) === false) { $sql = 'SELECT word, replacement FROM ' . WORDS_TABLE; @@ -89,7 +106,7 @@ class cache extends acm } $db->sql_freeresult($result); - $this->put('_word_censors', $censors); + $this->acm->put('_word_censors', $censors); } return $censors; @@ -100,7 +117,7 @@ class cache extends acm */ function obtain_icons() { - if (($icons = $this->get('_icons')) === false) + if (($icons = $this->acm->get('_icons')) === false) { global $db; @@ -120,7 +137,7 @@ class cache extends acm } $db->sql_freeresult($result); - $this->put('_icons', $icons); + $this->acm->put('_icons', $icons); } return $icons; @@ -131,7 +148,7 @@ class cache extends acm */ function obtain_ranks() { - if (($ranks = $this->get('_ranks')) === false) + if (($ranks = $this->acm->get('_ranks')) === false) { global $db; @@ -161,7 +178,7 @@ class cache extends acm } $db->sql_freeresult($result); - $this->put('_ranks', $ranks); + $this->acm->put('_ranks', $ranks); } return $ranks; @@ -176,7 +193,7 @@ class cache extends acm */ function obtain_attach_extensions($forum_id) { - if (($extensions = $this->get('_extensions')) === false) + if (($extensions = $this->acm->get('_extensions')) === false) { global $db; @@ -220,7 +237,7 @@ class cache extends acm } $db->sql_freeresult($result); - $this->put('_extensions', $extensions); + $this->acm->put('_extensions', $extensions); } // Forum post @@ -281,7 +298,7 @@ class cache extends acm */ function obtain_bots() { - if (($bots = $this->get('_bots')) === false) + if (($bots = $this->acm->get('_bots')) === false) { global $db; @@ -320,7 +337,7 @@ class cache extends acm } $db->sql_freeresult($result); - $this->put('_bots', $bots); + $this->acm->put('_bots', $bots); } return $bots; @@ -341,7 +358,7 @@ class cache extends acm foreach ($parsed_items as $key => $parsed_array) { - $parsed_array = $this->get('_cfg_' . $key . '_' . $theme[$key . '_path']); + $parsed_array = $this->acm->get('_cfg_' . $key . '_' . $theme[$key . '_path']); if ($parsed_array === false) { @@ -367,7 +384,7 @@ class cache extends acm $parsed_array = parse_cfg_file($filename); $parsed_array['filetime'] = @filemtime($filename); - $this->put('_cfg_' . $key . '_' . $theme[$key . '_path'], $parsed_array); + $this->acm->put('_cfg_' . $key . '_' . $theme[$key . '_path'], $parsed_array); } $parsed_items[$key] = $parsed_array; } @@ -380,7 +397,7 @@ class cache extends acm */ function obtain_disallowed_usernames() { - if (($usernames = $this->get('_disallowed_usernames')) === false) + if (($usernames = $this->acm->get('_disallowed_usernames')) === false) { global $db; @@ -395,7 +412,7 @@ class cache extends acm } $db->sql_freeresult($result); - $this->put('_disallowed_usernames', $usernames); + $this->acm->put('_disallowed_usernames', $usernames); } return $usernames; @@ -408,7 +425,7 @@ class cache extends acm { global $phpbb_root_path, $phpEx; - if (($hook_files = $this->get('_hooks')) === false) + if (($hook_files = $this->acm->get('_hooks')) === false) { $hook_files = array(); @@ -427,7 +444,7 @@ class cache extends acm closedir($dh); } - $this->put('_hooks', $hook_files); + $this->acm->put('_hooks', $hook_files); } return $hook_files; diff --git a/phpBB/includes/class_loader.php b/phpBB/includes/class_loader.php index 5df654799a..fa121341aa 100644 --- a/phpBB/includes/class_loader.php +++ b/phpBB/includes/class_loader.php @@ -33,7 +33,7 @@ class phpbb_class_loader { private $phpbb_root_path; private $php_ext; - private $cache; + private $acm; private $cached_paths = array(); /** @@ -42,13 +42,14 @@ class phpbb_class_loader * * @param string $phpbb_root_path phpBB's root directory containing includes/ * @param string $php_ext The file extension for PHP files + * @param phpbb_acm_interface $acm An implementation of the phpBB cache interface. */ - public function __construct($phpbb_root_path, $php_ext = '.php', $cache = null) + public function __construct($phpbb_root_path, $php_ext = '.php', phpbb_cache_driver_interface $acm = null) { $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; - $this->set_cache($cache); + $this->set_acm($acm); } /** @@ -56,13 +57,13 @@ class phpbb_class_loader * the class loader will resolve paths by checking for the existance of every * directory in the class name every time. * - * @param acm $cache An implementation of the phpBB cache interface. + * @param phpbb_acm_interface $acm An implementation of the phpBB cache interface. */ - public function set_cache($cache = null) + public function set_acm(phpbb_cache_driver_interface $acm = null) { - if ($cache) + if ($acm) { - $this->cached_paths = $cache->get('class_loader'); + $this->cached_paths = $acm->get('class_loader'); if ($this->cached_paths === false) { @@ -70,7 +71,7 @@ class phpbb_class_loader } } - $this->cache = $cache; + $this->acm = $acm; } /** @@ -133,10 +134,10 @@ class phpbb_class_loader return false; } - if ($this->cache) + if ($this->acm) { $this->cached_paths[$class] = $relative_path; - $this->cache->put('class_loader', $this->cached_paths); + $this->acm->put('class_loader', $this->cached_paths); } return $path_prefix . $relative_path . $this->php_ext; diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index b33c0f4a11..961edd589f 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -61,8 +61,6 @@ if (!empty($load_extensions) && function_exists('dl')) // Include files require($phpbb_root_path . 'includes/class_loader.' . $phpEx); -require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx); -require($phpbb_root_path . 'includes/cache.' . $phpEx); require($phpbb_root_path . 'includes/template.' . $phpEx); require($phpbb_root_path . 'includes/session.' . $phpEx); require($phpbb_root_path . 'includes/auth.' . $phpEx); @@ -93,11 +91,14 @@ else define('STRIP', (get_magic_quotes_gpc()) ? true : false); } -$cache = new cache(); - -$class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx, $cache); +$class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx); $class_loader->register(); +// set up caching +$acm = phpbb_cache_factory::create($acm_type)->get_acm(); +$class_loader->set_acm($acm); +$cache = new phpbb_cache_service($acm); + $request = new phpbb_request(); $user = new user(); $db = new $sql_db(); diff --git a/phpBB/install/index.php b/phpBB/install/index.php index 1a037fe496..6486eb09d8 100644 --- a/phpBB/install/index.php +++ b/phpBB/install/index.php @@ -163,8 +163,6 @@ if (file_exists($phpbb_root_path . 'includes/functions_content.' . $phpEx)) include($phpbb_root_path . 'includes/auth.' . $phpEx); include($phpbb_root_path . 'includes/session.' . $phpEx); include($phpbb_root_path . 'includes/template.' . $phpEx); -include($phpbb_root_path . 'includes/acm/acm_file.' . $phpEx); -include($phpbb_root_path . 'includes/cache.' . $phpEx); include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); include($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx); require($phpbb_root_path . 'includes/functions_install.' . $phpEx); @@ -172,6 +170,11 @@ require($phpbb_root_path . 'includes/functions_install.' . $phpEx); $class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx); $class_loader->register(); +// set up caching +$acm = phpbb_cache_factory::create($acm_type)->get_acm(); +$class_loader->set_acm($acm); +$cache = new phpbb_cache_service($acm); + $request = new phpbb_request(); // make sure request_var uses this request instance diff --git a/phpBB/style.php b/phpBB/style.php index 3c2f94023b..418bbb9bab 100644 --- a/phpBB/style.php +++ b/phpBB/style.php @@ -57,18 +57,19 @@ if ($id) { // Include files require($phpbb_root_path . 'includes/class_loader.' . $phpEx); - require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx); - require($phpbb_root_path . 'includes/cache.' . $phpEx); require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx); require($phpbb_root_path . 'includes/constants.' . $phpEx); require($phpbb_root_path . 'includes/functions.' . $phpEx); - $cache = new cache(); - - $class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx, $cache); + $class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx); $class_loader->register(); - $request = new phpbb_request(); + // set up caching + $acm = phpbb_cache_factory::create($acm_type)->get_acm(); + $class_loader->set_acm($acm); + $cache = new phpbb_cache_service($acm); + + $request = new phpbb_request(); $db = new $sql_db(); // make sure request_var uses this request instance @@ -308,5 +309,3 @@ if ($id) } $db->sql_close(); } - -exit; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 161bd83a3d..8d4e9c4527 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -28,7 +28,7 @@ else require_once $phpbb_root_path . 'includes/constants.php'; require_once $phpbb_root_path . 'includes/class_loader.' . $phpEx; -$class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx); +$class_loader = new phpbb_class_loader($phpbb_root_path, '.php'); $class_loader->register(); require_once 'test_framework/phpbb_test_case_helpers.php'; diff --git a/tests/cache/all_tests.php b/tests/cache/all_tests.php new file mode 100644 index 0000000000..829d496e5d --- /dev/null +++ b/tests/cache/all_tests.php @@ -0,0 +1,40 @@ +addTestSuite('phpbb_cache_test'); + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'phpbb_cache_all_tests::main') +{ + phpbb_cache_all_tests::main(); +} diff --git a/tests/cache/cache_test.php b/tests/cache/cache_test.php new file mode 100644 index 0000000000..220fddfd25 --- /dev/null +++ b/tests/cache/cache_test.php @@ -0,0 +1,39 @@ +put('test_key', 'test_value'); + $acm->save(); + + $this->assertEquals( + 'test_value', + $acm->get('test_key'), + 'File ACM put and get' + ); + } +} diff --git a/tests/class_loader/cache_mock.php b/tests/class_loader/cache_mock.php index b254978fcc..73d1e64cf5 100644 --- a/tests/class_loader/cache_mock.php +++ b/tests/class_loader/cache_mock.php @@ -8,7 +8,9 @@ * */ -class phpbb_cache_mock +require '../phpBB/includes/cache/driver/interface.php'; + +class phpbb_cache_mock implements phpbb_cache_driver_interface { private $variables = array(); @@ -22,8 +24,51 @@ class phpbb_cache_mock return false; } - function put($var_name, $value) + function put($var_name, $value, $ttl = 0) { $this->variables[$var_name] = $value; } + + function load() + { + } + function unload() + { + } + function save() + { + } + function tidy() + { + } + function purge() + { + } + function destroy($var_name, $table = '') + { + } + public function _exists($var_name) + { + } + public function sql_load($query) + { + } + public function sql_save($query, &$query_result, $ttl) + { + } + public function sql_exists($query_id) + { + } + public function sql_fetchrow($query_id) + { + } + public function sql_fetchfield($query_id, $field) + { + } + public function sql_rowseek($rownum, $query_id) + { + } + public function sql_freeresult($query_id) + { + } } diff --git a/tests/class_loader/class_loader_test.php b/tests/class_loader/class_loader_test.php index 3eb3c915a1..c01278f914 100644 --- a/tests/class_loader/class_loader_test.php +++ b/tests/class_loader/class_loader_test.php @@ -10,8 +10,6 @@ require_once __DIR__ . '/cache_mock.php'; -require_once __DIR__ . '/../../phpBB/includes/class_loader.php'; - class phpbb_class_loader_test extends PHPUnit_Framework_TestCase { public function setUp() @@ -63,8 +61,16 @@ class phpbb_class_loader_test extends PHPUnit_Framework_TestCase public function test_resolve_cached() { - $cache = new phpbb_cache_mock; - $cache->put('class_loader', array('phpbb_a_cached_name' => 'a/cached_name')); + $cacheMap = array('class_loader' => array('phpbb_a_cached_name' => 'a/cached_name')); + + $cache = $this->getMock('phpbb_cache_driver_interface', + array('get', 'put', 'load', 'unload', 'save', 'tidy', 'purge', 'destroy', '_exists', + 'sql_load', 'sql_save', 'sql_exists', 'sql_fetchrow', 'sql_fetchfield', 'sql_rowseek', 'sql_freeresult')); + $cache->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function($var_name) use ($cacheMap) { + return $cacheMap[$var_name]; + })); $prefix = __DIR__ . '/'; $class_loader = new phpbb_class_loader($prefix, '.php', $cache); From 1aef7eb20ee195c7f21d6c5b78653b7c43e669ec Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Sun, 9 Jan 2011 21:09:56 +0100 Subject: [PATCH 4/4] [task/acm-refactor] Cleaning up left over mentions of ACM and fixing tests. PHPBB3-9983 --- phpBB/common.php | 6 +-- phpBB/download/file.php | 6 +-- phpBB/includes/cache/factory.php | 21 +++------- phpBB/includes/class_loader.php | 22 +++++----- phpBB/install/database_update.php | 6 +-- phpBB/install/index.php | 7 ++-- phpBB/style.php | 6 +-- phpunit.xml.dist | 2 +- tests/cache/all_tests.php | 40 ------------------- tests/cache/cache_test.php | 24 ++++++----- tests/cache/tmp/.gitkeep | 0 tests/class_loader/class_loader_test.php | 18 +++------ .../cache_mock.php => mock/cache.php} | 35 +++++++++++----- 13 files changed, 76 insertions(+), 117 deletions(-) delete mode 100644 tests/cache/all_tests.php create mode 100644 tests/cache/tmp/.gitkeep rename tests/{class_loader/cache_mock.php => mock/cache.php} (51%) diff --git a/phpBB/common.php b/phpBB/common.php index fc6009eb21..7b6a407c94 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -206,9 +206,9 @@ $class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx); $class_loader->register(); // set up caching -$acm = phpbb_cache_factory::create($acm_type)->get_acm(); -$class_loader->set_acm($acm); -$cache = new phpbb_cache_service($acm); +$cache_factory = new phpbb_cache_factory($acm_type); +$class_loader->set_cache($cache_factory->get_driver()); +$cache = $cache_factory->get_service(); // Instantiate some basic classes $request = new phpbb_request(); diff --git a/phpBB/download/file.php b/phpBB/download/file.php index 74925fb447..a7e8b9f06c 100644 --- a/phpBB/download/file.php +++ b/phpBB/download/file.php @@ -54,9 +54,9 @@ if (isset($_GET['avatar'])) $class_loader->register(); // set up caching - $acm = phpbb_cache_factory::create($acm_type)->get_acm(); - $class_loader->set_acm($acm); - $cache = new phpbb_cache_service($acm); + $cache_factory = new phpbb_cache_factory($acm_type); + $class_loader->set_cache($cache_factory->get_driver()); + $cache = $cache_factory->get_service(); $db = new $sql_db(); diff --git a/phpBB/includes/cache/factory.php b/phpBB/includes/cache/factory.php index cc88780bf2..f38e19cbe6 100644 --- a/phpBB/includes/cache/factory.php +++ b/phpBB/includes/cache/factory.php @@ -22,31 +22,22 @@ if (!defined('IN_PHPBB')) class phpbb_cache_factory { private $acm_type; - + public function __construct($acm_type) { $this->acm_type = $acm_type; } - - public function get_acm() + + public function get_driver() { $class_name = 'phpbb_cache_driver_' . $this->acm_type; return new $class_name(); } - + public function get_service() { - $acm = $this->get_acm(); - $service = new phpbb_cache_service($acm); + $driver = $this->get_driver(); + $service = new phpbb_cache_service($driver); return $service; } - - /** - * for convenience to allow: - * $cache = phpbb_cache_factory::create('file')->get_service(); - */ - public static function create($acm_type) - { - return new self($acm_type); - } } diff --git a/phpBB/includes/class_loader.php b/phpBB/includes/class_loader.php index fa121341aa..a28d745983 100644 --- a/phpBB/includes/class_loader.php +++ b/phpBB/includes/class_loader.php @@ -33,7 +33,7 @@ class phpbb_class_loader { private $phpbb_root_path; private $php_ext; - private $acm; + private $cache; private $cached_paths = array(); /** @@ -42,14 +42,14 @@ class phpbb_class_loader * * @param string $phpbb_root_path phpBB's root directory containing includes/ * @param string $php_ext The file extension for PHP files - * @param phpbb_acm_interface $acm An implementation of the phpBB cache interface. + * @param phpbb_cache_driver_interface $cache An implementation of the phpBB cache interface. */ - public function __construct($phpbb_root_path, $php_ext = '.php', phpbb_cache_driver_interface $acm = null) + public function __construct($phpbb_root_path, $php_ext = '.php', phpbb_cache_driver_interface $cache = null) { $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; - $this->set_acm($acm); + $this->set_cache($cache); } /** @@ -57,13 +57,13 @@ class phpbb_class_loader * the class loader will resolve paths by checking for the existance of every * directory in the class name every time. * - * @param phpbb_acm_interface $acm An implementation of the phpBB cache interface. + * @param phpbb_cache_driver_interface $cache An implementation of the phpBB cache interface. */ - public function set_acm(phpbb_cache_driver_interface $acm = null) + public function set_cache(phpbb_cache_driver_interface $cache = null) { - if ($acm) + if ($cache) { - $this->cached_paths = $acm->get('class_loader'); + $this->cached_paths = $cache->get('class_loader'); if ($this->cached_paths === false) { @@ -71,7 +71,7 @@ class phpbb_class_loader } } - $this->acm = $acm; + $this->cache = $cache; } /** @@ -134,10 +134,10 @@ class phpbb_class_loader return false; } - if ($this->acm) + if ($this->cache) { $this->cached_paths[$class] = $relative_path; - $this->acm->put('class_loader', $this->cached_paths); + $this->cache->put('class_loader', $this->cached_paths); } return $path_prefix . $relative_path . $this->php_ext; diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index 961edd589f..f73f7472f0 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -95,9 +95,9 @@ $class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx); $class_loader->register(); // set up caching -$acm = phpbb_cache_factory::create($acm_type)->get_acm(); -$class_loader->set_acm($acm); -$cache = new phpbb_cache_service($acm); +$cache_factory = new phpbb_cache_factory($acm_type); +$class_loader->set_cache($cache_factory->get_driver()); +$cache = $cache_factory->get_service(); $request = new phpbb_request(); $user = new user(); diff --git a/phpBB/install/index.php b/phpBB/install/index.php index 6486eb09d8..653268ba68 100644 --- a/phpBB/install/index.php +++ b/phpBB/install/index.php @@ -171,9 +171,9 @@ $class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx); $class_loader->register(); // set up caching -$acm = phpbb_cache_factory::create($acm_type)->get_acm(); -$class_loader->set_acm($acm); -$cache = new phpbb_cache_service($acm); +$cache_factory = new phpbb_cache_factory('file'); +$class_loader->set_cache($cache_factory->get_driver()); +$cache = $cache_factory->get_service(); $request = new phpbb_request(); @@ -262,7 +262,6 @@ set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handle $user = new user(); $auth = new auth(); -$cache = new cache(); $template = new template(); // Add own hook handler, if present. :o diff --git a/phpBB/style.php b/phpBB/style.php index 418bbb9bab..cff91a2312 100644 --- a/phpBB/style.php +++ b/phpBB/style.php @@ -65,9 +65,9 @@ if ($id) $class_loader->register(); // set up caching - $acm = phpbb_cache_factory::create($acm_type)->get_acm(); - $class_loader->set_acm($acm); - $cache = new phpbb_cache_service($acm); + $cache_factory = new phpbb_cache_factory($acm_type); + $class_loader->set_cache($cache_factory->get_driver()); + $cache = $cache_factory->get_service(); $request = new phpbb_request(); $db = new $sql_db(); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d1d8adbdd5..4209c61947 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,7 +16,7 @@ ./tests/ - + ./tests/ diff --git a/tests/cache/all_tests.php b/tests/cache/all_tests.php deleted file mode 100644 index 829d496e5d..0000000000 --- a/tests/cache/all_tests.php +++ /dev/null @@ -1,40 +0,0 @@ -addTestSuite('phpbb_cache_test'); - - return $suite; - } -} - -if (PHPUnit_MAIN_METHOD == 'phpbb_cache_all_tests::main') -{ - phpbb_cache_all_tests::main(); -} diff --git a/tests/cache/cache_test.php b/tests/cache/cache_test.php index 220fddfd25..463095f129 100644 --- a/tests/cache/cache_test.php +++ b/tests/cache/cache_test.php @@ -2,37 +2,39 @@ /** * * @package testing -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ -require_once 'test_framework/framework.php'; +require_once __DIR__ . '/../../phpBB/includes/functions.php'; class phpbb_cache_test extends phpbb_test_case { protected function tearDown() { - $iterator = new DirectoryIterator('cache/tmp'); + $iterator = new DirectoryIterator(__DIR__ . '/tmp'); foreach ($iterator as $file) { - if (is_file('cache/tmp/' . $file)) + if (is_file(__DIR__ . '/tmp/' . $file) && $file != '.gitkeep') { - unlink('cache/tmp/' . $file); + unlink(__DIR__ . '/tmp/' . $file); } } } - public function test_acm_file() + public function test_cache_driver_file() { - $acm = new phpbb_cache_driver_file('cache/tmp/'); - $acm->put('test_key', 'test_value'); - $acm->save(); - + global $phpEx; + $phpEx = 'txt'; // do not store files as .php + + $driver = new phpbb_cache_driver_file(__DIR__ . '/tmp/'); + $driver->put('test_key', 'test_value'); + $driver->save(); + $this->assertEquals( 'test_value', - $acm->get('test_key'), + $driver->get('test_key'), 'File ACM put and get' ); } diff --git a/tests/cache/tmp/.gitkeep b/tests/cache/tmp/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/class_loader/class_loader_test.php b/tests/class_loader/class_loader_test.php index c01278f914..cc6862dc70 100644 --- a/tests/class_loader/class_loader_test.php +++ b/tests/class_loader/class_loader_test.php @@ -2,13 +2,12 @@ /** * * @package testing -* @version $Id$ -* @copyright (c) 2008 phpBB Group +* @copyright (c) 2011 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ -require_once __DIR__ . '/cache_mock.php'; +require_once __DIR__ . '/../mock/cache.php'; class phpbb_class_loader_test extends PHPUnit_Framework_TestCase { @@ -62,15 +61,7 @@ class phpbb_class_loader_test extends PHPUnit_Framework_TestCase public function test_resolve_cached() { $cacheMap = array('class_loader' => array('phpbb_a_cached_name' => 'a/cached_name')); - - $cache = $this->getMock('phpbb_cache_driver_interface', - array('get', 'put', 'load', 'unload', 'save', 'tidy', 'purge', 'destroy', '_exists', - 'sql_load', 'sql_save', 'sql_exists', 'sql_fetchrow', 'sql_fetchfield', 'sql_rowseek', 'sql_freeresult')); - $cache->expects($this->any()) - ->method('get') - ->will($this->returnCallback(function($var_name) use ($cacheMap) { - return $cacheMap[$var_name]; - })); + $cache = new phpbb_mock_cache($cacheMap); $prefix = __DIR__ . '/'; $class_loader = new phpbb_class_loader($prefix, '.php', $cache); @@ -88,5 +79,8 @@ class phpbb_class_loader_test extends PHPUnit_Framework_TestCase $class_loader->resolve_path('phpbb_a_cached_name'), 'Class in a directory' ); + + $cacheMap['class_loader']['phpbb_dir_class_name'] = 'dir/class_name'; + $cache->check($this, $cacheMap); } } diff --git a/tests/class_loader/cache_mock.php b/tests/mock/cache.php similarity index 51% rename from tests/class_loader/cache_mock.php rename to tests/mock/cache.php index 73d1e64cf5..3bfb31f1be 100644 --- a/tests/class_loader/cache_mock.php +++ b/tests/mock/cache.php @@ -2,31 +2,44 @@ /** * * @package testing -* @version $Id$ -* @copyright (c) 2008 phpBB Group +* @copyright (c) 2011 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ -require '../phpBB/includes/cache/driver/interface.php'; - -class phpbb_cache_mock implements phpbb_cache_driver_interface +class phpbb_mock_cache implements phpbb_cache_driver_interface { - private $variables = array(); + protected $data; - function get($var_name) + public function __construct($data = array()) { - if (isset($this->variables[$var_name])) + $this->data = $data; + } + + public function get($var_name) + { + if (isset($this->data[$var_name])) { - return $this->variables[$var_name]; + return $this->data[$var_name]; } return false; } - function put($var_name, $value, $ttl = 0) + public function put($var_name, $var, $ttl = 0) { - $this->variables[$var_name] = $value; + $this->data[$var_name] = $var; + } + + public function checkVar(PHPUnit_Framework_Assert $test, $var_name, $data) + { + $test->assertTrue(isset($this->data[$var_name])); + $test->assertEquals($data, $this->data[$var_name]); + } + + public function check(PHPUnit_Framework_Assert $test, $data) + { + $test->assertEquals($data, $this->data); } function load()