Completed
Push — 3.3.x ( 37d736...41194d )
by Erwan
02:32
created

controller_base::create_cron_manager()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 32
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 23
nc 1
nop 1
1
<?php
2
/**
3
*
4
* phpBB Directory extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace ernadoo\phpbbdirectory\tests\controller;
12
13
abstract class controller_base extends \phpbb_database_test_case
14
{
15
	/** @var \PHPUnit_Framework_MockObject_MockObject|\phpbb\auth\auth */
16
	protected $auth;
17
18
	/** @var \phpbb\config\config */
19
	protected $config;
20
21
	/** @var \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\DependencyInjection\ContainerInterface */
22
	protected $container;
23
24
	/** @var \PHPUnit_Framework_MockObject_MockObject|\phpbb\controller\helper */
25
	protected $helper;
26
27
	/** @var \phpbb\language\language */
28
	protected $lang;
29
30
	/** @var \PHPUnit_Framework_MockObject_MockObject|\phpbb\template\template */
31
	protected $template;
32
33
	/** @var \phpbb\user */
34
	protected $user;
35
36
	/**
37
	* Define the extensions to be tested
38
	*
39
	* @return array vendor/name of extension(s) to test
40
	*/
41
	static protected function setup_extensions()
42
	{
43
		return array('ernadoo/phpbbdirectory');
44
	}
45
46
	public function setUp()
