1010
1111class CyclomaticComplexityVisitorTest extends \PHPUnit_Framework_TestCase
1212{
13+ /**
14+ * @dataProvider provideExamplesForCcn
15+ */
16+ public function testCcnOfClassesIsWellCalculated ($ example , $ classname , $ expectedCcn )
17+ {
18+ $ metrics = new Metrics ();
19+
20+ $ parser = (new ParserFactory )->create (ParserFactory::PREFER_PHP7 );
21+ $ traverser = new NodeTraverser ();
22+ $ traverser ->addVisitor (new NameResolver ());
23+ $ traverser ->addVisitor (new ClassEnumVisitor ($ metrics ));
24+ $ traverser ->addVisitor (new CyclomaticComplexityVisitor ($ metrics ));
25+
26+ $ code = file_get_contents ($ example );
27+ $ stmts = $ parser ->parse ($ code );
28+ $ traverser ->traverse ($ stmts );
29+
30+ $ this ->assertSame ($ expectedCcn , $ metrics ->get ($ classname )->get ('ccn ' ));
31+ }
32+
1333 /**
1434 * @dataProvider provideExamplesForWmc
1535 */
@@ -27,7 +47,7 @@ public function testWeightedMethodCountOfClassesIsWellCalculated($example, $clas
2747 $ stmts = $ parser ->parse ($ code );
2848 $ traverser ->traverse ($ stmts );
2949
30- $ this ->assertSame ($ expectedWmc , $ metrics ->get ($ classname )->get ('ccn ' ));
50+ $ this ->assertSame ($ expectedWmc , $ metrics ->get ($ classname )->get ('wmc ' ));
3151 }
3252
3353 /**
@@ -64,6 +84,20 @@ public static function provideExamplesForWmc()
6484 ];
6585 }
6686
87+ public static function provideExamplesForCcn ()
88+ {
89+ return [
90+ 'A ' => [__DIR__ . '/../../examples/cyclomatic1.php ' , 'A ' , 8 ],
91+ 'B ' => [__DIR__ . '/../../examples/cyclomatic1.php ' , 'B ' , 4 ],
92+ 'Foo \\C ' => [__DIR__ . '/../../examples/cyclomatic_anon.php ' , 'Foo \\C ' , 1 ],
93+ 'SwitchCase ' => [__DIR__ . '/../../examples/cyclomatic_full.php ' , 'SwitchCase ' , 4 ],
94+ 'IfElseif ' => [__DIR__ . '/../../examples/cyclomatic_full.php ' , 'IfElseif ' , 7 ],
95+ 'Loops ' => [__DIR__ . '/../../examples/cyclomatic_full.php ' , 'Loops ' , 5 ],
96+ 'CatchIt ' => [__DIR__ . '/../../examples/cyclomatic_full.php ' , 'CatchIt ' , 3 ],
97+ 'Logical ' => [__DIR__ . '/../../examples/cyclomatic_full.php ' , 'Logical ' , 11 ],
98+ ];
99+ }
100+
67101 public static function provideExamplesForMaxCc ()
68102 {
69103 return [
0 commit comments