Arbit - project tracking

PHPMD

Browse source code

File: / test/ PHP/ PMD/ RuleSetFactoryTest.php

Type
text/plain text/plain
Last Author
mapi
Version
220
Line Rev. Author Source
1 41 mapi <?php
2 mapi /**
3 mapi * This file is part of PHP_PMD.
4 mapi *
5 mapi * PHP Version 5
6 mapi *
7 174 mapi * Copyright (c) 2009-2010, Manuel Pichler <mapi@phpmd.org>.
8 41 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 PHP
40 mapi * @package PHP_PMD
41 174 mapi * @author Manuel Pichler <mapi@phpmd.org>
42 150 mapi * @copyright 2009-2010 Manuel Pichler. All rights reserved.
43 41 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
44 mapi * @version SVN: $Id$
45 174 mapi * @link http://phpmd.org
46 41 mapi */
47 mapi
48 mapi require_once dirname(__FILE__) . '/AbstractTest.php';
49 mapi
50 mapi require_once 'PHP/PMD/RuleSetFactory.php';
51 mapi
52 mapi /**
53 mapi * Test case for the rule set factory class.
54 mapi *
55 mapi * @category PHP
56 mapi * @package PHP_PMD
57 174 mapi * @author Manuel Pichler <mapi@phpmd.org>
58 150 mapi * @copyright 2009-2010 Manuel Pichler. All rights reserved.
59 41 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
60 mapi * @version Release: @package_version@
61 174 mapi * @link http://phpmd.org
62 41 mapi */
63 mapi class PHP_PMD_RuleSetFactoryTest extends PHP_PMD_AbstractTest
64 mapi {
65 mapi /**
66 83 mapi * testCreateRuleSetFileNameFindsXmlFileInBundledRuleSets
67 mapi *
68 mapi * @return void
69 mapi * @covers PHP_PMD_RuleSetFactory::_createRuleSetFileName
70 mapi * @group phpmd
71 mapi * @group unittest
72 mapi */
73 mapi public function testCreateRuleSetFileNameFindsXmlFileInBundledRuleSets()
74 mapi {
75 mapi $factory = new PHP_PMD_RuleSetFactory();
76 mapi $ruleSet = $factory->createSingleRuleSet('codesize');
77 mapi
78 mapi $this->assertContains('The Code Size Ruleset', $ruleSet->getDescription());
79 mapi }
80 mapi
81 mapi /**
82 mapi * testCreateRuleSetFileNameFindsXmlFileInCurrentWorkingDirectory
83 mapi *
84 mapi * @return void
85 mapi * @covers PHP_PMD_RuleSetFactory::_createRuleSetFileName
86 mapi * @group phpmd
87 mapi * @group unittest
88 mapi */
89 mapi public function testCreateRuleSetFileNameFindsXmlFileInCurrentWorkingDirectory()
90 mapi {
91 mapi self::changeWorkingDirectory('rulesets');
92 mapi
93 mapi $factory = new PHP_PMD_RuleSetFactory();
94 mapi $ruleSet = $factory->createSingleRuleSet('set1.xml');
95 mapi
96 mapi $this->assertEquals('First description...', $ruleSet->getDescription());
97 mapi }
98 mapi
99 mapi /**
100 80 mapi * testCreateRuleSetsReturnsArray
101 41 mapi *
102 mapi * @return void
103 80 mapi * @covers PHP_PMD_RuleSetFactory
104 mapi * @group phpmd
105 mapi * @group unittest
106 41 mapi */
107 80 mapi public function testCreateRuleSetsReturnsArray()
108 41 mapi {
109 80 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
110 41 mapi $this->assertType('array', $ruleSets);
111 80 mapi }
112 41 mapi
113 80 mapi /**
114 mapi * testCreateRuleSetsForSingleFileReturnsArrayWithOneElement
115 mapi *
116 mapi * @return void
117 mapi * @covers PHP_PMD_RuleSetFactory
118 mapi * @group phpmd
119 mapi * @group unittest
120 mapi */
121 mapi public function testCreateRuleSetsForSingleFileReturnsArrayWithOneElement()
122 mapi {
123 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
124 mapi $this->assertEquals(1, count($ruleSets));
125 41 mapi }
126 mapi
127 mapi /**
128 80 mapi * testCreateRuleSetsForSingleFileReturnsOneRuleSetInstance
129 41 mapi *
130 mapi * @return void
131 80 mapi * @covers PHP_PMD_RuleSetFactory
132 mapi * @group phpmd
133 mapi * @group unittest
134 41 mapi */
135 80 mapi public function testCreateRuleSetsForSingleFileReturnsOneRuleSetInstance()
136 41 mapi {
137 80 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
138 mapi $this->assertType('PHP_PMD_RuleSet', $ruleSets[0]);
139 mapi }
140 mapi
141 mapi /**
142 mapi * testCreateRuleSetsConfiguresExpectedRuleSetName
143 mapi *
144 mapi * @return void
145 mapi * @covers PHP_PMD_RuleSetFactory
146 mapi * @group phpmd
147 mapi * @group unittest
148 mapi */
149 mapi public function testCreateRuleSetsConfiguresExpectedRuleSetName()
150 mapi {
151 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
152 mapi $this->assertEquals('First Test RuleSet', $ruleSets[0]->getName());
153 mapi }
154 mapi
155 mapi /**
156 mapi * testCreateRuleSetsConfiguresExpectedRuleSetName
157 mapi *
158 mapi * @return void
159 mapi * @covers PHP_PMD_RuleSetFactory
160 mapi * @group phpmd
161 mapi * @group unittest
162 mapi */
163 mapi public function testCreateRuleSetsConfiguresExpectedRuleSetDescription()
164 mapi {
165 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
166 mapi $this->assertEquals('First description...', $ruleSets[0]->getDescription());
167 mapi }
168 mapi
169 mapi /**
170 mapi * testCreateRuleSetsForTwoFilesReturnsArrayWithTwoElements
171 mapi *
172 mapi * @return void
173 mapi * @covers PHP_PMD_RuleSetFactory
174 mapi * @group phpmd
175 mapi * @group unittest
176 mapi */
177 mapi public function testCreateRuleSetsForTwoFilesReturnsArrayWithTwoElements()
178 mapi {
179 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles(
180 mapi 'rulesets/set1.xml',
181 mapi 'rulesets/set2.xml'
182 mapi );
183 mapi $this->assertEquals(2, count($ruleSets));
184 mapi }
185 mapi
186 mapi /**
187 mapi * testCreateRuleSetsForTwoFilesReturnsExpectedRuleSetInstances
188 mapi *
189 mapi * @return void
190 mapi * @covers PHP_PMD_RuleSetFactory
191 mapi * @group phpmd
192 mapi * @group unittest
193 mapi */
194 mapi public function testCreateRuleSetsForTwoFilesReturnsExpectedRuleSetInstances()
195 mapi {
196 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles(
197 mapi 'rulesets/set1.xml',
198 mapi 'rulesets/set2.xml'
199 mapi );
200 mapi $this->assertType('PHP_PMD_RuleSet', $ruleSets[0]);
201 mapi $this->assertType('PHP_PMD_RuleSet', $ruleSets[1]);
202 mapi }
203 mapi
204 mapi /**
205 mapi * testCreateRuleSetsForTwoConfiguresExpectedRuleSetNames
206 mapi *
207 mapi * @return void
208 mapi * @covers PHP_PMD_RuleSetFactory
209 mapi * @group phpmd
210 mapi * @group unittest
211 mapi */
212 mapi public function testCreateRuleSetsForTwoConfiguresExpectedRuleSetNames()
213 mapi {
214 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles(
215 mapi 'rulesets/set1.xml',
216 mapi 'rulesets/set2.xml'
217 mapi );
218 mapi $this->assertEquals('First Test RuleSet', $ruleSets[0]->getName());
219 mapi $this->assertEquals('Second Test RuleSet', $ruleSets[1]->getName());
220 mapi }
221 mapi
222 mapi /**
223 mapi * testCreateRuleSetsForTwoConfiguresExpectedRuleSetDescriptions
224 mapi *
225 mapi * @return void
226 mapi * @covers PHP_PMD_RuleSetFactory
227 mapi * @group phpmd
228 mapi * @group unittest
229 mapi */
230 mapi public function testCreateRuleSetsForTwoConfiguresExpectedRuleSetDescriptions()
231 mapi {
232 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles(
233 mapi 'rulesets/set1.xml',
234 mapi 'rulesets/set2.xml'
235 mapi );
236 mapi $this->assertSame('First description...', $ruleSets[0]->getDescription());
237 mapi $this->assertSame('Second description...', $ruleSets[1]->getDescription());
238 mapi }
239 mapi
240 mapi /**
241 mapi * testCreateRuleSetsForSingleLocalFileNameReturnsArray
242 mapi *
243 mapi * @return void
244 mapi * @covers PHP_PMD_RuleSetFactory
245 mapi * @group phpmd
246 mapi * @group unittest
247 mapi */
248 mapi public function testCreateRuleSetsForLocalFileNameReturnsArray()
249 mapi {
250 mapi self::changeWorkingDirectory();
251 41 mapi
252 80 mapi $ruleSets = $this->_createRuleSetsFromFiles('rulesets/set1.xml');
253 41 mapi $this->assertType('array', $ruleSets);
254 80 mapi }
255 41 mapi
256 80 mapi /**
257 mapi * testCreateRuleSetsForSingleLocalFileNameReturnsArrayWithOneElement
258 mapi *
259 mapi * @return void
260 mapi * @covers PHP_PMD_RuleSetFactory
261 mapi * @group phpmd
262 mapi * @group unittest
263 mapi */
264 mapi public function testCreateRuleSetsForLocalFileNameReturnsArrayWithOneElement()
265 mapi {
266 mapi self::changeWorkingDirectory();
267 41 mapi
268 80 mapi $ruleSets = $this->_createRuleSetsFromFiles('rulesets/set1.xml');
269 mapi $this->assertEquals(1, count($ruleSets));
270 41 mapi }
271 mapi
272 mapi /**
273 80 mapi * testCreateRuleSetsForSingleLocalFileNameConfiguresExpectedRuleSetName
274 41 mapi *
275 mapi * @return void
276 80 mapi * @covers PHP_PMD_RuleSetFactory
277 mapi * @group phpmd
278 mapi * @group unittest
279 41 mapi */
280 80 mapi public function testCreateRuleSetsForLocalFileNameConfiguresExpectedRuleSetName()
281 41 mapi {
282 mapi self::changeWorkingDirectory();
283 mapi
284 80 mapi $ruleSets = $this->_createRuleSetsFromFiles('rulesets/set1.xml');
285 mapi $this->assertEquals('First Test RuleSet', $ruleSets[0]->getName());
286 mapi }
287 41 mapi
288 80 mapi /**
289 mapi * testCreateRuleSetsWithReferenceContainsExpectedRuleSet
290 mapi *
291 mapi * @return void
292 mapi * @covers PHP_PMD_RuleSetFactory
293 mapi * @group phpmd
294 mapi * @group unittest
295 mapi */
296 mapi public function testCreateRuleSetsWithReferenceContainsExpectedRuleSet()
297 mapi {
298 mapi self::changeWorkingDirectory();
299 mapi
300 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles('rulesets/refset1.xml');
301 mapi $this->assertEquals('First Test RuleSet', $ruleSets[0]->getName());
302 41 mapi }
303 mapi
304 mapi /**
305 80 mapi * testCreateRuleSetsWithReferenceContainsExpectedNumberOfRules
306 41 mapi *
307 mapi * @return void
308 80 mapi * @covers PHP_PMD_RuleSetFactory
309 mapi * @group phpmd
310 mapi * @group unittest
311 41 mapi */
312 80 mapi public function testCreateRuleSetsWithReferenceContainsExpectedNumberOfRules()
313 41 mapi {
314 mapi self::changeWorkingDirectory();
315 mapi
316 80 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles('rulesets/refset1.xml');
317 mapi $this->assertEquals(4, iterator_count($ruleSets[0]));
318 mapi }
319 41 mapi
320 80 mapi /**
321 mapi * testCreateRuleSetsForLocalFileWithRuleSetReferenceNodes
322 mapi *
323 mapi * @return void
324 mapi * @covers PHP_PMD_RuleSetFactory
325 mapi * @group phpmd
326 mapi * @group unittest
327 mapi */
328 mapi public function testCreateRuleSetsWithReferenceContainsRuleInstances()
329 mapi {
330 mapi self::changeWorkingDirectory();
331 41 mapi
332 80 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles('rulesets/refset1.xml');
333 mapi $this->assertType('PHP_PMD_AbstractRule', $ruleSets[0]->getRules()->current());
334 41 mapi }
335 mapi
336 mapi /**
337 80 mapi * testCreateRuleSetsWithReferenceContainsExpectedRules
338 41 mapi *
339 mapi * @return void
340 80 mapi * @covers PHP_PMD_RuleSetFactory
341 mapi * @group phpmd
342 mapi * @group unittest
343 41 mapi */
344 80 mapi public function testCreateRuleSetsWithReferenceContainsExpectedRules()
345 41 mapi {
346 mapi self::changeWorkingDirectory();
347 mapi
348 80 mapi $ruleSets = $this->_createRuleSetsFromAbsoluteFiles('rulesets/refset2.xml');
349 41 mapi
350 80 mapi $actual = array();
351 mapi $expected = array('RuleTwoInFirstRuleSet', 'RuleOneInSecondRuleSet');
352 mapi
353 41 mapi foreach ($ruleSets[0]->getRules() as $rule) {
354 80 mapi $actual[] = $rule->getName();
355 41 mapi }
356 80 mapi
357 mapi $this->assertEquals($expected, $actual);
358 41 mapi }
359 mapi
360 mapi /**
361 80 mapi * testCreateSingleRuleSetReturnsRuleSetInstance
362 mapi *
363 mapi * @return void
364 mapi * @covers PHP_PMD_RuleSetFactory
365 mapi * @group phpmd
366 mapi * @group unittest
367 mapi */
368 mapi public function testCreateSingleRuleSetReturnsRuleSetInstance()
369 mapi {
370 mapi self::changeWorkingDirectory();
371 mapi
372 mapi $factory = new PHP_PMD_RuleSetFactory();
373 mapi $ruleSet = $factory->createSingleRuleSet('set1');
374 mapi
375 mapi $this->assertType('PHP_PMD_RuleSet', $ruleSet);
376 mapi }
377 mapi
378 mapi /**
379 41 mapi * Tests that the rule-set factory applies a set priority filter correct.
380 mapi *
381 mapi * @return void
382 80 mapi * @covers PHP_PMD_RuleSetFactory
383 mapi * @group phpmd
384 mapi * @group unittest
385 41 mapi */
386 80 mapi public function testCreateRuleSetWithSpecifiedPriorityOnlyContainsMatchingRules()
387 41 mapi {
388 mapi self::changeWorkingDirectory();
389 mapi
390 mapi $factory = new PHP_PMD_RuleSetFactory();
391 mapi $factory->setMinimumPriority(2);
392 mapi
393 mapi $ruleSet = $factory->createSingleRuleSet('set1');
394 80 mapi $this->assertSame(1, iterator_count($ruleSet->getRules()));
395 mapi }
396 41 mapi
397 80 mapi /**
398 mapi * testCreateRuleSetsWithRuleReferenceThatOverwritesPrioritySetting
399 mapi *
400 mapi * @return void
401 mapi * @covers PHP_PMD_RuleSetFactory
402 mapi * @group phpmd
403 mapi * @group unittest
404 mapi */
405 mapi public function testCreateRuleSetsWithRuleReferenceThatOverwritesPrioritySetting()
406 mapi {
407 mapi self::changeWorkingDirectory();
408 mapi
409 mapi $factory = new PHP_PMD_RuleSetFactory();
410 mapi $ruleSets = $factory->createRuleSets('refset3');
411 mapi
412 mapi $rule = $ruleSets[0]->getRules()->current();
413 mapi $this->assertSame(-42, $rule->getPriority());
414 41 mapi }
415 mapi
416 mapi /**
417 193 mapi * testCreateRuleWithExpectedExample
418 mapi *
419 mapi * @return void
420 mapi * @covers PHP_PMD_RuleSetFactory
421 mapi * @group phpmd
422 mapi * @group unittest
423 mapi */
424 mapi public function testCreateRuleWithExpectedExample()
425 mapi {
426 mapi self::changeWorkingDirectory();
427 mapi
428 mapi $factory = new PHP_PMD_RuleSetFactory();
429 mapi $ruleSets = $factory->createRuleSets('set1');
430 mapi
431 mapi $rule = $ruleSets[0]->getRules()->current();
432 mapi $this->assertEquals(array(__FUNCTION__), $rule->getExamples());
433 mapi }
434 mapi
435 mapi /**
436 mapi * testCreateRuleWithExpectedMultipleExamples
437 mapi *
438 mapi * @return void
439 mapi * @covers PHP_PMD_RuleSetFactory
440 mapi * @group phpmd
441 mapi * @group unittest
442 mapi */
443 mapi public function testCreateRuleWithExpectedMultipleExamples()
444 mapi {
445 mapi self::changeWorkingDirectory();
446 mapi
447 mapi $factory = new PHP_PMD_RuleSetFactory();
448 mapi $ruleSets = $factory->createRuleSets('set2');
449 mapi
450 mapi $rule = $ruleSets[0]->getRules()->current();
451 mapi $this->assertEquals(array(__FUNCTION__ . 'One', __FUNCTION__ . 'Two'), $rule->getExamples());
452 mapi }
453 mapi
454 mapi /**
455 80 mapi * testCreateRuleSetsWithRuleReferenceThatOverwritesDescriptionSetting
456 41 mapi *
457 mapi * @return void
458 80 mapi * @covers PHP_PMD_RuleSetFactory
459 mapi * @group phpmd
460 mapi * @group unittest
461 41 mapi */
462 80 mapi public function testCreateRuleSetsWithRuleReferenceThatOverwritesDescriptionSetting()
463 41 mapi {
464 mapi self::changeWorkingDirectory();
465 mapi
466 mapi $factory = new PHP_PMD_RuleSetFactory();
467 mapi $ruleSets = $factory->createRuleSets('refset3');
468 mapi
469 80 mapi $rule = $ruleSets[0]->getRules()->current();
470 mapi $this->assertSame('description 42', $rule->getDescription());
471 mapi }
472 41 mapi
473 80 mapi /**
474 mapi * testCreateRuleSetsWithRuleReferenceThatOverwritesPropertySetting
475 mapi *
476 mapi * @return void
477 mapi * @covers PHP_PMD_RuleSetFactory
478 mapi * @group phpmd
479 mapi * @group unittest
480 mapi */
481 mapi public function testCreateRuleSetsWithRuleReferenceThatOverwritesPropertySetting()
482 mapi {
483 mapi self::changeWorkingDirectory();
484 mapi
485 mapi $factory = new PHP_PMD_RuleSetFactory();
486 mapi $ruleSets = $factory->createRuleSets('refset3');
487 mapi
488 41 mapi $rule = $ruleSets[0]->getRules()->current();
489 mapi $this->assertSame(42, $rule->getIntProperty('foo'));
490 80 mapi }
491 41 mapi
492 80 mapi /**
493 220 mapi * testFactorySupportsAlternativeSyntaxForPropertyValue
494 mapi *
495 mapi * @return void
496 mapi * @covers PHP_PMD_RuleSetFactory
497 mapi * @group phpmd
498 mapi * @group unittest
499 mapi */
500 mapi public function testFactorySupportsAlternativeSyntaxForPropertyValue()
501 mapi {
502 mapi self::changeWorkingDirectory();
503 mapi
504 mapi $factory = new PHP_PMD_RuleSetFactory();
505 mapi $ruleSets = $factory->createRuleSets('alternative-property-value-syntax');
506 mapi
507 mapi $rule = $ruleSets[0]->getRules()->current();
508 mapi $this->assertSame(42, $rule->getIntProperty('foo'));
509 mapi }
510 mapi
511 mapi /**
512 80 mapi * testCreateRuleSetsWithRuleReferenceThatOverwritesExamplesSetting
513 mapi *
514 mapi * @return void
515 mapi * @covers PHP_PMD_RuleSetFactory
516 mapi * @group phpmd
517 mapi * @group unittest
518 mapi */
519 mapi public function testCreateRuleSetsWithRuleReferenceThatOverwritesExamplesSetting()
520 mapi {
521 mapi self::changeWorkingDirectory();
522 mapi
523 mapi $factory = new PHP_PMD_RuleSetFactory();
524 mapi $ruleSets = $factory->createRuleSets('refset3');
525 mapi
526 mapi $rule = $ruleSets[0]->getRules()->current();
527 mapi
528 41 mapi $examples = $rule->getExamples();
529 80 mapi $this->assertEquals('foreach ($foo as $bar) { echo $bar; }', $examples[0]);
530 41 mapi }
531 mapi
532 mapi /**
533 83 mapi * testCreateRuleSetsWithRuleReferenceThatOverwritesExamplesSetting
534 mapi *
535 mapi * @return void
536 mapi * @covers PHP_PMD_RuleSetFactory
537 mapi * @group phpmd
538 mapi * @group unittest
539 mapi */
540 mapi public function testCreateRuleSetsWithRuleReferenceThatOverwritesNameSetting()
541 mapi {
542 mapi self::changeWorkingDirectory();
543 mapi
544 mapi $factory = new PHP_PMD_RuleSetFactory();
545 mapi $ruleSets = $factory->createRuleSets('refset4');
546 mapi
547 mapi $rule = $ruleSets[0]->getRules()->current();
548 mapi $this->assertEquals('Name overwritten', $rule->getName());
549 mapi }
550 mapi
551 mapi /**
552 mapi * testCreateRuleSetsWithRuleReferenceThatOverwritesMessageSetting
553 mapi *
554 mapi * @return void
555 mapi * @covers PHP_PMD_RuleSetFactory
556 mapi * @group phpmd
557 mapi * @group unittest
558 mapi */
559 mapi public function testCreateRuleSetsWithRuleReferenceThatOverwritesMessageSetting()
560 mapi {
561 mapi self::changeWorkingDirectory();
562 mapi
563 mapi $factory = new PHP_PMD_RuleSetFactory();
564 mapi $ruleSets = $factory->createRuleSets('refset4');
565 mapi
566 mapi $rule = $ruleSets[0]->getRules()->current();
567 mapi $this->assertEquals('Message overwritten', $rule->getMessage());
568 mapi }
569 mapi
570 mapi /**
571 mapi * testCreateRuleSetsWithRuleReferenceThatOverwritesExtInfoUrlSetting
572 mapi *
573 mapi * @return void
574 mapi * @covers PHP_PMD_RuleSetFactory
575 mapi * @group phpmd
576 mapi * @group unittest
577 mapi */
578 mapi public function testCreateRuleSetsWithRuleReferenceThatOverwritesExtInfoUrlSetting()
579 mapi {
580 mapi self::changeWorkingDirectory();
581 mapi
582 mapi $factory = new PHP_PMD_RuleSetFactory();
583 mapi $ruleSets = $factory->createRuleSets('refset4');
584 mapi
585 mapi $rule = $ruleSets[0]->getRules()->current();
586 mapi $this->assertEquals('http://example.com/overwritten', $rule->getExternalInfoUrl());
587 mapi }
588 mapi
589 mapi /**
590 189 mapi * testCreateRuleSetsWithRuleReferenceNotContainsExcludedRule
591 mapi *
592 mapi * @return void
593 mapi * @covers PHP_PMD_RuleSetFactory
594 mapi * @group phpmd
595 mapi * @group unittest
596 mapi */
597 mapi public function testCreateRuleSetsWithRuleReferenceNotContainsExcludedRule()
598 mapi {
599 mapi self::changeWorkingDirectory();
600 mapi
601 mapi $factory = new PHP_PMD_RuleSetFactory();
602 mapi $ruleSets = $factory->createRuleSets('refset-exclude-one');
603 mapi
604 mapi $rules = $ruleSets[0]->getRules();
605 mapi $this->assertEquals(1, iterator_count($rules));
606 mapi }
607 mapi
608 mapi /**
609 mapi * testCreateRuleSetsWithRuleReferenceNotContainsExcludedRules
610 mapi *
611 mapi * @return void
612 mapi * @covers PHP_PMD_RuleSetFactory
613 mapi * @group phpmd
614 mapi * @group unittest
615 mapi */
616 mapi public function testCreateRuleSetsWithRuleReferenceNotContainsExcludedRules()
617 mapi {
618 mapi self::changeWorkingDirectory();
619 mapi
620 mapi $factory = new PHP_PMD_RuleSetFactory();
621 mapi $ruleSets = $factory->createRuleSets('refset-exclude-all');
622 mapi
623 mapi $rules = $ruleSets[0]->getRules();
624 mapi $this->assertEquals(0, iterator_count($rules));
625 mapi }
626 mapi
627 mapi /**
628 41 mapi * Tests that the factory throws the expected exception for an invalid ruleset
629 mapi * identifier.
630 mapi *
631 mapi * @return void
632 80 mapi * @covers PHP_PMD_RuleSetFactory
633 81 mapi * @covers PHP_PMD_RuleSetNotFoundException
634 80 mapi * @group phpmd
635 mapi * @group unittest
636 41 mapi */
637 79 mapi public function testCreateRuleSetsThrowsExceptionForInvalidIdentifier()
638 41 mapi {
639 mapi $factory = new PHP_PMD_RuleSetFactory();
640 mapi
641 mapi $this->setExpectedException(
642 mapi 'PHP_PMD_RuleSetNotFoundException',
643 mapi 'Cannot find specified rule-set "foo-bar-ruleset-23".'
644 mapi );
645 mapi
646 mapi $factory->createRuleSets('foo-bar-ruleset-23');
647 mapi }
648 mapi
649 mapi /**
650 mapi * Tests that the factory throws an exception when the source code filename
651 mapi * for the configured rule does not exist.
652 mapi *
653 mapi * @return void
654 80 mapi * @covers PHP_PMD_RuleSetFactory
655 81 mapi * @covers PHP_PMD_RuleClassFileNotFoundException
656 80 mapi * @group phpmd
657 mapi * @group unittest
658 41 mapi */
659 80 mapi public function testCreateRuleSetsThrowsExceptionWhenClassFileNotInIncludePath()
660 41 mapi {
661 mapi $fileName = self::createFileUri('rulesets/set-class-file-not-found.xml');
662 mapi $factory = new PHP_PMD_RuleSetFactory();
663 mapi
664 mapi $this->setExpectedException(
665 mapi 'PHP_PMD_RuleClassFileNotFoundException',
666 59 mapi 'Cannot load source file for class: rules_ClassFileNotFoundRule'
667 41 mapi );
668 mapi
669 mapi $factory->createRuleSets($fileName);
670 mapi }
671 mapi
672 mapi /**
673 mapi * Tests that the factory throws the expected exception when a rule class
674 mapi * cannot be found.
675 mapi *
676 mapi * @return void
677 80 mapi * @covers PHP_PMD_RuleSetFactory
678 81 mapi * @covers PHP_PMD_RuleClassNotFoundException
679 80 mapi * @group phpmd
680 mapi * @group unittest
681 41 mapi */
682 79 mapi public function testCreateRuleSetThrowsExceptionWhenFileNotContainsClass()
683 41 mapi {
684 mapi $fileName = self::createFileUri('rulesets/set-class-not-found.xml');
685 mapi $factory = new PHP_PMD_RuleSetFactory();
686 mapi
687 mapi $this->setExpectedException(
688 mapi 'PHP_PMD_RuleClassNotFoundException',
689 59 mapi 'Cannot find rule class: rules_ClassNotFoundRule'
690 41 mapi );
691 mapi
692 mapi $factory->createRuleSets($fileName);
693 mapi }
694 80 mapi
695 mapi /**
696 83 mapi * Tests that the factory throws the expected exception when a rule class
697 mapi * cannot be found.
698 mapi *
699 mapi * @return void
700 mapi * @covers PHP_PMD_RuleSetFactory
701 mapi * @covers PHP_PMD_RuleClassNotFoundException
702 mapi * @group phpmd
703 mapi * @group unittest
704 115 mapi * @expectedException RuntimeException
705 83 mapi */
706 mapi public function testCreateRuleSetsThrowsExpectedExceptionForInvalidXmlFile()
707 mapi {
708 mapi $fileName = self::createFileUri('rulesets/set-invalid-xml.xml');
709 mapi
710 mapi $factory = new PHP_PMD_RuleSetFactory();
711 mapi $factory->createRuleSets($fileName);
712 mapi }
713 mapi
714 mapi /**
715 80 mapi * Invokes the <b>createRuleSets()</b> of the {@link PHP_PMD_RuleSetFactory}
716 mapi * class.
717 mapi *
718 mapi * @param string $file At least one rule configuration file name. You can
719 mapi * also pass multiple parameters with ruleset configuration files.
720 mapi *
721 mapi * @return array(PHP_PMD_RuleSet)
722 mapi */
723 mapi private function _createRuleSetsFromAbsoluteFiles($file)
724 mapi {
725 mapi $files = func_get_args();
726 mapi $files = array_map(array(__CLASS__, 'createFileUri'), $files);
727 mapi
728 mapi return call_user_func_array(array($this, '_createRuleSetsFromFiles'), $files);
729 mapi }
730 mapi
731 mapi /**
732 mapi * Invokes the <b>createRuleSets()</b> of the {@link PHP_PMD_RuleSetFactory}
733 mapi * class.
734 mapi *
735 mapi * @param string $file At least one rule configuration file name. You can
736 mapi * also pass multiple parameters with ruleset configuration files.
737 mapi *
738 mapi * @return array(PHP_PMD_RuleSet)
739 mapi */
740 mapi private function _createRuleSetsFromFiles($file)
741 mapi {
742 115 mapi $args = func_get_args();
743 mapi
744 80 mapi $factory = new PHP_PMD_RuleSetFactory();
745 115 mapi return $factory->createRuleSets(join(',', $args));
746 80 mapi }
747 115 mapi }