Skip to content

Commit 9d203d6

Browse files
committed
Merge pull request #22 from VSEphpbb/testing
Add event listener unit tests
2 parents 8e8cbd5 + ee7bfd2 commit 9d203d6

2 files changed

Lines changed: 328 additions & 0 deletions

File tree

tests/event/listener_test.php

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
<?php
2+
/**
3+
*
4+
* Google Analytics extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\googleanalytics\tests\event;
12+
13+
class event_listener_test extends \phpbb_test_case
14+
{
15+
/** @var \phpbb\googleanalytics\event\listener */
16+
protected $listener;
17+
18+
/**
19+
* Setup test environment
20+
*
21+
* @access public
22+
*/
23+
public function setUp()
24+
{
25+
parent::setUp();
26+
27+
global $phpbb_dispatcher, $phpbb_extension_manager, $phpbb_root_path;
28+
29+
// Mock some global classes that may be called during code execution
30+
$phpbb_dispatcher = new \phpbb_mock_event_dispatcher();
31+
$phpbb_extension_manager = new \phpbb_mock_extension_manager($phpbb_root_path);
32+
33+
// Load/Mock classes required by the event listener class
34+
$this->config = new \phpbb\config\config(array('googleanalytics_id' => 'UA-000000-01'));
35+
$this->template = new \phpbb\googleanalytics\tests\mock\template();
36+
$this->user = new \phpbb\user('\phpbb\datetime');
37+
}
38+
39+
/**
40+
* Create our event listener
41+
*
42+
* @access protected
43+
*/
44+
protected function set_listener()
45+
{
46+
$this->listener = new \phpbb\googleanalytics\event\listener(
47+
$this->config,
48+
$this->template,
49+
$this->user
50+
);
51+
}
52+
53+
/**
54+
* Test the event listener is constructed correctly
55+
*
56+
* @access public
57+
*/
58+
public function test_construct()
59+
{
60+
$this->set_listener();
61+
$this->assertInstanceOf('\Symfony\Component\EventDispatcher\EventSubscriberInterface', $this->listener);
62+
}
63+
64+
/**
65+
* Test the event listener is subscribing events
66+
*
67+
* @access public
68+
*/
69+
public function test_getSubscribedEvents()
70+
{
71+
$this->assertEquals(array(
72+
'core.acp_board_config_edit_add',
73+
'core.page_header',
74+
'core.validate_config_variable',
75+
), array_keys(\phpbb\googleanalytics\event\listener::getSubscribedEvents()));
76+
}
77+
78+
/**
79+
* Test the load_google_analytics event
80+
*
81+
* @access public
82+
*/
83+
public function test_load_google_analytics()
84+
{
85+
$this->set_listener();
86+
87+
$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
88+
$dispatcher->addListener('core.page_header', array($this->listener, 'load_google_analytics'));
89+
$dispatcher->dispatch('core.page_header');
90+
91+
$this->assertEquals(array(
92+
'GOOGLEANALYTICS_ID' => $this->config['googleanalytics_id'],
93+
), $this->template->get_template_vars());
94+
}
95+
96+
/**
97+
* Data set for test_add_googleanalytics_configs
98+
*
99+
* @return array Array of test data
100+
* @access public
101+
*/
102+
public function add_googleanalytics_configs_data()
103+
{
104+
return array(
105+
array( // expected config and mode
106+
'settings',
107+
array('vars' => array('override_user_style' => array())),
108+
array('override_user_style', 'googleanalytics_id'),
109+
),
110+
array( // unexpected mode
111+
'foobar',
112+
array('vars' => array('override_user_style' => array())),
113+
array('override_user_style'),
114+
),
115+
array( // unexpected config
116+
'settings',
117+
array('vars' => array('foobar' => array())),
118+
array('foobar'),
119+
),
120+
array( // unexpected config and mode
121+
'foobar',
122+
array('vars' => array('foobar' => array())),
123+
array('foobar'),
124+
),
125+
);
126+
}
127+
128+
/**
129+
* Test the add_googleanalytics_configs event
130+
*
131+
* @dataProvider add_googleanalytics_configs_data
132+
* @access public
133+
*/
134+
public function test_add_googleanalytics_configs($mode, $display_vars, $expected_keys)
135+
{
136+
$this->set_listener();
137+
138+
$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
139+
$dispatcher->addListener('core.acp_board_config_edit_add', array($this->listener, 'add_googleanalytics_configs'));
140+
141+
$event_data = array('display_vars', 'mode');
142+
$event = new \phpbb\event\data(compact($event_data));
143+
$dispatcher->dispatch('core.acp_board_config_edit_add', $event);
144+
145+
$event_data_after = $event->get_data_filtered($event_data);
146+
foreach ($event_data as $expected)
147+
{
148+
$this->assertArrayHasKey($expected, $event_data_after);
149+
}
150+
extract($event_data_after);
151+
152+
$keys = array_keys($display_vars['vars']);
153+
154+
$this->assertEquals($expected_keys, $keys);
155+
}
156+
157+
/**
158+
* Data set for test_validate_googleanalytics_id
159+
*
160+
* @return array Array of test data
161+
* @access public
162+
*/
163+
public function validate_googleanalytics_id_data()
164+
{
165+
return array(
166+
array(
167+
// valid code, no error
168+
array('googleanalytics_id' => 'UA-0000-00'),
169+
array(),
170+
),
171+
array(
172+
// no code, no error
173+
array('googleanalytics_id' => ''),
174+
array(),
175+
),
176+
array(
177+
// invalid code, error
178+
array('googleanalytics_id' => 'UA-00-00'),
179+
array('ACP_GOOGLEANALYTICS_ID_INVALID'),
180+
),
181+
array(
182+
// invalid code, error
183+
array('googleanalytics_id' => 'UA-00000-00000'),
184+
array('ACP_GOOGLEANALYTICS_ID_INVALID'),
185+
),
186+
array(
187+
// invalid code, error
188+
array('googleanalytics_id' => 'AU-0000-00'),
189+
array('ACP_GOOGLEANALYTICS_ID_INVALID'),
190+
),
191+
array(
192+
// invalid code, error
193+
array('googleanalytics_id' => 'foo-bar-foo'),
194+
array('ACP_GOOGLEANALYTICS_ID_INVALID'),
195+
),
196+
);
197+
}
198+
199+
/**
200+
* Test the validate_googleanalytics_id event
201+
*
202+
* @dataProvider validate_googleanalytics_id_data
203+
* @access public
204+
*/
205+
public function test_validate_googleanalytics_id($cfg_array, $expected_error)
206+
{
207+
$this->set_listener();
208+
209+
$config_name = 'googleanalytics_id';
210+
$config_definition = array('validate' => 'googleanalytics_id');
211+
$error = array();
212+
213+
$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
214+
$dispatcher->addListener('core.validate_config_variable', array($this->listener, 'validate_googleanalytics_id'));
215+
216+
$event_data = array('cfg_array', 'config_name', 'config_definition', 'error');
217+
$event = new \phpbb\event\data(compact($event_data));
218+
$dispatcher->dispatch('core.validate_config_variable', $event);
219+
220+
$event_data_after = $event->get_data_filtered($event_data);
221+
foreach ($event_data as $expected)
222+
{
223+
$this->assertArrayHasKey($expected, $event_data_after);
224+
}
225+
extract($event_data_after);
226+
227+
$this->assertEquals($expected_error, $error);
228+
}
229+
}

