aboutsummaryrefslogtreecommitdiffstats
path: root/tests/extension/finder_test.php
blob: 622f404786566701bd921deb467cdaa2e70ba5e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
*
* @package testing
* @copyright (c) 2011 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

class phpbb_extension_finder_test extends phpbb_test_case
{
	protected $extension_manager;
	protected $finder;

	public function setUp()
	{
		$this->extension_manager = new phpbb_mock_extension_manager(
			dirname(__FILE__) . '/',
			array(
				'foo' => array(
					'ext_name' => 'foo',
					'ext_active' => '1',
					'ext_path' => 'ext/foo/',
				),
				'bar' => array(
					'ext_name' => 'bar',
					'ext_active' => '1',
					'ext_path' => 'ext/bar/',
				),
			));

		$this->finder = $this->extension_manager->get_finder();
	}

	public function test_suffix_get_classes()
	{
		$classes = $this->finder
			->core_path('includes/default/')
			->extension_suffix('_class')
			->get_classes();

		sort($classes);
		$this->assertEquals(
			array(
				'phpbb_default_implementation',
				'phpbb_ext_bar_my_hidden_class',
				'phpbb_ext_foo_a_class',
				'phpbb_ext_foo_b_class',
			),
			$classes
		);
	}

	public function test_get_directories()
	{
		$dirs = $this->finder
			->directory('/type')
			->get_directories();

		sort($dirs);
		$this->assertEquals(array(
			dirname(__FILE__) . '/ext/foo/type/',
		), $dirs);
	}

	public function test_prefix_get_directories()
	{
		$dirs = $this->finder
            ->prefix('t')
			->get_directories();

		sort($dirs);
		$this->assertEquals(array(
			dirname(__FILE__) . '/ext/foo/sub/type/',
			dirname(__FILE__) . '/ext/foo/type/',
			dirname(__FILE__) . '/ext/foo/typewrong/',
		), $dirs);
	}

	public function test_prefix_get_classes()
	{
		$classes = $this->finder
			->core_path('includes/default/')
			->extension_prefix('hidden_')
			->get_classes();

		sort($classes);
		$this->assertEquals(
			array(
				'phpbb_default_implementation',
				'phpbb_ext_bar_my_hidden_class',
			),
			$classes
		);
	}

	public function test_directory_get_classes()
	{
		$classes = $this->finder
			->core_path('includes/default/')
			->extension_directory('type')
			->get_classes();

		sort($classes);
		$this->assertEquals(
			array(
				'phpbb_default_implementation',
				'phpbb_ext_foo_sub_type_alternative',
				'phpbb_ext_foo_type_alternative',
			),
			$classes
		);
	}

	public function test_absolute_directory_get_classes()
	{
		$classes = $this->finder
			->directory('/type/')
			->get_classes();

		sort($classes);
		$this->assertEquals(
			array(
				'phpbb_ext_foo_type_alternative',
			),
			$classes
		);
	}

	public function test_sub_directory_get_classes()
	{
		$classes = $this->finder
			->directory('/sub/type')
			->get_classes();

		sort($classes);
		$this->assertEquals(
			array(
				'phpbb_ext_foo_sub_type_alternative',
			),
			$classes
		);
	}

	public function test_get_classes_create_cache()
	{
		$cache = new phpbb_mock_cache;
		$finder = new phpbb_extension_finder($this->extension_manager, dirname(__FILE__) . '/', $cache, '.php', '_custom_cache_name');
		$files = $finder->suffix('_class.php')->get_files();

		$expected_files = array(
			'ext/bar/my/hidden_class.php' => 'bar',
			'ext/foo/a_class.php' => 'foo',
			'ext/foo/b_class.php' => 'foo',
		);

		$query = array(
			'core_path' => false,
			'core_suffix' => '_class.php',
			'core_prefix' => false,
			'core_directory' => false,
			'extension_suffix' => '_class.php',
			'extension_prefix' => false,
			'extension_directory' => false,
			'is_dir' => false,
		);

		$cache->checkAssociativeVar($this, '_custom_cache_name', array(
			md5(serialize($query)) => $expected_files,
		), false);
	}

	public function test_cached_get_files()
	{
		$query = array(
			'core_path' => 'includes/foo',
			'core_suffix' => false,
			'core_prefix' => false,
			'core_directory' => 'bar',
			'extension_suffix' => false,
			'extension_prefix' => false,
			'extension_directory' => false,
			'is_dir' => false,
		);

		$finder = new phpbb_extension_finder($this->extension_manager, dirname(__FILE__) . '/', new phpbb_mock_cache(array(
			'_ext_finder' => array(
				md5(serialize($query)) => array('file_name' => 'extension'),
			),
		)));

		$classes = $finder
			->core_path($query['core_path'])
			->core_directory($query['core_directory'])
			->get_files();

		sort($classes);
		$this->assertEquals(
			array(dirname(__FILE__) . '/file_name'),
			$classes
		);
	}
}