diff options
author | Tristan Darricau <github@nicofuma.fr> | 2014-06-28 01:36:59 +0200 |
---|---|---|
committer | Tristan Darricau <github@nicofuma.fr> | 2014-07-07 01:02:27 +0200 |
commit | 1d966fbc86db47c3518b35de849cad3a1f295e71 (patch) | |
tree | 06ba40dbd2a99046bee989b1b4ff987e37e37a0b | |
parent | c87f44c6692c55adb3ce5bc5b7bca8da7b1357ab (diff) | |
download | forums-1d966fbc86db47c3518b35de849cad3a1f295e71.tar forums-1d966fbc86db47c3518b35de849cad3a1f295e71.tar.gz forums-1d966fbc86db47c3518b35de849cad3a1f295e71.tar.bz2 forums-1d966fbc86db47c3518b35de849cad3a1f295e71.tar.xz forums-1d966fbc86db47c3518b35de849cad3a1f295e71.zip |
[ticket/12775] Add tests for the container factory
PHPBB3-12775
-rw-r--r-- | phpBB/includes/functions_container.php | 291 | ||||
-rw-r--r-- | phpBB/phpbb/di/container_factory.php | 2 | ||||
-rw-r--r-- | tests/di/create_container_test.php | 148 | ||||
-rw-r--r-- | tests/di/fixtures/config/services.yml | 9 | ||||
-rw-r--r-- | tests/di/fixtures/ext/vendor/available/config/services.yml | 2 | ||||
-rw-r--r-- | tests/di/fixtures/ext/vendor/disabled/config/services.yml | 2 | ||||
-rw-r--r-- | tests/di/fixtures/ext/vendor/enabled/config/services.yml | 2 | ||||
-rw-r--r-- | tests/di/fixtures/other_config/services.yml | 9 | ||||
-rw-r--r-- | tests/mock/container_factory.php | 20 |
9 files changed, 165 insertions, 320 deletions
diff --git a/phpBB/includes/functions_container.php b/phpBB/includes/functions_container.php deleted file mode 100644 index 96086a59ed..0000000000 --- a/phpBB/includes/functions_container.php +++ /dev/null @@ -1,291 +0,0 @@ -<?php -///** -//* -//* This file is part of the phpBB Forum Software package. -//* -//* @copyright (c) phpBB Limited <https://www.phpbb.com> -//* @license GNU General Public License, version 2 (GPL-2.0) -//* -//* For full copyright and license information, please see -//* the docs/CREDITS.txt file. -//* -//*/ -// -//use Symfony\Component\Config\FileLocator; -//use Symfony\Component\DependencyInjection\ContainerBuilder; -//use Symfony\Component\DependencyInjection\Dumper\PhpDumper; -//use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; -// -///** -//* @ignore -//*/ -//if (!defined('IN_PHPBB')) -//{ -// exit; -//} -// -///** -//* Get DB connection from config.php. -//* -//* Used to bootstrap the container. -//* -//* @param string $config_file -//* @return \phpbb\db\driver\driver_interface -//*/ -//function phpbb_bootstrap_db_connection($config_file) -//{ -// require($config_file); -// $dbal_driver_class = phpbb_convert_30_dbms_to_31($dbms); -// -// $db = new $dbal_driver_class(); -// $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, defined('PHPBB_DB_NEW_LINK')); -// -// return $db; -//} -// -///** -//* Get table prefix from config.php. -//* -//* Used to bootstrap the container. -//* -//* @param string $config_file -//* @return string table prefix -//*/ -//function phpbb_bootstrap_table_prefix($config_file) -//{ -// require($config_file); -// return $table_prefix; -//} -// -///** -//* Get enabled extensions. -//* -//* Used to bootstrap the container. -//* -//* @param string $config_file -//* @param string $phpbb_root_path -//* @return array enabled extensions -//*/ -//function phpbb_bootstrap_enabled_exts($config_file, $phpbb_root_path) -//{ -// $db = phpbb_bootstrap_db_connection($config_file); -// $table_prefix = phpbb_bootstrap_table_prefix($config_file); -// $extension_table = $table_prefix.'ext'; -// -// $sql = 'SELECT * -// FROM ' . $extension_table . ' -// WHERE ext_active = 1'; -// -// $result = $db->sql_query($sql); -// $rows = $db->sql_fetchrowset($result); -// $db->sql_freeresult($result); -// -// $exts = array(); -// foreach ($rows as $row) -// { -// $exts[$row['ext_name']] = $phpbb_root_path . 'ext/' . $row['ext_name'] . '/'; -// } -// -// return $exts; -//} -// -///** -//* Create the ContainerBuilder object -//* -//* @param array $extensions Array of Container extension objects -//* @param string $phpbb_root_path Root path -//* @param string $php_ext PHP Extension -//* @return ContainerBuilder object -//*/ -//function phpbb_create_container(array $extensions, $phpbb_root_path, $php_ext) -//{ -// $container = new ContainerBuilder(); -// -// foreach ($extensions as $extension) -// { -// $container->registerExtension($extension); -// $container->loadFromExtension($extension->getAlias()); -// } -// -// $container->setParameter('core.root_path', $phpbb_root_path); -// $container->setParameter('core.php_ext', $php_ext); -// -// return $container; -//} -// -///** -//* Create installer container -//* -//* @param string $phpbb_root_path Root path -//* @param string $php_ext PHP Extension -//* @return ContainerBuilder object -//*/ -//function phpbb_create_install_container($phpbb_root_path, $php_ext) -//{ -// $other_config_path = $phpbb_root_path . 'install/update/new/config/'; -// $config_path = file_exists($other_config_path . 'services.yml') ? $other_config_path : $phpbb_root_path . 'config/'; -// -// $core = new \phpbb\di\extension\core($config_path); -// $container = phpbb_create_container(array($core), $phpbb_root_path, $php_ext); -// -// $container->setParameter('core.root_path', $phpbb_root_path); -// $container->setParameter('core.adm_relative_path', $phpbb_adm_relative_path); -// $container->setParameter('core.php_ext', $php_ext); -// $container->setParameter('core.table_prefix', ''); -// -// $container->register('dbal.conn')->setSynthetic(true); -// -// $container->setAlias('cache.driver', 'cache.driver.install'); -// -// $container->compile(); -// -// return $container; -//} -// -///** -//* Create updater container -//* -//* @param string $phpbb_root_path Root path -//* @param string $php_ext PHP Extension -//* @param array $config_path Path to config directory -//* @return ContainerBuilder object (compiled) -//*/ -//function phpbb_create_update_container($phpbb_root_path, $php_ext, $config_path) -//{ -// $config_file = $phpbb_root_path . 'config.' . $php_ext; -// return phpbb_create_compiled_container( -// $config_file, -// array( -// new phpbb\di\extension\config($config_file), -// new phpbb\di\extension\core($config_path), -// ), -// array( -// new phpbb\di\pass\collection_pass(), -// new phpbb\di\pass\kernel_pass(), -// ), -// $phpbb_root_path, -// $php_ext -// ); -//} -// -///** -//* Create a compiled ContainerBuilder object -//* -//* @param array $extensions Array of Container extension objects -//* @param array $passes Array of Compiler Pass objects -//* @param string $phpbb_root_path Root path -//* @param string $php_ext PHP Extension -//* @return ContainerBuilder object (compiled) -//*/ -//function phpbb_create_compiled_container($config_file, array $extensions, array $passes, $phpbb_root_path, $php_ext) -//{ -// // Create the final container to be compiled and cached -// $container = phpbb_create_container($extensions, $phpbb_root_path, $php_ext); -// -// // Compile the container -// foreach ($passes as $pass) -// { -// $container->addCompilerPass($pass); -// } -// $container->compile(); -// -// return $container; -//} -// -///** -//* Create a compiled and dumped ContainerBuilder object -//* -//* @param array $extensions Array of Container extension objects -//* @param array $passes Array of Compiler Pass objects -//* @param string $phpbb_root_path Root path -//* @param string $php_ext PHP Extension -//* @return ContainerBuilder object (compiled) -//*/ -//function phpbb_create_dumped_container($config_file, array $extensions, array $passes, $phpbb_root_path, $php_ext) -//{ -// // Check for our cached container; if it exists, use it -// $container_filename = phpbb_container_filename($phpbb_root_path, $php_ext); -// if (file_exists($container_filename)) -// { -// require($container_filename); -// return new phpbb_cache_container(); -// } -// -// $container = phpbb_create_compiled_container($config_file, $extensions, $passes, $phpbb_root_path, $php_ext); -// -// // Lastly, we create our cached container class -// $dumper = new PhpDumper($container); -// $cached_container_dump = $dumper->dump(array( -// 'class' => 'phpbb_cache_container', -// 'base_class' => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder', -// )); -// -// file_put_contents($container_filename, $cached_container_dump); -// -// return $container; -//} -// -///** -//* Create an environment-specific ContainerBuilder object -//* -//* If debug is enabled, the container is re-compiled every time. -//* This ensures that the latest changes will always be reflected -//* during development. -//* -//* Otherwise it will get the existing dumped container and use -//* that one instead. -//* -//* @param array $extensions Array of Container extension objects -//* @param array $passes Array of Compiler Pass objects -//* @param string $phpbb_root_path Root path -//* @param string $php_ext PHP Extension -//* @return ContainerBuilder object (compiled) -//*/ -//function phpbb_create_dumped_container_unless_debug($config_file, array $extensions, array $passes, $phpbb_root_path, $php_ext) -//{ -// $container_factory = defined('DEBUG_CONTAINER') ? 'phpbb_create_compiled_container' : 'phpbb_create_dumped_container'; -// return $container_factory($config_file, $extensions, $passes, $phpbb_root_path, $php_ext); -//} -// -///** -//* Create a default ContainerBuilder object -//* -//* Contains the default configuration of the phpBB container. -//* -//* @param array $extensions Array of Container extension objects -//* @param array $passes Array of Compiler Pass objects -//* @return ContainerBuilder object (compiled) -//*/ -//function phpbb_create_default_container($phpbb_root_path, $php_ext) -//{ -// $config_file = $phpbb_root_path . 'config.' . $php_ext; -// $installed_exts = phpbb_bootstrap_enabled_exts($config_file, $phpbb_root_path); -// -// return phpbb_create_dumped_container_unless_debug( -// $config_file, -// array( -// new \phpbb\di\extension\config($config_file), -// new \phpbb\di\extension\core($phpbb_root_path . 'config'), -// new \phpbb\di\extension\ext($installed_exts), -// ), -// array( -// new \phpbb\di\pass\collection_pass(), -// new \phpbb\di\pass\kernel_pass(), -// ), -// $phpbb_root_path, -// $php_ext -// ); -//} -// -///** -//* Get the filename under which the dumped container will be stored. -//* -//* @param string $phpbb_root_path Root path -//* @param string $php_ext PHP Extension -//* @return Path for dumped container -//*/ -//function phpbb_container_filename($phpbb_root_path, $php_ext) -//{ -// $filename = str_replace(array('/', '.'), array('slash', 'dot'), $phpbb_root_path); -// return $phpbb_root_path . 'cache/container_' . $filename . '.' . $php_ext; -//} diff --git a/phpBB/phpbb/di/container_factory.php b/phpBB/phpbb/di/container_factory.php index a83c79f517..426c411e07 100644 --- a/phpBB/phpbb/di/container_factory.php +++ b/phpBB/phpbb/di/container_factory.php @@ -170,7 +170,7 @@ class container_factory $this->container->compile(); } - if ($this->dump_container && defined('DEBUG')) + if ($this->dump_container && !defined('DEBUG')) { $this->dump_container($container_filename); } diff --git a/tests/di/create_container_test.php b/tests/di/create_container_test.php index 5983b244fd..d41f1af5ae 100644 --- a/tests/di/create_container_test.php +++ b/tests/di/create_container_test.php @@ -14,50 +14,135 @@ namespace { require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; - require_once dirname(__FILE__) . '/../../phpBB/includes/functions_container.php'; - class phpbb_di_container_test extends phpbb_test_case + class phpbb_di_container_test extends \phpbb_test_case { - public function test_phpbb_create_container() + protected $config_php; + + /** + * @var \phpbb\di\container_factory + */ + protected $factory; + protected $phpbb_root_path; + protected $filename; + + public function setUp() { - $this->markTestSkipped(); - $phpbb_root_path = __DIR__ . '/../../phpBB/'; - $extensions = array( - new \phpbb\di\extension\config(__DIR__ . '/fixtures/config.php'), - new \phpbb\di\extension\core($phpbb_root_path . 'config'), - ); - $container = phpbb_create_container($extensions, $phpbb_root_path, 'php'); + $this->phpbb_root_path = dirname(__FILE__) . '/'; + $this->config_php = new \phpbb\config_php($this->phpbb_root_path . 'fixtures/', 'php'); + $this->factory = new phpbb_mock_container_factory($this->config_php, $this->phpbb_root_path . 'fixtures/', 'php'); + + $this->filename = $this->phpbb_root_path . '../tmp/cache/container.php'; + if (is_file($this->filename)) + { + unlink($this->filename); + } + + parent::setUp(); + } + public function test_default_container() + { + $container = $this->factory->get_container(); $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container); + + // Checks the core services + $this->assertTrue($container->hasParameter('core')); + + // Checks compile_container + $this->assertTrue($container->isFrozen()); + + // Checks inject_config + $this->assertTrue($container->hasParameter('dbal.dbhost')); + + // Checks use_extensions + $this->assertTrue($container->hasParameter('enabled')); + $this->assertFalse($container->hasParameter('disabled')); + $this->assertFalse($container->hasParameter('available')); + + // Checks set_custom_parameters + $this->assertTrue($container->hasParameter('core.root_path')); + + // Checks dump_container + $this->assertTrue(is_file($this->filename)); + + // Checks the construction of a dumped container + $container = $this->factory->get_container(); + $this->assertInstanceOf('phpbb_cache_container', $container); + $this->assertFalse($container->isFrozen()); + $container->getParameterBag(); // needed, otherwise the container is not marked as frozen + $this->assertTrue($container->isFrozen()); } - public function test_phpbb_create_install_container() + public function test_dump_container() { - $this->markTestSkipped(); - $phpbb_root_path = __DIR__ . '/../../phpBB/'; - $extensions = array( - new \phpbb\di\extension\config(__DIR__ . '/fixtures/config.php'), - new \phpbb\di\extension\core($phpbb_root_path . 'config'), - ); - $container = phpbb_create_install_container($phpbb_root_path, 'php'); + $this->factory->set_dump_container(false); + $container = $this->factory->get_container(); + $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container); + // Checks dump_container + $this->assertFalse(is_file($this->filename)); + + // Checks the construction of a dumped container + $container = $this->factory->get_container(); + $this->assertNotInstanceOf('phpbb_cache_container', $container); $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container); $this->assertTrue($container->isFrozen()); } - public function test_phpbb_create_compiled_container() + public function test_use_extensions() { - $this->markTestSkipped(); - $phpbb_root_path = __DIR__ . '/../../phpBB/'; - $config_file = __DIR__ . '/fixtures/config.php'; - $extensions = array( - new \phpbb\di\extension\config(__DIR__ . '/fixtures/config.php'), - new \phpbb\di\extension\core($phpbb_root_path . 'config'), - ); - $container = phpbb_create_compiled_container($config_file, $extensions, array(), $phpbb_root_path, 'php'); + $this->factory->set_use_extensions(false); + $container = $this->factory->get_container(); + $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container); + + // Checks the core services + $this->assertTrue($container->hasParameter('core')); + + // Checks use_extensions + $this->assertFalse($container->hasParameter('enabled')); + $this->assertFalse($container->hasParameter('disabled')); + $this->assertFalse($container->hasParameter('available')); + } + public function test_compile_container() + { + $this->factory->set_compile_container(false); + $container = $this->factory->get_container(); $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container); - $this->assertTrue($container->isFrozen()); + + // Checks compile_container + $this->assertFalse($container->isFrozen()); + } + + public function test_inject_config() + { + $this->factory->set_inject_config(false); + $container = $this->factory->get_container(); + $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container); + + // Checks inject_config + $this->assertFalse($container->hasParameter('dbal.dbhost')); + } + + public function test_set_config_path() + { + $this->factory->set_config_path($this->phpbb_root_path . 'fixtures/other_config/'); + $container = $this->factory->get_container(); + $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container); + + $this->assertTrue($container->hasParameter('other_config')); + $this->assertFalse($container->hasParameter('core')); + } + + public function test_set_custom_parameters() + { + $this->factory->set_custom_parameters(array('my_parameter' => true)); + $container = $this->factory->get_container(); + $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container); + + $this->assertTrue($container->hasParameter('my_parameter')); + $this->assertFalse($container->hasParameter('core.root_path')); } } } @@ -105,5 +190,12 @@ namespace phpbb\db\driver function sql_like_expression($expression) { } + + function sql_fetchrowset($query_id = false) + { + return array( + array('ext_name' => 'vendor/enabled'), + ); + } } } diff --git a/tests/di/fixtures/config/services.yml b/tests/di/fixtures/config/services.yml new file mode 100644 index 0000000000..71bc14f69a --- /dev/null +++ b/tests/di/fixtures/config/services.yml @@ -0,0 +1,9 @@ +parameters: + core: true + +services: + dispatcher: + class: phpbb\db\driver\container_mock + + config.php: + synthetic: true diff --git a/tests/di/fixtures/ext/vendor/available/config/services.yml b/tests/di/fixtures/ext/vendor/available/config/services.yml new file mode 100644 index 0000000000..2ced431f5a --- /dev/null +++ b/tests/di/fixtures/ext/vendor/available/config/services.yml @@ -0,0 +1,2 @@ +parameters: + available: true diff --git a/tests/di/fixtures/ext/vendor/disabled/config/services.yml b/tests/di/fixtures/ext/vendor/disabled/config/services.yml new file mode 100644 index 0000000000..31ada384bf --- /dev/null +++ b/tests/di/fixtures/ext/vendor/disabled/config/services.yml @@ -0,0 +1,2 @@ +parameters: + disabled: true diff --git a/tests/di/fixtures/ext/vendor/enabled/config/services.yml b/tests/di/fixtures/ext/vendor/enabled/config/services.yml new file mode 100644 index 0000000000..88a7919ed1 --- /dev/null +++ b/tests/di/fixtures/ext/vendor/enabled/config/services.yml @@ -0,0 +1,2 @@ +parameters: + enabled: true diff --git a/tests/di/fixtures/other_config/services.yml b/tests/di/fixtures/other_config/services.yml new file mode 100644 index 0000000000..5974d3b758 --- /dev/null +++ b/tests/di/fixtures/other_config/services.yml @@ -0,0 +1,9 @@ +parameters: + other_config: true + +services: + dispatcher: + class: phpbb\db\driver\container_mock + + config.php: + synthetic: true diff --git a/tests/mock/container_factory.php b/tests/mock/container_factory.php new file mode 100644 index 0000000000..8b1720d027 --- /dev/null +++ b/tests/mock/container_factory.php @@ -0,0 +1,20 @@ +<?php +/** +* +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +class phpbb_mock_container_factory extends \phpbb\di\container_factory +{ + protected function get_container_filename() + { + return $this->phpbb_root_path . '../../tmp/cache/container.' . $this->php_ext; + } +} |