tests/mock/template.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
*
4+
* Google Analytics extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\googleanalytics\tests\mock;
12+
13+
/**
14+
* Mock template class.
15+
* This class has a minimum amount of functionality, just to make tests work.
16+
* (Credit to nickvergessen for desigining this mock class.)
17+
*/
18+
class template implements \phpbb\template\template
19+
{
20+
protected $template_data;
21+
22+
public function __construct()
23+
{
24+
}
25+
26+
public function get_template_vars()
27+
{
28+
return $this->template_data;
29+
}
30+
31+
public function clear_cache()
32+
{
33+
}
34+
35+
public function set_filenames(array $filename_array)
36+
{
37+
}
38+
39+
public function get_user_style()
40+
{
41+
}
42+
43+
public function set_style($style_directories = array('styles'))
44+
{
45+
}
46+
47+
public function set_custom_style($names, $paths)
48+
{
49+
}
50+
51+
public function destroy()
52+
{
53+
}
54+
55+
public function destroy_block_vars($blockname)
56+
{
57+
}
58+
59+
public function display($handle)
60+
{
61+
}
62+
63+
public function assign_display($handle, $template_var = '', $return_content = true)
64+
{
65+
}
66+
67+
public function assign_vars(array $vararray)
68+
{
69+
foreach ($vararray as $varname => $varval)
70+
{
71+
$this->assign_var($varname, $varval);
72+
}
73+
}
74+
75+
public function assign_var($varname, $varval)
76+
{
77+
$this->template_data[$varname] = $varval;
78+
}
79+
80+
public function append_var($varname, $varval)
81+
{
82+
}
83+
84+
public function assign_block_vars($blockname, array $vararray)
85+
{
86+
}
87+
88+
public function assign_block_vars_array($blockname, array $block_vars_array)
89+
{
90+
}
91+
92+
public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert')
93+
{
94+
}
95+
96+
public function get_source_file_for_handle($handle)
97+
{
98+
}
99+
}

0 commit comments

Comments
 (0)