diff options
Diffstat (limited to 'tests/console')
-rw-r--r-- | tests/console/cache/purge_test.php | 95 | ||||
-rw-r--r-- | tests/console/config/config_test.php | 251 | ||||
-rw-r--r-- | tests/console/cron/cron_list_test.php | 103 | ||||
-rw-r--r-- | tests/console/cron/fixtures/config.xml | 8 | ||||
-rw-r--r-- | tests/console/cron/run_test.php | 158 | ||||
-rw-r--r-- | tests/console/cron/tasks/simple.php | 27 | ||||
-rw-r--r-- | tests/console/cron/tasks/simple_not_ready.php | 13 | ||||
-rw-r--r-- | tests/console/cron/tasks/simple_ready.php | 8 |
8 files changed, 663 insertions, 0 deletions
diff --git a/tests/console/cache/purge_test.php b/tests/console/cache/purge_test.php new file mode 100644 index 0000000000..96988c1028 --- /dev/null +++ b/tests/console/cache/purge_test.php @@ -0,0 +1,95 @@ +<?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\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use phpbb\console\command\cache\purge; + +require_once dirname(__FILE__) . '/../../../phpBB/includes/functions_admin.php'; + +class phpbb_console_command_cache_purge_test extends phpbb_test_case +{ + protected $cache_dir; + protected $cache; + protected $command_name; + protected $db; + protected $config; + + public function __construct() + { + $this->cache_dir = dirname(__FILE__) . '/tmp/cache/'; + } + + protected function setUp() + { + if (file_exists($this->cache_dir)) + { + // cache directory possibly left after aborted + // or failed run earlier + $this->remove_cache_dir(); + } + $this->create_cache_dir(); + + $this->cache = new \phpbb\cache\driver\file($this->cache_dir); + + $this->db = $this->getMock('\phpbb\db\driver\driver_interface'); + + $this->config = new \phpbb\config\config(array('assets_version' => 1)); + $this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); + } + + public function test_purge() + { + $this->cache->put('test_key', 'test_value'); + + $this->assertEquals( + 'test_value', + $this->cache->get('test_key'), + 'File ACM put and get' + ); + + $command_tester = $this->get_command_tester(); + $exit_status = $command_tester->execute(array('command' => $this->command_name)); + + $this->assertSame(false, $this->cache->get('test_key')); + $this->assertSame(2, $this->config['assets_version']); + } + + private function create_cache_dir() + { + $this->get_test_case_helpers()->makedirs($this->cache_dir); + } + + private function remove_cache_dir() + { + $iterator = new DirectoryIterator($this->cache_dir); + foreach ($iterator as $file) + { + if ($file != '.' && $file != '..') + { + unlink($this->cache_dir . '/' . $file); + } + } + rmdir($this->cache_dir); + } + + public function get_command_tester() + { + $application = new Application(); + $application->add(new purge($this->user, $this->cache, $this->db, $this->getMock('\phpbb\auth\auth'), new \phpbb\log\null(), $this->config)); + + $command = $application->find('cache:purge'); + $this->command_name = $command->getName(); + return new CommandTester($command); + } +} diff --git a/tests/console/config/config_test.php b/tests/console/config/config_test.php new file mode 100644 index 0000000000..7c098af004 --- /dev/null +++ b/tests/console/config/config_test.php @@ -0,0 +1,251 @@ +<?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\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; + +class phpbb_console_command_config_test extends phpbb_test_case +{ + protected $config; + protected $command_name; + protected $user; + + public function setUp() + { + $this->config = new \phpbb\config\config(array()); + + $this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); + $this->user->method('lang')->will($this->returnArgument(0)); + } + + public function test_set_dynamic() + { + $this->assertEmpty($this->config); + + $command_tester = $this->get_command_tester('set'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'test_key', + 'value' => 'test_value', + '--dynamic' => true, + )); + + $this->assertSame($this->config['test_key'], 'test_value'); + } + + public function test_set_no_dynamic() + { + $this->assertEmpty($this->config); + + $command_tester = $this->get_command_tester('set'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'test_key', + 'value' => 'test_value', + '--dynamic' => false, + )); + + $this->assertSame($this->config['test_key'], 'test_value'); + } + + public function test_set_atomic_dynamic() + { + $this->assertEmpty($this->config); + + $this->config->set('test_key', 'old_value', true); + $this->assertSame($this->config['test_key'], 'old_value'); + + $command_tester = $this->get_command_tester('set_atomic'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'test_key', + 'old' => 'old_value', + 'new' => 'new_value', + '--dynamic' => true, + )); + + $this->assertSame($this->config['test_key'], 'new_value'); + } + + public function test_set_atomic_no_dynamic() + { + $this->assertEmpty($this->config); + + $this->config->set('test_key', 'old_value', false); + $this->assertSame($this->config['test_key'], 'old_value'); + + $command_tester = $this->get_command_tester('set_atomic'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'test_key', + 'old' => 'old_value', + 'new' => 'new_value', + '--dynamic' => false, + )); + + $this->assertSame($this->config['test_key'], 'new_value'); + } + + public function test_set_atomic_error_dynamic() + { + $this->assertEmpty($this->config); + + $this->config->set('test_key', 'wrong_value', true); + $this->assertSame($this->config['test_key'], 'wrong_value'); + + $command_tester = $this->get_command_tester('set_atomic'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'test_key', + 'old' => 'old_value', + 'new' => 'new_value', + '--dynamic' => true, + )); + + $this->assertSame($this->config['test_key'], 'wrong_value'); + } + + public function test_get_no_new_line() + { + $this->config->set('test_key', 'test_value', false); + $this->assertSame($this->config['test_key'], 'test_value'); + + $command_tester = $this->get_command_tester('get'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'test_key', + '--no-newline' => true, + )); + + $this->assertSame($this->config['test_key'], $command_tester->getDisplay()); + } + + public function test_get_new_line() + { + $this->config->set('test_key', 'test_value', false); + $this->assertSame($this->config['test_key'], 'test_value'); + + $command_tester = $this->get_command_tester('get'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'test_key', + '--no-newline' => false, + )); + + $this->assertSame($this->config['test_key'] . PHP_EOL, $command_tester->getDisplay()); + } + + public function test_get_error() + { + $this->config->set('test_key', 'test_value', false); + $this->assertSame($this->config['test_key'], 'test_value'); + + $command_tester = $this->get_command_tester('get'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'wrong_key', + '--no-newline' => false, + )); + + $this->assertContains('CLI_CONFIG_NOT_EXISTS', $command_tester->getDisplay()); + } + + public function test_increment_dynamic() + { + $this->config->set('test_key', 0, false); + $this->assertSame($this->config['test_key'], 0); + + $command_tester = $this->get_command_tester('increment'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'test_key', + 'increment' => 2, + '--dynamic' => true, + )); + + $this->assertContains('CLI_CONFIG_INCREMENT_SUCCESS', $command_tester->getDisplay()); + $this->assertSame(2, $this->config['test_key']); + } + + public function test_increment_no_dynamic() + { + $this->config->set('test_key', 0, false); + $this->assertSame($this->config['test_key'], 0); + + $command_tester = $this->get_command_tester('increment'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'test_key', + 'increment' => 2, + '--dynamic' => false, + )); + + $this->assertContains('CLI_CONFIG_INCREMENT_SUCCESS', $command_tester->getDisplay()); + $this->assertSame(2, $this->config['test_key']); + } + + public function test_increment_no_set() + { + $this->assertEmpty($this->config); + + $command_tester = $this->get_command_tester('increment'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'test_key', + 'increment' => 2, + '--dynamic' => true, + )); + + $this->assertContains('CLI_CONFIG_INCREMENT_SUCCESS', $command_tester->getDisplay()); + $this->assertSame(2, $this->config['test_key']); + } + + public function test_delete_ok() + { + $this->config->set('test_key', 'test_value', false); + $this->assertSame($this->config['test_key'], 'test_value'); + + $command_tester = $this->get_command_tester('delete'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'test_key', + )); + + $this->assertContains('CLI_CONFIG_DELETE_SUCCESS', $command_tester->getDisplay()); + $this->assertEmpty($this->config); + } + + public function test_delete_error() + { + $this->assertEmpty($this->config); + + $command_tester = $this->get_command_tester('delete'); + $command_tester->execute(array( + 'command' => $this->command_name, + 'key' => 'wrong_key', + )); + + $this->assertContains('CLI_CONFIG_NOT_EXISTS', $command_tester->getDisplay()); + $this->assertEmpty($this->config); + } + + public function get_command_tester($class_name) + { + $command_complete_name = '\phpbb\console\command\config' . '\\' . $class_name; + $application = new Application(); + $application->add(new $command_complete_name($this->user, $this->config)); + $command = $application->find('config:' . $this->command_name); + $this->command_name = $command->getName(); + return new CommandTester($command); + } +} diff --git a/tests/console/cron/cron_list_test.php b/tests/console/cron/cron_list_test.php new file mode 100644 index 0000000000..22423304be --- /dev/null +++ b/tests/console/cron/cron_list_test.php @@ -0,0 +1,103 @@ +<?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. +* +*/ + +require_once dirname(__FILE__) . '/tasks/simple_ready.php'; +require_once dirname(__FILE__) . '/tasks/simple_not_ready.php'; + +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use phpbb\console\command\cron\cron_list; + +class phpbb_console_command_cron_list_test extends phpbb_test_case +{ + /** @var \phpbb\cron\manager */ + protected $cron_manager; + + /** @var \phpbb\user */ + protected $user; + + protected $command_name; + + protected $command_tester; + + protected function setUp() + { + $this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); + $this->user->method('lang')->will($this->returnArgument(0)); + } + + public function test_no_task() + { + $this->initiate_test(0, 0); + $this->assertContains('CRON_NO_TASKS', $this->command_tester->getDisplay()); + } + + public function test_only_ready() + { + $this->initiate_test(2, 0); + $this->assertContains('TASKS_READY command1 command2', preg_replace('/\s+/', ' ', trim($this->command_tester->getDisplay()))); + } + + public function test_only_not_ready() + { + $this->initiate_test(0, 2); + $this->assertContains('TASKS_NOT_READY command1 command2', preg_replace('/\s+/', ' ', trim($this->command_tester->getDisplay()))); + } + + public function test_both_ready() + { + $this->initiate_test(2, 2); + $this->assertSame('TASKS_READY command1 command2 TASKS_NOT_READY command3 command4', preg_replace('/\s+/', ' ', trim($this->command_tester->getDisplay()))); + } + + public function get_cron_manager(array $tasks) + { + global $pathEx, $phpbb_root_path; + $i = 1; + foreach ($tasks as $task) + { + $task->set_name('command' . $i); + $i++; + } + $this->cron_manager = new \phpbb\cron\manager($tasks, $phpbb_root_path, $pathEx); + } + + public function get_command_tester() + { + $application = new Application(); + $application->add(new cron_list($this->user, $this->cron_manager)); + + $command = $application->find('cron:list'); + $this->command_name = $command->getName(); + return new CommandTester($command); + } + + public function initiate_test($number_ready, $number_not_ready) + { + $tasks = array(); + + for ($i = 0; $i < $number_ready; $i++) + { + $tasks[] = new phpbb_cron_task_simple_ready(); + } + + for ($i = 0; $i < $number_not_ready; $i++) + { + $tasks[] = new phpbb_cron_task_simple_not_ready(); + } + + $this->get_cron_manager($tasks); + $this->command_tester = $this->get_command_tester(); + $this->command_tester->execute(array('command' => $this->command_name), array('decorated' => false)); + } +} diff --git a/tests/console/cron/fixtures/config.xml b/tests/console/cron/fixtures/config.xml new file mode 100644 index 0000000000..2cb683d409 --- /dev/null +++ b/tests/console/cron/fixtures/config.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_config"> + <column>config_name</column> + <column>config_value</column> + <column>is_dynamic</column> + </table> +</dataset> diff --git a/tests/console/cron/run_test.php b/tests/console/cron/run_test.php new file mode 100644 index 0000000000..f76e967484 --- /dev/null +++ b/tests/console/cron/run_test.php @@ -0,0 +1,158 @@ +<?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\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use phpbb\console\command\cron\run; + +require_once dirname(__FILE__) . '/tasks/simple.php'; +require_once dirname(__FILE__) . '/../../../phpBB/includes/functions.php'; + +class phpbb_console_command_cron_run_test extends phpbb_database_test_case +{ + protected $db; + protected $config; + protected $lock; + protected $user; + protected $cron_manager; + protected $command_name; + protected $task; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); + } + + public function setUp() + { + global $db, $config, $phpbb_root_path, $phpEx; + + $db = $this->db = $this->new_dbal(); + $config = $this->config = new \phpbb\config\config(array('cron_lock' => '0')); + set_config(null, null, null, $this->config); + $this->lock = new \phpbb\lock\db('cron_lock', $this->config, $this->db); + + $this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); + $this->user->method('lang')->will($this->returnArgument(0)); + + $this->task = new phpbb_cron_task_simple(); + $tasks = array( + $this->task, + ); + $this->cron_manager = new \phpbb\cron\manager($tasks, $phpbb_root_path, $phbEx); + + $this->assertSame('0', $config['cron_lock']); + } + + public function test_normal_use() + { + $command_tester = $this->get_command_tester(); + $exit_status = $command_tester->execute(array('command' => $this->command_name)); + + $this->assertSame('', $command_tester->getDisplay()); + $this->assertSame(true, $this->task->executed); + $this->assertSame(0, $exit_status); + $this->assertSame(false, $this->lock->owns_lock()); + } + + public function test_verbose_mode() + { + $command_tester = $this->get_command_tester(); + $exit_status = $command_tester->execute(array('command' => $this->command_name, '--verbose' => true)); + + $this->assertContains('RUNNING_TASK', $command_tester->getDisplay()); + $this->assertSame(true, $this->task->executed); + $this->assertSame(0, $exit_status); + $this->assertSame(false, $this->lock->owns_lock()); + } + + public function test_error_lock() + { + $this->lock->acquire(); + $command_tester = $this->get_command_tester(); + $exit_status = $command_tester->execute(array('command' => $this->command_name)); + + $this->assertContains('CRON_LOCK_ERROR', $command_tester->getDisplay()); + $this->assertSame(false, $this->task->executed); + $this->assertSame(1, $exit_status); + } + + public function test_no_task() + { + $tasks = array( + ); + $this->cron_manager = new \phpbb\cron\manager($tasks, $phpbb_root_path, $phpEx); + $command_tester = $this->get_command_tester(); + $exit_status = $command_tester->execute(array('command' => $this->command_name)); + + $this->assertSame('', $command_tester->getDisplay()); + $this->assertSame(0, $exit_status); + $this->assertSame(false, $this->lock->owns_lock()); + } + + public function test_no_task_verbose() + { + $tasks = array( + ); + $this->cron_manager = new \phpbb\cron\manager($tasks, $phpbb_root_path, $phpEx); + $command_tester = $this->get_command_tester(); + $exit_status = $command_tester->execute(array('command' => $this->command_name, '--verbose' => true)); + + $this->assertContains('CRON_NO_TASK', $command_tester->getDisplay()); + $this->assertSame(0, $exit_status); + $this->assertSame(false, $this->lock->owns_lock()); + } + + public function test_arg_valid() + { + $command_tester = $this->get_command_tester(); + $exit_status = $command_tester->execute(array('command' => $this->command_name, 'name' => 'phpbb_cron_task_simple')); + + $this->assertSame('', $command_tester->getDisplay()); + $this->assertSame(true, $this->task->executed); + $this->assertSame(0, $exit_status); + $this->assertSame(false, $this->lock->owns_lock()); + } + + public function test_arg_invalid() + { + $command_tester = $this->get_command_tester(); + $exit_status = $command_tester->execute(array('command' => $this->command_name, 'name' => 'foo')); + + $this->assertContains('CRON_NO_SUCH_TASK', $command_tester->getDisplay()); + $this->assertSame(false, $this->task->executed); + $this->assertSame(2, $exit_status); + $this->assertSame(false, $this->lock->owns_lock()); + } + + public function test_arg_valid_verbose() + { + $command_tester = $this->get_command_tester(); + $exit_status = $command_tester->execute(array('command' => $this->command_name, 'name' => 'phpbb_cron_task_simple', '--verbose' => true)); + + $this->assertContains('RUNNING_TASK', $command_tester->getDisplay()); + $this->assertSame(true, $this->task->executed); + $this->assertSame(0, $exit_status); + $this->assertSame(false, $this->lock->owns_lock()); + } + + public function get_command_tester() + { + $application = new Application(); + $application->add(new run($this->user, $this->cron_manager, $this->lock)); + + $command = $application->find('cron:run'); + $this->command_name = $command->getName(); + return new CommandTester($command); + } +} diff --git a/tests/console/cron/tasks/simple.php b/tests/console/cron/tasks/simple.php new file mode 100644 index 0000000000..194c52afe1 --- /dev/null +++ b/tests/console/cron/tasks/simple.php @@ -0,0 +1,27 @@ +<?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_cron_task_simple extends \phpbb\cron\task\base +{ + public $executed = false; + + public function get_name() + { + return get_class($this); + } + + public function run() + { + $this->executed = true; + } +} diff --git a/tests/console/cron/tasks/simple_not_ready.php b/tests/console/cron/tasks/simple_not_ready.php new file mode 100644 index 0000000000..887768e5fe --- /dev/null +++ b/tests/console/cron/tasks/simple_not_ready.php @@ -0,0 +1,13 @@ +<?php + +class phpbb_cron_task_simple_not_ready extends \phpbb\cron\task\base +{ + public function run() + { + } + + public function should_run() + { + return false; + } +} diff --git a/tests/console/cron/tasks/simple_ready.php b/tests/console/cron/tasks/simple_ready.php new file mode 100644 index 0000000000..47970e104f --- /dev/null +++ b/tests/console/cron/tasks/simple_ready.php @@ -0,0 +1,8 @@ +<?php + +class phpbb_cron_task_simple_ready extends \phpbb\cron\task\base +{ + public function run() + { + } +} |