Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.24% covered (success)
95.24%
140 / 147
81.82% covered (warning)
81.82%
9 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_console_command_cron_run_test
95.24% covered (success)
95.24%
140 / 147
81.82% covered (warning)
81.82%
9 / 11
11
0.00% covered (danger)
0.00%
0 / 1
 getDataSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUp
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
1
 test_normal_use
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 test_verbose_mode
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 test_error_lock
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
1.05
 test_no_task
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
1
 test_no_task_verbose
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
1
 test_arg_valid
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 test_arg_invalid
50.00% covered (danger)
50.00%
4 / 8
0.00% covered (danger)
0.00%
0 / 1
1.12
 test_arg_valid_verbose
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 get_command_tester
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14use Symfony\Component\Console\Application;
15use Symfony\Component\Console\Tester\CommandTester;
16use phpbb\console\command\cron\run;
17
18require_once __DIR__ . '/tasks/simple.php';
19
20class phpbb_console_command_cron_run_test extends phpbb_database_test_case
21{
22    protected $db;
23    protected $config;
24    protected $lock;
25    protected $user;
26    protected $cron_manager;
27    protected $task;
28
29    public function getDataSet()
30    {
31        return $this->createXMLDataSet(__DIR__ . '/fixtures/config.xml');
32    }
33
34    protected function setUp(): void
35    {
36        global $db, $config, $phpbb_root_path, $phpEx;
37
38        $db = $this->db = $this->new_dbal();
39        $config = $this->config = new \phpbb\config\config(array('cron_lock' => '0'));
40        $this->lock = new \phpbb\lock\db('cron_lock', $this->config, $this->db);
41
42        $this->user = $this->createMock('\phpbb\user');
43        $this->user->method('lang')->will($this->returnArgument(0));
44
45        $this->task = new phpbb_cron_task_simple();
46        $tasks = array(
47            $this->task,
48        );
49
50        $mock_config = new \phpbb\config\config(array(
51            'force_server_vars' => false,
52            'enable_mod_rewrite' => '',
53        ));
54
55        $mock_router = $this->getMockBuilder('\phpbb\routing\router')
56            ->onlyMethods(array('setContext', 'generate'))
57            ->disableOriginalConstructor()
58            ->getMock();
59        $mock_router->method('generate')
60            ->willReturn('foobar');
61
62        $request = new \phpbb\request\request();
63        $request->enable_super_globals();
64
65        $routing_helper = new \phpbb\routing\helper(
66            $mock_config,
67            $mock_router,
68            new \phpbb\symfony_request($request),
69            $request,
70            $phpbb_root_path,
71            $phpEx
72        );
73
74        $mock_container = new phpbb_mock_container_builder();
75        $task_collection = new \phpbb\di\service_collection($mock_container);
76        $mock_container->set('cron.task_collection', $task_collection);
77
78        $this->cron_manager = new \phpbb\cron\manager($mock_container, $routing_helper, $phpbb_root_path, $phpEx, null);
79        $this->cron_manager->load_tasks($tasks);
80
81        $this->assertSame('0', $config['cron_lock']);
82    }
83
84    public function test_normal_use()
85    {
86        $command_tester = $this->get_command_tester();
87        $exit_status = $command_tester->execute([]);
88
89        $this->assertSame('', $command_tester->getDisplay());
90        $this->assertSame(true, $this->task->executed);
91        $this->assertSame(0, $exit_status);
92        $this->assertSame(false, $this->lock->owns_lock());
93    }
94
95    public function test_verbose_mode()
96    {
97        $command_tester = $this->get_command_tester();
98        $exit_status = $command_tester->execute(array('--verbose' => true));
99
100        $this->assertStringContainsString('RUNNING_TASK', $command_tester->getDisplay());
101        $this->assertSame(true, $this->task->executed);
102        $this->assertSame(0, $exit_status);
103        $this->assertSame(false, $this->lock->owns_lock());
104    }
105
106    public function test_error_lock()
107    {
108        $this->expectException(\phpbb\exception\runtime_exception::class);
109        $this->expectExceptionMessage('CRON_LOCK_ERROR');
110
111        $this->lock->acquire();
112        $command_tester = $this->get_command_tester();
113        $exit_status = $command_tester->execute([]);
114
115        $this->assertStringContainsString('CRON_LOCK_ERROR', $command_tester->getDisplay());
116        $this->assertSame(false, $this->task->executed);
117        $this->assertSame(1, $exit_status);
118    }
119
120    public function test_no_task()
121    {
122        global $phpbb_root_path, $phpEx;
123
124        $tasks = array(
125        );
126
127        $mock_config = new \phpbb\config\config(array(
128            'force_server_vars' => false,
129            'enable_mod_rewrite' => '',
130        ));
131
132        $mock_router = $this->getMockBuilder('\phpbb\routing\router')
133            ->onlyMethods(array('setContext', 'generate'))
134            ->disableOriginalConstructor()
135            ->getMock();
136        $mock_router->method('generate')
137            ->willReturn('foobar');
138
139        $request = new \phpbb\request\request();
140        $request->enable_super_globals();
141
142        $routing_helper = new \phpbb\routing\helper(
143            $mock_config,
144            $mock_router,
145            new \phpbb\symfony_request($request),
146            $request,
147            $phpbb_root_path,
148            $phpEx
149        );
150
151        $mock_container = new phpbb_mock_container_builder();
152        $task_collection = new \phpbb\di\service_collection($mock_container);
153        $mock_container->set('cron.task_collection', $task_collection);
154
155        $this->cron_manager = new \phpbb\cron\manager($mock_container, $routing_helper, $phpbb_root_path, $phpEx, null);
156        $this->cron_manager->load_tasks($tasks);
157
158        $command_tester = $this->get_command_tester();
159        $exit_status = $command_tester->execute([]);
160
161        $this->assertSame('', $command_tester->getDisplay());
162        $this->assertSame(0, $exit_status);
163        $this->assertSame(false, $this->lock->owns_lock());
164    }
165
166    public function test_no_task_verbose()
167    {
168        global $phpbb_root_path, $phpEx;
169
170        $tasks = array(
171        );
172
173        $mock_config = new \phpbb\config\config(array(
174            'force_server_vars' => false,
175            'enable_mod_rewrite' => '',
176        ));
177
178        $mock_router = $this->getMockBuilder('\phpbb\routing\router')
179            ->onlyMethods(array('setContext', 'generate'))
180            ->disableOriginalConstructor()
181            ->getMock();
182        $mock_router->method('generate')
183            ->willReturn('foobar');
184
185        $request = new \phpbb\request\request();
186        $request->enable_super_globals();
187
188        $routing_helper = new \phpbb\routing\helper(
189            $mock_config,
190            $mock_router,
191            new \phpbb\symfony_request($request),
192            $request,
193            $phpbb_root_path,
194            $phpEx
195        );
196
197        $mock_container = new phpbb_mock_container_builder();
198        $task_collection = new \phpbb\di\service_collection($mock_container);
199        $mock_container->set('cron.task_collection', $task_collection);
200
201        $this->cron_manager = new \phpbb\cron\manager($mock_container, $routing_helper, $phpbb_root_path, $phpEx, null);
202        $this->cron_manager->load_tasks($tasks);
203
204        $command_tester = $this->get_command_tester();
205        $exit_status = $command_tester->execute(array('--verbose' => true));
206
207        $this->assertStringContainsString('CRON_NO_TASK', $command_tester->getDisplay());
208        $this->assertSame(0, $exit_status);
209        $this->assertSame(false, $this->lock->owns_lock());
210    }
211
212    public function test_arg_valid()
213    {
214        $command_tester = $this->get_command_tester();
215        $exit_status = $command_tester->execute(array('name' => 'phpbb_cron_task_simple'));
216
217        $this->assertSame('', $command_tester->getDisplay());
218        $this->assertSame(true, $this->task->executed);
219        $this->assertSame(0, $exit_status);
220        $this->assertSame(false, $this->lock->owns_lock());
221    }
222
223    public function test_arg_invalid()
224    {
225        $this->expectException(\phpbb\exception\runtime_exception::class);
226        $this->expectExceptionMessage('CRON_NO_SUCH_TASK');
227
228        $command_tester = $this->get_command_tester();
229        $exit_status = $command_tester->execute(array('name' => 'foo'));
230
231        $this->assertStringContainsString('CRON_NO_SUCH_TASK', $command_tester->getDisplay());
232        $this->assertSame(false, $this->task->executed);
233        $this->assertSame(2, $exit_status);
234        $this->assertSame(false, $this->lock->owns_lock());
235    }
236
237    public function test_arg_valid_verbose()
238    {
239        $command_tester = $this->get_command_tester();
240        $exit_status = $command_tester->execute(array('name' => 'phpbb_cron_task_simple', '--verbose' => true));
241
242        $this->assertStringContainsString('RUNNING_TASK', $command_tester->getDisplay());
243        $this->assertSame(true, $this->task->executed);
244        $this->assertSame(0, $exit_status);
245        $this->assertSame(false, $this->lock->owns_lock());
246    }
247
248    public function get_command_tester()
249    {
250        $application = new Application();
251        $application->add(new run($this->user, $this->cron_manager, $this->lock));
252
253        $command = $application->find('cron:run');
254        return new CommandTester($command);
255    }
256}