DetectorTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /*
  3. * This file is part of PHP Copy/Paste Detector (PHPCPD).
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. if (!defined('TEST_FILES_PATH')) {
  11. define(
  12. 'TEST_FILES_PATH',
  13. dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR
  14. );
  15. }
  16. use PHPUnit\Framework\TestCase;
  17. class PHPCPD_DetectorTest extends TestCase
  18. {
  19. /**
  20. * @covers SebastianBergmann\PHPCPD\Detector\Detector::copyPasteDetection
  21. * @covers SebastianBergmann\PHPCPD\CodeClone::getLines
  22. * @dataProvider strategyProvider
  23. */
  24. public function testDetectingSimpleClonesWorks($strategy)
  25. {
  26. $detector = new SebastianBergmann\PHPCPD\Detector\Detector(new $strategy);
  27. $clones = $detector->copyPasteDetection(
  28. [TEST_FILES_PATH . 'Math.php']
  29. );
  30. $clones = $clones->getClones();
  31. $files = $clones[0]->getFiles();
  32. $file = current($files);
  33. $this->assertEquals(TEST_FILES_PATH . 'Math.php', $file->getName());
  34. $this->assertEquals(75, $file->getStartLine());
  35. $file = next($files);
  36. $this->assertEquals(TEST_FILES_PATH . 'Math.php', $file->getName());
  37. $this->assertEquals(139, $file->getStartLine());
  38. $this->assertEquals(59, $clones[0]->getSize());
  39. $this->assertEquals(136, $clones[0]->getTokens());
  40. $this->assertEquals(
  41. ' public function div($v1, $v2)
  42. {
  43. $v3 = $v1 / ($v2 + $v1);
  44. if ($v3 > 14)
  45. {
  46. $v4 = 0;
  47. for ($i = 0; $i < $v3; $i++)
  48. {
  49. $v4 += ($v2 * $i);
  50. }
  51. }
  52. $v5 = ($v4 < $v3 ? ($v3 - $v4) : ($v4 - $v3));
  53. $v6 = ($v1 * $v2 * $v3 * $v4 * $v5);
  54. $d = array($v1, $v2, $v3, $v4, $v5, $v6);
  55. $v7 = 1;
  56. for ($i = 0; $i < $v6; $i++)
  57. {
  58. shuffle( $d );
  59. $v7 = $v7 + $i * end($d);
  60. }
  61. $v8 = $v7;
  62. foreach ( $d as $x )
  63. {
  64. $v8 *= $x;
  65. }
  66. $v3 = $v1 / ($v2 + $v1);
  67. if ($v3 > 14)
  68. {
  69. $v4 = 0;
  70. for ($i = 0; $i < $v3; $i++)
  71. {
  72. $v4 += ($v2 * $i);
  73. }
  74. }
  75. $v5 = ($v4 < $v3 ? ($v3 - $v4) : ($v4 - $v3));
  76. $v6 = ($v1 * $v2 * $v3 * $v4 * $v5);
  77. $d = array($v1, $v2, $v3, $v4, $v5, $v6);
  78. $v7 = 1;
  79. for ($i = 0; $i < $v6; $i++)
  80. {
  81. shuffle( $d );
  82. $v7 = $v7 + $i * end($d);
  83. }
  84. $v8 = $v7;
  85. foreach ( $d as $x )
  86. {
  87. $v8 *= $x;
  88. }
  89. return $v8;
  90. ',
  91. $clones[0]->getLines()
  92. );
  93. }
  94. /**
  95. * @covers SebastianBergmann\PHPCPD\Detector\Detector::copyPasteDetection
  96. * @dataProvider strategyProvider
  97. */
  98. public function testDetectingExactDuplicateFilesWorks($strategy)
  99. {
  100. $detector = new SebastianBergmann\PHPCPD\Detector\Detector(new $strategy);
  101. $clones = $detector->copyPasteDetection([
  102. TEST_FILES_PATH . 'a.php',
  103. TEST_FILES_PATH . 'b.php'
  104. ], 20, 60);
  105. $clones = $clones->getClones();
  106. $files = $clones[0]->getFiles();
  107. $file = current($files);
  108. $this->assertCount(1, $clones);
  109. $this->assertEquals(TEST_FILES_PATH . 'a.php', $file->getName());
  110. $this->assertEquals(4, $file->getStartLine());
  111. $file = next($files);
  112. $this->assertEquals(TEST_FILES_PATH . 'b.php', $file->getName());
  113. $this->assertEquals(4, $file->getStartLine());
  114. $this->assertEquals(20, $clones[0]->getSize());
  115. $this->assertEquals(60, $clones[0]->getTokens());
  116. }
  117. /**
  118. * @covers SebastianBergmann\PHPCPD\Detector\Detector::copyPasteDetection
  119. * @dataProvider strategyProvider
  120. */
  121. public function testDetectingClonesInMoreThanTwoFiles($strategy)
  122. {
  123. $detector = new SebastianBergmann\PHPCPD\Detector\Detector(new $strategy);
  124. $clones = $detector->copyPasteDetection(
  125. [
  126. TEST_FILES_PATH . 'a.php',
  127. TEST_FILES_PATH . 'b.php',
  128. TEST_FILES_PATH . 'c.php',
  129. ],
  130. 20,
  131. 60
  132. );
  133. $clones = $clones->getClones();
  134. $files = $clones[0]->getFiles();
  135. sort($files);
  136. $file = current($files);
  137. $this->assertCount(1, $clones);
  138. $this->assertEquals(TEST_FILES_PATH . 'a.php', $file->getName());
  139. $this->assertEquals(4, $file->getStartLine());
  140. $file = next($files);
  141. $this->assertEquals(TEST_FILES_PATH . 'b.php', $file->getName());
  142. $this->assertEquals(4, $file->getStartLine());
  143. $file = next($files);
  144. $this->assertEquals(TEST_FILES_PATH . 'c.php', $file->getName());
  145. $this->assertEquals(4, $file->getStartLine());
  146. }
  147. /**
  148. * @covers SebastianBergmann\PHPCPD\Detector\Detector::copyPasteDetection
  149. * @dataProvider strategyProvider
  150. */
  151. public function testClonesAreIgnoredIfTheySpanLessTokensThanMinTokens($strategy)
  152. {
  153. $detector = new SebastianBergmann\PHPCPD\Detector\Detector(new $strategy);
  154. $clones = $detector->copyPasteDetection(
  155. [
  156. TEST_FILES_PATH . 'a.php',
  157. TEST_FILES_PATH . 'b.php'
  158. ],
  159. 20,
  160. 61
  161. );
  162. $this->assertCount(0, $clones->getClones());
  163. }
  164. /**
  165. * @covers SebastianBergmann\PHPCPD\Detector\Detector::copyPasteDetection
  166. * @dataProvider strategyProvider
  167. */
  168. public function testClonesAreIgnoredIfTheySpanLessLinesThanMinLines($strategy)
  169. {
  170. $detector = new SebastianBergmann\PHPCPD\Detector\Detector(new $strategy);
  171. $clones = $detector->copyPasteDetection(
  172. [
  173. TEST_FILES_PATH . 'a.php',
  174. TEST_FILES_PATH . 'b.php'
  175. ],
  176. 21,
  177. 60
  178. );
  179. $this->assertCount(0, $clones->getClones());
  180. }
  181. /**
  182. * @covers SebastianBergmann\PHPCPD\Detector\Detector::copyPasteDetection
  183. * @dataProvider strategyProvider
  184. */
  185. public function testFuzzyClonesAreFound($strategy)
  186. {
  187. $detector = new SebastianBergmann\PHPCPD\Detector\Detector(new $strategy);
  188. $clones = $detector->copyPasteDetection(
  189. [
  190. TEST_FILES_PATH . 'a.php',
  191. TEST_FILES_PATH . 'd.php'
  192. ],
  193. 5,
  194. 20,
  195. true
  196. );
  197. $clones = $clones->getClones();
  198. $this->assertCount(1, $clones);
  199. }
  200. /**
  201. * @covers SebastianBergmann\PHPCPD\Detector\Detector::copyPasteDetection
  202. * @dataProvider strategyProvider
  203. */
  204. public function testStripComments($strategy)
  205. {
  206. $detector = new SebastianBergmann\PHPCPD\Detector\Detector(new $strategy);
  207. $clones = $detector->copyPasteDetection(
  208. [
  209. TEST_FILES_PATH . 'e.php',
  210. TEST_FILES_PATH . 'f.php'
  211. ],
  212. 8,
  213. 10,
  214. true
  215. );
  216. $clones = $clones->getClones();
  217. $this->assertCount(0, $clones);
  218. $clones = $detector->copyPasteDetection(
  219. [
  220. TEST_FILES_PATH . 'e.php',
  221. TEST_FILES_PATH . 'f.php'
  222. ],
  223. 7,
  224. 10,
  225. true
  226. );
  227. $clones = $clones->getClones();
  228. $this->assertCount(1, $clones);
  229. }
  230. public function strategyProvider()
  231. {
  232. return [
  233. ['SebastianBergmann\\PHPCPD\\Detector\\Strategy\\DefaultStrategy']
  234. ];
  235. }
  236. }