Skip to content

Commit 0559079

Browse files
committed
Add event listener unit tests
1 parent de6a709 commit 0559079

2 files changed

Lines changed: 327 additions & 0 deletions

File tree

tests/event/listener_test.php

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

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)