47
	{
48
		global $cache, $phpbb_container, $phpbb_path_helper, $phpbb_extension_manager, $request, $user, $phpbb_root_path, $cron, $phpEx;
49
		global $phpbb_dispatcher, $auth, $config, $phpbb_filesystem, $template;
50
		global $table_categories, $tables_comments, $tables_links, $tables_votes, $tables_watch;
51
52
		parent::setUp();
53
54
		$table_categories	= 'phpbb_directory_cats';
55
		$tables_comments	= 'phpbb_directory_comments';
56
		$tables_links		= 'phpbb_directory_links';
57
		$tables_votes		= 'phpbb_directory_votes';
58
		$tables_watch		= 'phpbb_directory_watch';
59
60
		//Let's build some deps
61
		$auth = $this->auth = $this->createMock('\phpbb\auth\auth');
62
63
		$config = $this->config = new \phpbb\config\config(array());
64
65
		$db = $this->db = $this->new_dbal();
66
67
		$this->request = $this->createMock('\phpbb\request\request');
68
69
		$this->template = $this->getMockBuilder('\phpbb\template\template')->getMock();
70
71
		$request = new \phpbb_mock_request();
72
73
		$symfony_request = new \phpbb\symfony_request(
74
			$request
75
		);
76
77
		$phpbb_filesystem = $this->filesystem = new \phpbb\filesystem\filesystem();
78
79
		$phpbb_path_helper = $this->phpbb_path_helper = new \phpbb\path_helper(
80
			$symfony_request,
81
			$request,
82
			$phpbb_root_path,
83
			$phpEx
84
		);
85
86
		$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
87
		$this->lang = new \phpbb\language\language($lang_loader);
88
89
		$user = $this->user = new \phpbb\user($this->lang, '\phpbb\datetime');
90
		$this->user->timezone = new \DateTimeZone('UTC');
91
		$this->user->lang = array(
92
			'datetime' => array(),
93
			'DATE_FORMAT' => 'm/d/Y',
94
		);
95
96
		$this->helper = $this->getMockBuilder('\phpbb\controller\helper')
97
			->disableOriginalConstructor()
98
			->getMock();
99
100
		$this->helper->expects($this->any())
101
			->method('render')
102
			->willReturnCallback(function ($template_file, $page_title = '', $status_code = 200, $display_online_list = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $display_online_list is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
				return new \Symfony\Component\HttpFoundation\Response($template_file, $status_code);
104
		});
105
		$this->helper
106
			->method('route')
107
			->will($this->returnArgument(0));
108
109
		$cache = $this->cache = new \phpbb_mock_cache();
110
111
		$this->cache->purge();
112
113
		$phpbb_dispatcher = $this->dispatcher = new \phpbb_mock_event_dispatcher();
114
115
		$this->pagination = new \phpbb\pagination($this->template, $this->user, $this->helper, $phpbb_dispatcher);
116
117
		$this->user_loader = $this->getMockBuilder('\phpbb\user_loader')
118
			->disableOriginalConstructor()
119
		->getMock();
120
121
		$this->notification_helper = $this->getMockBuilder('\phpbb\notification\manager')
122
			->disableOriginalConstructor()
123
		->getMock();
124
125
		$phpbb_extension_manager = new \phpbb_mock_extension_manager($phpbb_root_path);
126
127
		$phpbb_container = new \phpbb_mock_container_builder();
128
		$phpbb_container->set('config', $config);
129
		$phpbb_container->set('filesystem', $phpbb_filesystem);
130
		$phpbb_container->set('path_helper', $phpbb_path_helper);
131
		$phpbb_container->set('ext.manager', $phpbb_extension_manager);
132
		$phpbb_container->set('user', $this->user);
133
		$phpbb_container->setParameter('core.cache_dir', $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/');
134
135
		$context = new \phpbb\template\context();
136
		$twig_extension = new \phpbb\template\twig\extension($context, $this->lang);
137
		$phpbb_container->set('template.twig.extensions.phpbb', $twig_extension);
138
139
		$twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container);
140
		$twig_extensions_collection->add('template.twig.extensions.phpbb');
141
		$phpbb_container->set('template.twig.extensions.collection', $twig_extensions_collection);
142
143
		$phpbb_container->set('ernadoo.phpbbdirectory.core.nestedset_category',
144
			new \ernadoo\phpbbdirectory\core\nestedset_category(
145
				$this->db,
146
				new \phpbb\lock\db(
147
					'ernadoo.phpbbdirectory.table_lock.directory_cats',
148
					$this->config,
149
					$this->db
150
				),
151
				$table_categories
152
			)
153
		);
154
155
		$imagesize = $this->getMockBuilder('\FastImageSize\FastImageSize')
156
			->getMock();
157
158
		$files = $this->getMockBuilder('\phpbb\files\factory')
159
			->disableOriginalConstructor()
160
			->getMock();
161
162
		$phpbb_log = new \phpbb\log\log($this->db, $this->user, $this->auth, $phpbb_dispatcher, $phpbb_root_path, 'adm/', $phpEx, LOG_TABLE);
163
164
		$this->core_link = new \ernadoo\phpbbdirectory\core\link(
165
			$this->db,
166
			$config,
167
			$this->lang,
168
			$this->template,
169
			$this->user,
170
			$this->helper,
171
			$this->request,
172
			$auth,
173
			$this->notification_helper,
174
			$this->filesystem,
175
			$imagesize,
176
			$files,
177
			$phpbb_root_path,
178
			$phpEx
179
		);
180
		$this->core_link->set_tables($table_categories, $tables_comments, $tables_links, $tables_votes, $tables_watch);
181
		$this->core_link->set_path_helper($phpbb_path_helper);
182
		$this->core_link->set_extension_manager($phpbb_extension_manager);
183
184
		$this->core_cron = new \ernadoo\phpbbdirectory\core\cron(
185
			$this->db,
186
			$this->config,
187
			$phpbb_log,
188
			$this->user,
189
			$this->notification_helper,
190
			$this->core_link,
191
			$phpbb_root_path,
192
			$phpEx
193
		);
194
		$this->core_cron->set_tables($table_categories, $tables_comments, $tables_links, $tables_votes, $tables_watch);
195
		$this->core_cron->set_path_helper($phpbb_path_helper);
196
		$this->core_cron->set_extension_manager($phpbb_extension_manager);
197
198
		$cron_task = new \ernadoo\phpbbdirectory\cron\task\core\prune_categorie(
199
			$this->config,
200
			$this->core_cron,
201
			$phpEx
202
		);
203
		$cron_task->set_name('ernadoo.phpbbdirectory.cron.task.core.prune_categorie');
204
205
		$this->cron = $this->create_cron_manager(array($cron_task));
206
207
		$this->core_categorie = new \ernadoo\phpbbdirectory\core\categorie(
208
			$db,
209
			$config,
210
			$this->lang,
211
			$this->template,
212
			$this->user,
213
			$this->helper,
214
			$this->request,
215
			$auth,
216
			$this->cron
217
		);
218
		$this->core_categorie->set_tables($table_categories, $tables_comments, $tables_links, $tables_votes, $tables_watch);
219
		$this->core_categorie->set_path_helper($phpbb_path_helper);
220
		$this->core_categorie->set_extension_manager($phpbb_extension_manager);
221
	}
222
223
	private function create_cron_manager($tasks)
224
	{
225
		global $phpbb_root_path, $phpEx;
226
227
		$mock_config = new \phpbb\config\config(array(
228
			'force_server_vars' => false,
229
			'enable_mod_rewrite' => '',
230
		));
231
232
		$mock_router = $this->getMockBuilder('\phpbb\routing\router')
233
		->setMethods(array('setContext', 'generate'))
234
		->disableOriginalConstructor()
235
		->getMock();
236
		$mock_router->method('setContext')
237
		->willReturn(true);
238
		$mock_router->method('generate')
239
		->willReturn('foobar');
240
241
		$request = new \phpbb\request\request();
242
		$request->enable_super_globals();
243
244
		$routing_helper = new \phpbb\routing\helper(
245
			$mock_config,
246
			$mock_router,
247
			new \phpbb\symfony_request($request),
248
			$request,
249
			$phpbb_root_path,
250
			$phpEx
251
			);
252
253
		return new \phpbb\cron\manager($tasks, $routing_helper, $phpbb_root_path, $phpEx);
254
	}
255
}
256