Arbit - project tracking

PHP Depend

Browse source code

File: / tests/ PHP/ Depend/ DependTest.php

Type
text/plain text/plain
Last Author
mapi
Version
1181
Line Rev. Author Source
1 51 mapi <?php
2 mapi /**
3 mapi * This file is part of PHP_Depend.
4 mapi *
5 mapi * PHP Version 5
6 mapi *
7 1030 mapi * Copyright (c) 2008-2010, Manuel Pichler <mapi@pdepend.org>.
8 51 mapi * All rights reserved.
9 mapi *
10 mapi * Redistribution and use in source and binary forms, with or without
11 mapi * modification, are permitted provided that the following conditions
12 mapi * are met:
13 mapi *
14 mapi * * Redistributions of source code must retain the above copyright
15 mapi * notice, this list of conditions and the following disclaimer.
16 mapi *
17 mapi * * Redistributions in binary form must reproduce the above copyright
18 mapi * notice, this list of conditions and the following disclaimer in
19 mapi * the documentation and/or other materials provided with the
20 mapi * distribution.
21 mapi *
22 mapi * * Neither the name of Manuel Pichler nor the names of his
23 mapi * contributors may be used to endorse or promote products derived
24 mapi * from this software without specific prior written permission.
25 mapi *
26 mapi * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 mapi * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 mapi * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 mapi * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 mapi * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 mapi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 mapi * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 mapi * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 mapi * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 mapi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 mapi * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 mapi * POSSIBILITY OF SUCH DAMAGE.
38 mapi *
39 mapi * @category QualityAssurance
40 mapi * @package PHP_Depend
41 269 mapi * @author Manuel Pichler <mapi@pdepend.org>
42 1030 mapi * @copyright 2008-2010 Manuel Pichler. All rights reserved.
43 51 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
44 mapi * @version SVN: $Id: DependTest.php 1181 2010-03-15 18:19:14Z mapi $
45 823 mapi * @link http://pdepend.org/
46 51 mapi */
47 mapi
48 mapi require_once dirname(__FILE__) . '/AbstractTest.php';
49 mapi
50 mapi require_once 'PHP/Depend.php';
51 609 mapi require_once 'PHP/Depend/Input/ExtensionFilter.php';
52 867 mapi require_once 'PHP/Depend/Storage/EngineI.php';
53 51 mapi
54 mapi /**
55 mapi * Test case for PHP_Depend facade.
56 mapi *
57 mapi * @category QualityAssurance
58 mapi * @package PHP_Depend
59 269 mapi * @author Manuel Pichler <mapi@pdepend.org>
60 1030 mapi * @copyright 2008-2010 Manuel Pichler. All rights reserved.
61 51 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
62 mapi * @version Release: @package_version@
63 823 mapi * @link http://pdepend.org/
64 51 mapi */
65 mapi class PHP_Depend_DependTest extends PHP_Depend_AbstractTest
66 mapi {
67 mapi /**
68 mapi * Tests that the {@link PHP_Depend::addDirectory()} method fails with an
69 mapi * exception for an invalid directory.
70 mapi *
71 mapi * @return void
72 mapi */
73 mapi public function testAddInvalidDirectoryFail()
74 mapi {
75 168 mapi $dir = dirname(__FILE__) . '/foobar';
76 221 mapi $msg = "Invalid directory '{$dir}' added.";
77 51 mapi
78 634 mapi $this->setExpectedException('InvalidArgumentException', $msg);
79 168 mapi
80 51 mapi $pdepend = new PHP_Depend();
81 168 mapi $pdepend->addDirectory($dir);
82 51 mapi }
83 mapi /**
84 mapi * Tests that the {@link PHP_Depend::addDirectory()} method with an existing
85 mapi * directory.
86 mapi *
87 mapi * @return void
88 mapi */
89 mapi public function testAddDirectory()
90 mapi {
91 mapi $pdepend = new PHP_Depend();
92 141 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code/code-5.2.x');
93 51 mapi }
94 mapi
95 mapi /**
96 mapi * Tests the {@link PHP_Depend::analyze()} method and the return value.
97 mapi *
98 mapi * @return void
99 mapi */
100 mapi public function testAnalyze()
101 mapi {
102 mapi $pdepend = new PHP_Depend();
103 141 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code/code-5.2.x');
104 609 mapi $pdepend->addFileFilter(new PHP_Depend_Input_ExtensionFilter(array('php')));
105 51 mapi
106 mapi $metrics = $pdepend->analyze();
107 mapi
108 mapi $expected = array(
109 184 mapi 'package1' => true,
110 mapi 'package2' => true,
111 205 mapi 'package3' => true
112 51 mapi );
113 mapi
114 mapi $this->assertType('Iterator', $metrics);
115 mapi foreach ($metrics as $metric) {
116 205 mapi $this->assertType('PHP_Depend_Code_Package', $metric);
117 51 mapi
118 mapi unset($expected[$metric->getName()]);
119 mapi }
120 mapi
121 mapi $this->assertEquals(0, count($expected));
122 mapi }
123 mapi
124 mapi /**
125 293 mapi * Tests that {@PHP_Depend::analyze()} throws an exception if no source
126 235 mapi * directory was set.
127 mapi *
128 mapi * @return void
129 mapi */
130 mapi public function testAnalyzeThrowsAnExceptionForNoSourceDirectory()
131 mapi {
132 mapi $pdepend = new PHP_Depend();
133 634 mapi $this->setExpectedException('RuntimeException', 'No source directory and file set.');
134 235 mapi $pdepend->analyze();
135 mapi }
136 mapi
137 mapi /**
138 1181 mapi * testAnalyzerReturnsEmptyIteratorWhenNoPackageExists
139 293 mapi *
140 mapi * @return void
141 1181 mapi * @covers PHP_Depend
142 mapi * @group pdepend
143 mapi * @group unittest
144 293 mapi */
145 1181 mapi public function testAnalyzerReturnsEmptyIteratorWhenNoPackageExists()
146 293 mapi {
147 mapi $pdepend = new PHP_Depend();
148 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code/code-without-comments');
149 1095 mapi $pdepend->addFileFilter(new PHP_Depend_Input_ExtensionFilter(array(__METHOD__)));
150 1181 mapi
151 mapi $this->assertEquals(0, $pdepend->analyze()->count());
152 293 mapi }
153 mapi
154 mapi /**
155 235 mapi * Tests that {PHP_Depend::analyze()} configures the ignore annotations
156 mapi * option correct.
157 mapi *
158 mapi * @return void
159 mapi */
160 mapi public function testAnalyzeSetsWithoutAnnotations()
161 mapi {
162 mapi $pdepend = new PHP_Depend();
163 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code');
164 609 mapi $pdepend->addFileFilter(new PHP_Depend_Input_ExtensionFilter(array('inc')));
165 235 mapi $pdepend->setWithoutAnnotations();
166 mapi $packages = $pdepend->analyze();
167 mapi
168 301 mapi $this->assertEquals(2, $packages->count());
169 235 mapi $this->assertEquals('pdepend.test', $packages->current()->getName());
170 mapi
171 mapi $function = $packages->current()->getFunctions()->current();
172 mapi
173 mapi $this->assertNotNull($function);
174 mapi $this->assertEquals('foo', $function->getName());
175 827 mapi $this->assertEquals(0, $function->getExceptionClasses()->count());
176 235 mapi }
177 mapi
178 mapi /**
179 51 mapi * Tests that the {@link PHP_Depend::countClasses()} method returns the
180 mapi * expected number of classes.
181 mapi *
182 mapi * @return void
183 mapi */
184 mapi public function testCountClasses()
185 mapi {
186 mapi $pdepend = new PHP_Depend();
187 141 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code/code-5.2.x');
188 993 mapi $pdepend->addFileFilter(new PHP_Depend_Input_ExtensionFilter(array('php')));
189 51 mapi $pdepend->analyze();
190 mapi
191 205 mapi $this->assertEquals(10, $pdepend->countClasses());
192 51 mapi }
193 mapi
194 mapi /**
195 mapi * Tests that the {@link PHP_Depend::countClasses()} method fails with an
196 mapi * exception if the code was not analyzed before.
197 mapi *
198 mapi * @return void
199 mapi */
200 mapi public function testCountClassesWithoutAnalyzeFail()
201 mapi {
202 113 mapi $this->setExpectedException(
203 mapi 'RuntimeException',
204 mapi 'countClasses() doesn\'t work before the source was analyzed.'
205 mapi );
206 51 mapi
207 mapi $pdepend = new PHP_Depend();
208 141 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code/code-5.2.x');
209 51 mapi $pdepend->countClasses();
210 mapi }
211 mapi
212 mapi /**
213 mapi * Tests that the {@link PHP_Depend::countPackages()} method returns the
214 mapi * expected number of packages.
215 mapi *
216 mapi * @return void
217 mapi */
218 mapi public function testCountPackages()
219 mapi {
220 mapi $pdepend = new PHP_Depend();
221 141 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code/code-5.2.x');
222 51 mapi $pdepend->analyze();
223 mapi
224 1095 mapi $this->assertEquals(4, $pdepend->countPackages());
225 51 mapi }
226 mapi
227 mapi /**
228 mapi * Tests that the {@link PHP_Depend::countPackages()} method fails with an
229 mapi * exception if the code was not analyzed before.
230 mapi *
231 mapi * @return void
232 mapi */
233 mapi public function testCountPackagesWithoutAnalyzeFail()
234 mapi {
235 113 mapi $this->setExpectedException(
236 mapi 'RuntimeException',
237 mapi 'countPackages() doesn\'t work before the source was analyzed.'
238 mapi );
239 51 mapi
240 mapi $pdepend = new PHP_Depend();
241 141 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code/code-5.2.x');
242 51 mapi $pdepend->countPackages();
243 mapi }
244 mapi
245 mapi /**
246 mapi * Tests that the {@link PHP_Depend::getPackage()} method returns the
247 207 mapi * expected {@link PHP_Depend_Code_Package} objects.
248 51 mapi *
249 mapi * @return void
250 mapi */
251 mapi public function testGetPackage()
252 mapi {
253 mapi $pdepend = new PHP_Depend();
254 141 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code/code-5.2.x');
255 51 mapi $pdepend->analyze();
256 mapi
257 113 mapi $packages = array(
258 mapi 'package1',
259 mapi 'package2',
260 205 mapi 'package3'
261 113 mapi );
262 mapi
263 205 mapi $className = 'PHP_Depend_Code_Package';
264 113 mapi
265 mapi foreach ($packages as $package) {
266 mapi $this->assertType($className, $pdepend->getPackage($package));
267 mapi }
268 51 mapi }
269 mapi
270 mapi /**
271 mapi * Tests that the {@link PHP_Depend::getPackage()} method fails with an
272 mapi * exception if the code was not analyzed before.
273 mapi *
274 mapi * @return void
275 mapi */
276 mapi public function testGetPackageWithoutAnalyzeFail()
277 mapi {
278 113 mapi $this->setExpectedException(
279 mapi 'RuntimeException',
280 mapi 'getPackage() doesn\'t work before the source was analyzed.'
281 mapi );
282 51 mapi
283 mapi $pdepend = new PHP_Depend();
284 141 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code/code-5.2.x');
285 51 mapi $pdepend->getPackage('package1');
286 mapi }
287 mapi
288 mapi /**
289 mapi * Tests that the {@link PHP_Depend::getPackage()} method fails with an
290 mapi * exception if you request an invalid package.
291 mapi *
292 mapi * @return void
293 mapi */
294 mapi public function testGetPackageWithUnknownPackageFail()
295 mapi {
296 113 mapi $this->setExpectedException(
297 mapi 'OutOfBoundsException',
298 mapi 'Unknown package "package0".'
299 mapi );
300 51 mapi
301 mapi $pdepend = new PHP_Depend();
302 141 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code/code-5.2.x');
303 51 mapi $pdepend->analyze();
304 mapi $pdepend->getPackage('package0');
305 mapi }
306 mapi
307 mapi /**
308 mapi * Tests that the {@link PHP_Depend::getPackages()} method returns the
309 207 mapi * expected {@link PHP_Depend_Code_Package} objects and reuses the result of
310 mapi * {@link PHP_Depend::analyze()}.
311 51 mapi *
312 mapi * @return void
313 mapi */
314 mapi public function testGetPackages()
315 mapi {
316 mapi $pdepend = new PHP_Depend();
317 141 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code/code-5.2.x');
318 51 mapi
319 205 mapi $package1 = $pdepend->analyze();
320 mapi $package2 = $pdepend->getPackages();
321 51 mapi
322 205 mapi $this->assertNotNull($package1);
323 mapi $this->assertNotNull($package2);
324 51 mapi
325 205 mapi $this->assertSame($package1, $package2);
326 51 mapi }
327 mapi
328 mapi /**
329 mapi * Tests that the {@link PHP_Depend::getPackages()} method fails with an
330 mapi * exception if the code was not analyzed before.
331 mapi *
332 mapi * @return void
333 mapi */
334 mapi public function testGetPackagesWithoutAnalyzeFail()
335 mapi {
336 113 mapi $this->setExpectedException(
337 mapi 'RuntimeException',
338 mapi 'getPackages() doesn\'t work before the source was analyzed.'
339 mapi );
340 51 mapi
341 mapi $pdepend = new PHP_Depend();
342 141 mapi $pdepend->addDirectory(dirname(__FILE__) . '/_code/code-5.2.x');
343 51 mapi $pdepend->getPackages();
344 mapi }
345 634 mapi
346 mapi /**
347 mapi * Tests the newly added support for single file handling.
348 mapi *
349 mapi * @return void
350 mapi */
351 mapi public function testSupportForSingleFileIssue90()
352 mapi {
353 mapi $pdepend = new PHP_Depend();
354 mapi $pdepend->addFile(dirname(__FILE__) . '/_code/issues/090.php');
355 mapi $pdepend->analyze();
356 mapi
357 mapi $packages = $pdepend->getPackages();
358 mapi $this->assertSame(1, $packages->count());
359 mapi
360 mapi $package = $packages->current();
361 mapi $this->assertSame(1, $package->getClasses()->count());
362 mapi $this->assertSame(1, $package->getInterfaces()->count());
363 mapi }
364 867 mapi
365 mapi /**
366 mapi * Tests that the addFile() method throws the expected exception when an
367 mapi * added file does not exist.
368 mapi *
369 mapi * @return void
370 mapi */
371 mapi public function testAddFileMethodThrowsExpectedExceptionForFileThatNotExists()
372 mapi {
373 mapi $pdepend = new PHP_Depend();
374 mapi
375 mapi $fileName = '/tmp/' . uniqid('pdepend_', true) . '.php';
376 mapi $this->assertFileNotExists($fileName);
377 mapi
378 mapi $this->setExpectedException(
379 mapi 'InvalidArgumentException',
380 mapi 'does not exist.'
381 mapi );
382 mapi
383 mapi $pdepend->addFile($fileName);
384 mapi }
385 mapi
386 mapi /**
387 1132 mapi * testSetStorageSetsPruneFlagOnTokenCache
388 mapi *
389 mapi * @return void
390 mapi * @covers PHP_Depend
391 mapi * @group pdepend
392 mapi * @group unittest
393 mapi */
394 mapi public function testSetStorageSetsPruneFlagOnTokenCache()
395 mapi {
396 mapi $cache = $this->getMock('PHP_Depend_Storage_EngineI');
397 mapi $cache->expects($this->once())
398 mapi ->method('setPrune');
399 mapi
400 mapi $pdepend = new PHP_Depend();
401 mapi $pdepend->setStorage(PHP_Depend::TOKEN_STORAGE, $cache);
402 mapi }
403 mapi
404 mapi /**
405 mapi * testSetStorageSetsMaxLifetimeAndProbabilityOnParserCache
406 mapi *
407 mapi * @return void
408 mapi * @covers PHP_Depend
409 mapi * @group pdepend
410 mapi * @group unittest
411 mapi */
412 mapi public function testSetStorageSetsMaxLifetimeAndProbabilityOnParserCache()
413 mapi {
414 mapi $cache = $this->getMock('PHP_Depend_Storage_EngineI');
415 mapi $cache->expects($this->once())
416 mapi ->method('setProbability');
417 mapi $cache->expects($this->once())
418 mapi ->method('setMaxLifetime');
419 mapi
420 mapi $pdepend = new PHP_Depend();
421 mapi $pdepend->setStorage(PHP_Depend::PARSER_STORAGE, $cache);
422 mapi }
423 mapi
424 mapi /**
425 867 mapi * Tests that the setStorage() method throws an exception when an invalid
426 mapi * storage type was given.
427 mapi *
428 mapi * @return void
429 mapi */
430 mapi public function testSetStorageThrowsTheExpectedExceptionForAnUnknownStorageType()
431 mapi {
432 mapi $pdepend = new PHP_Depend();
433 mapi
434 mapi $this->setExpectedException(
435 mapi 'InvalidArgumentException',
436 mapi 'Unknown storage identifier'
437 mapi );
438 mapi
439 mapi $pdepend->setStorage(
440 mapi PHP_INT_MAX,
441 mapi $this->getMock('PHP_Depend_Storage_EngineI')
442 mapi );
443 mapi }
444 993 mapi }