PageRenderTime 27ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/PHP/Depend/Metrics/Inheritance/AnalyzerTest.php

https://github.com/proofek/pdepend
PHP | 440 lines | 134 code | 37 blank | 269 comment | 0 complexity | 7674ed82c78af3c9ae16c67a984a8a2c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of PHP_Depend.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2008-2010, Manuel Pichler <mapi@pdepend.org>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category QualityAssurance
  40. * @package PHP_Depend
  41. * @subpackage Metrics
  42. * @author Manuel Pichler <mapi@pdepend.org>
  43. * @copyright 2008-2010 Manuel Pichler. All rights reserved.
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id$
  46. * @link http://pdepend.org/
  47. */
  48. require_once dirname(__FILE__) . '/../AbstractTest.php';
  49. require_once 'PHP/Depend/Code/Class.php';
  50. require_once 'PHP/Depend/Code/File.php';
  51. require_once 'PHP/Depend/Code/Interface.php';
  52. require_once 'PHP/Depend/Code/NodeIterator.php';
  53. require_once 'PHP/Depend/Code/Package.php';
  54. require_once 'PHP/Depend/Code/Filter/Package.php';
  55. require_once 'PHP/Depend/Code/Filter/Collection.php';
  56. require_once 'PHP/Depend/Metrics/Inheritance/Analyzer.php';
  57. /**
  58. * Test case for the inheritance analyzer.
  59. *
  60. * @category QualityAssurance
  61. * @package PHP_Depend
  62. * @subpackage Metrics
  63. * @author Manuel Pichler <mapi@pdepend.org>
  64. * @copyright 2008-2010 Manuel Pichler. All rights reserved.
  65. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  66. * @version Release: @package_version@
  67. * @link http://pdepend.org/
  68. */
  69. class PHP_Depend_Metrics_Inheritance_AnalyzerTest extends PHP_Depend_Metrics_AbstractTest
  70. {
  71. /**
  72. * Tests that the analyzer calculates the correct average number of derived
  73. * classes.
  74. *
  75. * @return void
  76. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  77. * @group pdepend
  78. * @group pdepend::metrics
  79. * @group pdepend::metrics::inheritance
  80. * @group unittest
  81. */
  82. public function testAnalyzerCalculatesCorrectANDCValue()
  83. {
  84. $filter = PHP_Depend_Code_Filter_Collection::getInstance();
  85. $filter->setFilter(new PHP_Depend_Code_Filter_Package(array('library')));
  86. $packages = self::parseTestCaseSource(__METHOD__);
  87. $analyzer = new PHP_Depend_Metrics_Inheritance_Analyzer();
  88. $analyzer->analyze($packages);
  89. $project = $analyzer->getProjectMetrics();
  90. $this->assertEquals(0.7368, $project['andc'], null, 0.0001);
  91. }
  92. /**
  93. * Tests that the analyzer calculates the correct average hierarchy height.
  94. *
  95. * @return void
  96. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  97. * @group pdepend
  98. * @group pdepend::metrics
  99. * @group pdepend::metrics::inheritance
  100. * @group unittest
  101. */
  102. public function testAnalyzerCalculatesCorrectAHHValue()
  103. {
  104. $filter = PHP_Depend_Code_Filter_Collection::getInstance();
  105. $filter->setFilter(new PHP_Depend_Code_Filter_Package(array('library')));
  106. $packages = self::parseTestCaseSource(__METHOD__);
  107. $analyzer = new PHP_Depend_Metrics_Inheritance_Analyzer();
  108. $analyzer->analyze($packages);
  109. $project = $analyzer->getProjectMetrics();
  110. $this->assertEquals(1, $project['ahh']);
  111. }
  112. /**
  113. * testCalculatesExpectedNoccMetricForClassWithoutChildren
  114. *
  115. * @return void
  116. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  117. * @group pdepend
  118. * @group pdepend::metrics
  119. * @group pdepend::metrics::inheritance
  120. * @group unittest
  121. */
  122. public function testCalculatesExpectedNoccMetricForClassWithoutChildren()
  123. {
  124. $this->assertEquals(0, $this->_getCalculatedMetric(__METHOD__, 'nocc'));
  125. }
  126. /**
  127. * testCalculatesExpectedNoccMetricForClassWithDirectChildren
  128. *
  129. * @return void
  130. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  131. * @group pdepend
  132. * @group pdepend::metrics
  133. * @group pdepend::metrics::inheritance
  134. * @group unittest
  135. */
  136. public function testCalculatesExpectedNoccMetricForClassWithDirectChildren()
  137. {
  138. $this->assertEquals(3, $this->_getCalculatedMetric(__METHOD__, 'nocc'));
  139. }
  140. /**
  141. * testCalculatesExpectedNoccMetricForClassWithDirectAndIndirectChildren
  142. *
  143. * @return void
  144. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  145. * @group pdepend
  146. * @group pdepend::metrics
  147. * @group pdepend::metrics::inheritance
  148. * @group unittest
  149. */
  150. public function testCalculatesExpectedNoccMetricForClassWithDirectAndIndirectChildren()
  151. {
  152. $this->assertEquals(1, $this->_getCalculatedMetric(__METHOD__, 'nocc'));
  153. }
  154. /**
  155. * Tests that the analyzer calculates the correct DIT values.
  156. *
  157. * @return void
  158. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  159. * @group pdepend
  160. * @group pdepend::metrics
  161. * @group pdepend::metrics::inheritance
  162. * @group unittest
  163. */
  164. public function testCalculateDITMetricNoInheritance()
  165. {
  166. $this->assertEquals(0, $this->_getCalculatedMetric(__METHOD__, 'dit'));
  167. }
  168. /**
  169. * Tests that the analyzer calculates the correct DIT values.
  170. *
  171. * @return void
  172. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  173. * @group pdepend
  174. * @group pdepend::metrics
  175. * @group pdepend::metrics::inheritance
  176. * @group unittest
  177. */
  178. public function testCalculateDITMetricOneLevelInheritance()
  179. {
  180. $this->assertEquals(1, $this->_getCalculatedMetric(__METHOD__, 'dit'));
  181. }
  182. /**
  183. * Tests that the analyzer calculates the correct DIT values.
  184. *
  185. * @return void
  186. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  187. * @group pdepend
  188. * @group pdepend::metrics
  189. * @group pdepend::metrics::inheritance
  190. * @group unittest
  191. */
  192. public function testCalculateDITMetricTwoLevelNoInheritance()
  193. {
  194. $this->assertEquals(2, $this->_getCalculatedMetric(__METHOD__, 'dit'));
  195. }
  196. /**
  197. * Tests that the analyzer calculates the correct DIT values.
  198. *
  199. * @return void
  200. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  201. * @group pdepend
  202. * @group pdepend::metrics
  203. * @group pdepend::metrics::inheritance
  204. * @group unittest
  205. */
  206. public function testCalculateDITMetricThreeLevelNoInheritance()
  207. {
  208. $this->assertEquals(3, $this->_getCalculatedMetric(__METHOD__, 'dit'));
  209. }
  210. /**
  211. * Tests that the analyzer calculates the correct DIT values.
  212. *
  213. * @return void
  214. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  215. * @group pdepend
  216. * @group pdepend::metrics
  217. * @group pdepend::metrics::inheritance
  218. * @group unittest
  219. */
  220. public function testCalculateDITMetricFourLevelNoInheritance()
  221. {
  222. $this->assertEquals(4, $this->_getCalculatedMetric(__METHOD__, 'dit'));
  223. }
  224. /**
  225. * testCalculateDITMetricForUnknownParentIncrementsMetricWithTwo
  226. *
  227. * @return void
  228. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  229. * @group pdepend
  230. * @group pdepend::metrics
  231. * @group pdepend::metrics::inheritance
  232. * @group unittest
  233. */
  234. public function testCalculateDITMetricForUnknownParentIncrementsMetricWithTwo()
  235. {
  236. $this->assertEquals(3, $this->_getCalculatedMetric(__METHOD__, 'dit'));
  237. }
  238. /**
  239. * testCalculateDITMetricForInternalParentIncrementsMetricWithTwo
  240. *
  241. * @return void
  242. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  243. * @group pdepend
  244. * @group pdepend::metrics
  245. * @group pdepend::metrics::inheritance
  246. * @group unittest
  247. */
  248. public function testCalculateDITMetricForInternalParentIncrementsMetricWithTwo()
  249. {
  250. $this->assertEquals(3, $this->_getCalculatedMetric(__METHOD__, 'dit'));
  251. }
  252. /**
  253. * Tests that {@link PHP_Depend_Metrics_Inheritance_Analyzer::analyze()}
  254. * calculates the expected DIT values.
  255. *
  256. * @return void
  257. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  258. * @group pdepend
  259. * @group pdepend::metrics
  260. * @group pdepend::metrics::inheritance
  261. * @group unittest
  262. */
  263. public function testCalculateDepthOfInheritanceForSeveralClasses()
  264. {
  265. $packages = self::parseTestCaseSource(__METHOD__);
  266. $package = $packages->current();
  267. $analyzer = new PHP_Depend_Metrics_Inheritance_Analyzer();
  268. $analyzer->analyze($packages);
  269. $actual = array();
  270. foreach ($package->getClasses() as $class) {
  271. $metrics = $analyzer->getNodeMetrics($class);
  272. $actual[$class->getName()] = $metrics['dit'];
  273. }
  274. ksort($actual);
  275. $expected = array(
  276. 'A' => 0,
  277. 'B' => 1,
  278. 'C' => 1,
  279. 'D' => 2,
  280. 'E' => 3,
  281. );
  282. $this->assertEquals($expected, $actual);
  283. }
  284. /**
  285. * testCalculatesExpectedMaxDepthOfInheritanceTreeMetric
  286. *
  287. * @return void
  288. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  289. * @group pdepend
  290. * @group pdepend::metrics
  291. * @group pdepend::metrics::inheritance
  292. * @group unittest
  293. */
  294. public function testCalculatesExpectedMaxDepthOfInheritanceTreeMetric()
  295. {
  296. $analyzer = new PHP_Depend_Metrics_Inheritance_Analyzer();
  297. $analyzer->analyze(self::parseTestCaseSource(__METHOD__));
  298. $metrics = $analyzer->getProjectMetrics();
  299. $this->assertEquals(3, $metrics['maxDIT']);
  300. }
  301. /**
  302. * testCalculatesExpectedNoamMetricForClassWithoutParent
  303. *
  304. * @return void
  305. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  306. * @group pdepend
  307. * @group pdepend::metrics
  308. * @group pdepend::metrics::inheritance
  309. * @group unittest
  310. */
  311. public function testCalculatesExpectedNoamMetricForClassWithoutParent()
  312. {
  313. $this->assertEquals(0, $this->_getCalculatedMetric(__METHOD__, 'noam'));
  314. }
  315. /**
  316. * testCalculatesExpectedNoamMetricForClassWithDirectParent
  317. *
  318. * @return void
  319. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  320. * @group pdepend
  321. * @group pdepend::metrics
  322. * @group pdepend::metrics::inheritance
  323. * @group unittest
  324. */
  325. public function testCalculatesExpectedNoamMetricForClassWithDirectParent()
  326. {
  327. $this->assertEquals(2, $this->_getCalculatedMetric(__METHOD__, 'noam'));
  328. }
  329. /**
  330. * testCalculatesExpectedNoamMetricForClassWithIndirectParent
  331. *
  332. * @return void
  333. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  334. * @group pdepend
  335. * @group pdepend::metrics
  336. * @group pdepend::metrics::inheritance
  337. * @group unittest
  338. */
  339. public function testCalculatesExpectedNoamMetricForClassWithIndirectParent()
  340. {
  341. $this->assertEquals(2, $this->_getCalculatedMetric(__METHOD__, 'noam'));
  342. }
  343. /**
  344. * testCalculatesExpectedNoomMetricForClassWithoutParent
  345. *
  346. * @return void
  347. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  348. * @group pdepend
  349. * @group pdepend::metrics
  350. * @group pdepend::metrics::inheritance
  351. * @group unittest
  352. */
  353. public function testCalculatesExpectedNoomMetricForClassWithoutParent()
  354. {
  355. $this->assertEquals(0, $this->_getCalculatedMetric(__METHOD__, 'noom'));
  356. }
  357. /**
  358. * testCalculatesExpectedNoomMetricForClassWithParent
  359. *
  360. * @return void
  361. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  362. * @group pdepend
  363. * @group pdepend::metrics
  364. * @group pdepend::metrics::inheritance
  365. * @group unittest
  366. */
  367. public function testCalculatesExpectedNoomMetricForClassWithParent()
  368. {
  369. $this->assertEquals(2, $this->_getCalculatedMetric(__METHOD__, 'noom'));
  370. }
  371. /**
  372. * testCalculatesExpectedNoomMetricForClassWithParentPrivateMethods
  373. *
  374. * @return void
  375. * @covers PHP_Depend_Metrics_Inheritance_Analyzer
  376. * @group pdepend
  377. * @group pdepend::metrics
  378. * @group pdepend::metrics::inheritance
  379. * @group unittest
  380. */
  381. public function testCalculatesExpectedNoomMetricForClassWithParentPrivateMethods()
  382. {
  383. $this->assertEquals(1, $this->_getCalculatedMetric(__METHOD__, 'noom'));
  384. }
  385. /**
  386. * Analyzes the source associated with the calling test and returns the
  387. * calculated metric value.
  388. *
  389. * @param string $testCase Name of the calling test case.
  390. * @param string $metric Name of the searched metric.
  391. *
  392. * @return mixed
  393. */
  394. private function _getCalculatedMetric($testCase, $metric)
  395. {
  396. $packages = self::parseTestCaseSource($testCase);
  397. $package = $packages->current();
  398. $analyzer = new PHP_Depend_Metrics_Inheritance_Analyzer();
  399. $analyzer->analyze($packages);
  400. $metrics = $analyzer->getNodeMetrics($package->getClasses()->current());
  401. return $metrics[$metric];
  402. }
  403. }