Completed
Push — master ( 20efb0...a2cc06 )
by Hamish
29s
created

testGetImportedNamespaceParser()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 24
rs 8.9713
1
<?php
2
/**
3
 * Tests for the {@link SS_ClassManifest} class.
4
 *
5
 * @package framework
6
 * @subpackage tests
7
 */
8
class NamespacedClassManifestTest extends SapphireTest {
9
10
	protected $base;
11
12
	/**
13
	 * @var SS_ClassManifest
14
	 */
15
	protected $manifest;
16
17
	public function setUp() {
18
		parent::setUp();
19
20
		$this->base = dirname(__FILE__) . '/fixtures/namespaced_classmanifest';
21
		$this->manifest      = new SS_ClassManifest($this->base, false, true, false);
22
		SS_ClassLoader::instance()->pushManifest($this->manifest, false);
23
	}
24
25
	public function tearDown() {
26
		parent::tearDown();
27
		SS_ClassLoader::instance()->popManifest();
28
	}
29
30
	public function testGetImportedNamespaceParser() {
31
		$file = file_get_contents($this->base . DIRECTORY_SEPARATOR . 'module/classes/ClassI.php');
32
		$tokens = token_get_all($file);
33
		$parsedTokens = SS_ClassManifest::get_imported_namespace_parser()->findAll($tokens);
34
35
		/** @skipUpgrade */
36
		$expectedItems = array(
37
			array('ModelAdmin'),
38
			array('Controller', '  ', 'as', '  ', 'Cont'),
39
			array(
40
				'SS_HTTPRequest', ' ', 'as', ' ', 'Request', ',',
41
				'SS_HTTPResponse', ' ', 'as', ' ', 'Response', ',',
42
				'SilverStripe', '\\', 'Security', '\\', 'PermissionProvider', ' ', 'as', ' ', 'P',
43
			),
44
			array('silverstripe', '\\', 'test', '\\', 'ClassA'),
45
			array('\\', 'Object'),
46
		);
47
48
		$this->assertEquals(count($expectedItems), count($parsedTokens));
49
50
		foreach ($expectedItems as $i => $item) {
51
			$this->assertEquals($item, $parsedTokens[$i]['importString']);
52
		}
53
	}
54
55
	public function testGetImportsFromTokens() {
56
		$file = file_get_contents($this->base . DIRECTORY_SEPARATOR . 'module/classes/ClassI.php');
57
		$tokens = token_get_all($file);
58
59
		$method = new ReflectionMethod($this->manifest, 'getImportsFromTokens');
60
		$method->setAccessible(true);
61
62
		$expectedImports = array(
63
			'ModelAdmin',
64
			'Cont' => 'Controller',
65
			'Request' => 'SS_HTTPRequest',
66
			'Response' => 'SS_HTTPResponse',
67
			'P' => 'SilverStripe\\Security\\PermissionProvider',
68
			'silverstripe\test\ClassA',
69
			'\Object',
70
		);
71
72
		$imports = $method->invoke($this->manifest, $tokens);
73
74
		$this->assertEquals($expectedImports, $imports);
75
76
	}
77
78
	public function testClassInfoIsCorrect() {
79
		$this->assertContains('SilverStripe\Framework\Tests\ClassI', ClassInfo::implementorsOf('SilverStripe\\Security\\PermissionProvider'));
80
81
		//because we're using a nested manifest we have to "coalesce" the descendants again to correctly populate the
82
		// descendants of the core classes we want to test against - this is a limitation of the test manifest not
83
		// including all core classes
84
		$method = new ReflectionMethod($this->manifest, 'coalesceDescendants');
85
		$method->setAccessible(true);
86
		$method->invoke($this->manifest, 'ModelAdmin');
87
88
		$this->assertContains('SilverStripe\Framework\Tests\ClassI', ClassInfo::subclassesFor('ModelAdmin'));
89
	}
90
91
	/**
92
	 * @skipUpgrade
93
	 */
94
	public function testFindClassOrInterfaceFromCandidateImports() {
95
		$method = new ReflectionMethod($this->manifest, 'findClassOrInterfaceFromCandidateImports');
96
		$method->setAccessible(true);
97
98
		$this->assertTrue(ClassInfo::exists('silverstripe\test\ClassA'));
99
100
		$this->assertEquals(
101
			'PermissionProvider',
102
			$method->invokeArgs($this->manifest, [
103
				'\PermissionProvider',
104
				'Test\Namespace',
105
				array(
106
					'TestOnly',
107
					'Controller',
108
				),
109
			])
110
		);
111
112
		$this->assertEquals('PermissionProvider', $method->invokeArgs($this->manifest, array(
113
			'PermissionProvider',
114
			'Test\NAmespace',
115
			array(
116
				'PermissionProvider',
117
			)
118
		)));
119
120
		$this->assertEmpty($method->invokeArgs($this->manifest, array(
121
			'',
122
			'TextNamespace',
123
			array(
124
				'PermissionProvider',
125
			),
126
		)));
127
128
		$this->assertEmpty($method->invokeArgs($this->manifest, array(
129
			'',
130
			'',
131
			array()
132
		)));
133
134
		$this->assertEquals('silverstripe\test\ClassA', $method->invokeArgs($this->manifest, array(
135
			'ClassA',
136
			'Test\Namespace',
137
			array(
138
				'silverstripe\test\ClassA',
139
				'PermissionProvider',
140
			),
141
		)));
142
143
		$this->assertEquals('ClassA', $method->invokeArgs($this->manifest, array(
144
			'\ClassA',
145
			'Test\Namespace',
146
			array(
147
				'silverstripe\test',
148
			),
149
		)));
150
151
		$this->assertEquals('ClassA', $method->invokeArgs($this->manifest, array(
152
			'ClassA',
153
			'silverstripe\test',
154
			array(
155
				'\ClassA',
156
			),
157
		)));
158
159
		$this->assertEquals('ClassA', $method->invokeArgs($this->manifest, array(
160
			'Alias',
161
			'silverstripe\test',
162
			array(
163
				'Alias' => '\ClassA',
164
			),
165
		)));
166
167
		$this->assertEquals('silverstripe\test\ClassA', $method->invokeArgs($this->manifest, array(
168
			'ClassA',
169
			'silverstripe\test',
170
			array(
171
				'silverstripe\test\ClassB',
172
			),
173
		)));
174
175
	}
176
177
	public function testGetItemPath() {
178
		$expect = array(
179
			'SILVERSTRIPE\TEST\CLASSA'     => 'module/classes/ClassA.php',
180
			'Silverstripe\Test\ClassA'     => 'module/classes/ClassA.php',
181
			'silverstripe\test\classa'     => 'module/classes/ClassA.php',
182
			'SILVERSTRIPE\TEST\INTERFACEA' => 'module/interfaces/InterfaceA.php',
183
			'Silverstripe\Test\InterfaceA' => 'module/interfaces/InterfaceA.php',
184
			'silverstripe\test\interfacea' => 'module/interfaces/InterfaceA.php'
185
		);
186
187
		foreach ($expect as $name => $path) {
188
			$this->assertEquals("{$this->base}/$path", $this->manifest->getItemPath($name));
189
		}
190
	}
191
192
	public function testGetClasses() {
193
		$expect = array(
194
			'silverstripe\test\classa' => "{$this->base}/module/classes/ClassA.php",
195
			'silverstripe\test\classb' => "{$this->base}/module/classes/ClassB.php",
196
			'silverstripe\test\classc' => "{$this->base}/module/classes/ClassC.php",
197
			'silverstripe\test\classd' => "{$this->base}/module/classes/ClassD.php",
198
			'silverstripe\test\classe' => "{$this->base}/module/classes/ClassE.php",
199
			'silverstripe\test\classf' => "{$this->base}/module/classes/ClassF.php",
200
			'silverstripe\test\classg' => "{$this->base}/module/classes/ClassG.php",
201
			'silverstripe\test\classh' => "{$this->base}/module/classes/ClassH.php",
202
			'sstemplateparser'         => FRAMEWORK_PATH."/view/SSTemplateParser.php",
203
			'sstemplateparseexception' => FRAMEWORK_PATH."/view/SSTemplateParser.php",
204
			'silverstripe\framework\tests\classi' => "{$this->base}/module/classes/ClassI.php",
205
		);
206
207
		$this->assertEquals($expect, $this->manifest->getClasses());
208
	}
209
210
	public function testGetClassNames() {
211
		$this->assertEquals(
212
			array('sstemplateparser', 'sstemplateparseexception', 'silverstripe\test\classa',
213
				'silverstripe\test\classb', 'silverstripe\test\classc', 'silverstripe\test\classd',
214
				'silverstripe\test\classe', 'silverstripe\test\classf', 'silverstripe\test\classg',
215
				'silverstripe\test\classh', 'silverstripe\framework\tests\classi'),
216
			$this->manifest->getClassNames());
217
	}
218
219
	public function testGetDescendants() {
220
		$expect = array(
221
			'silverstripe\test\classa' => array('silverstripe\test\ClassB', 'silverstripe\test\ClassH'),
222
		);
223
224
		$this->assertEquals($expect, $this->manifest->getDescendants());
225
	}
226
227
	public function testGetDescendantsOf() {
228
		$expect = array(
229
			'SILVERSTRIPE\TEST\CLASSA' => array('silverstripe\test\ClassB', 'silverstripe\test\ClassH'),
230
			'silverstripe\test\classa' => array('silverstripe\test\ClassB', 'silverstripe\test\ClassH'),
231
		);
232
233
		foreach ($expect as $class => $desc) {
234
			$this->assertEquals($desc, $this->manifest->getDescendantsOf($class));
235
		}
236
	}
237
238
	public function testGetInterfaces() {
239
		$expect = array(
240
			'silverstripe\test\interfacea' => "{$this->base}/module/interfaces/InterfaceA.php",
241
		);
242
		$this->assertEquals($expect, $this->manifest->getInterfaces());
243
	}
244
245
	public function testGetImplementors() {
246
		$expect = array(
247
			'silverstripe\test\interfacea' => array('silverstripe\test\ClassE'),
248
			'interfacea' => array('silverstripe\test\ClassF'),
249
			'silverstripe\test\subtest\interfacea' => array('silverstripe\test\ClassG'),
250
			'silverstripe\security\permissionprovider' => array('SilverStripe\Framework\Tests\ClassI'),
251
		);
252
		$this->assertEquals($expect, $this->manifest->getImplementors());
253
	}
254
255
	public function testGetImplementorsOf() {
256
		$expect = array(
257
			'SILVERSTRIPE\TEST\INTERFACEA' => array('silverstripe\test\ClassE'),
258
			'silverstripe\test\interfacea' => array('silverstripe\test\ClassE'),
259
			'INTERFACEA' => array('silverstripe\test\ClassF'),
260
			'interfacea' => array('silverstripe\test\ClassF'),
261
			'SILVERSTRIPE\TEST\SUBTEST\INTERFACEA' => array('silverstripe\test\ClassG'),
262
			'silverstripe\test\subtest\interfacea' => array('silverstripe\test\ClassG'),
263
		);
264
265
		foreach ($expect as $interface => $impl) {
266
			$this->assertEquals($impl, $this->manifest->getImplementorsOf($interface));
267
		}
268
	}
269
270
	public function testGetConfigs() {
271
		$expect = array("{$this->base}/module/_config.php");
272
		$this->assertEquals($expect, $this->manifest->getConfigs());
273
	}
274
275
	public function testGetModules() {
276
		$expect = array(
277
			"module" => "{$this->base}/module",
278
			"moduleb" => "{$this->base}/moduleb"
279
		);
280
		$this->assertEquals($expect, $this->manifest->getModules());
281
	}
282
}
283