ProjectCodeTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\AutoReview;
  12. if (!class_exists(\PHPUnit\Runner\Version::class)) {
  13. class_alias('PHPUnit_Runner_Version', \PHPUnit\Runner\Version::class);
  14. }
  15. use PhpCsFixer\DocBlock\DocBlock;
  16. use PhpCsFixer\Preg;
  17. use PhpCsFixer\Tests\TestCase;
  18. use PhpCsFixer\Tokenizer\Token;
  19. use PhpCsFixer\Tokenizer\Tokens;
  20. use Symfony\Component\Finder\Finder;
  21. use Symfony\Component\Finder\SplFileInfo;
  22. /**
  23. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  24. *
  25. * @internal
  26. *
  27. * @coversNothing
  28. * @group auto-review
  29. * @group covers-nothing
  30. */
  31. final class ProjectCodeTest extends TestCase
  32. {
  33. /**
  34. * This structure contains older classes that are not yet covered by tests.
  35. *
  36. * It may only shrink, never add anything to it.
  37. *
  38. * @var string[]
  39. */
  40. private static $classesWithoutTests = [
  41. \PhpCsFixer\Console\SelfUpdate\GithubClient::class,
  42. \PhpCsFixer\Doctrine\Annotation\Tokens::class,
  43. \PhpCsFixer\Fixer\Operator\AlignDoubleArrowFixerHelper::class,
  44. \PhpCsFixer\Fixer\Operator\AlignEqualsFixerHelper::class,
  45. \PhpCsFixer\Fixer\Whitespace\NoExtraConsecutiveBlankLinesFixer::class,
  46. \PhpCsFixer\Runner\FileCachingLintingIterator::class,
  47. \PhpCsFixer\Test\AccessibleObject::class,
  48. ];
  49. public function testThatClassesWithoutTestsVarIsProper()
  50. {
  51. $unknownClasses = array_filter(
  52. self::$classesWithoutTests,
  53. static function ($class) { return !class_exists($class) && !trait_exists($class); }
  54. );
  55. static::assertSame([], $unknownClasses);
  56. }
  57. /**
  58. * @param string $className
  59. *
  60. * @dataProvider provideSrcConcreteClassCases
  61. */
  62. public function testThatSrcClassHaveTestClass($className)
  63. {
  64. $testClassName = str_replace('PhpCsFixer', 'PhpCsFixer\\Tests', $className).'Test';
  65. if (\in_array($className, self::$classesWithoutTests, true)) {
  66. static::assertFalse(class_exists($testClassName), sprintf('Class "%s" already has tests, so it should be removed from "%s::$classesWithoutTests".', $className, __CLASS__));
  67. static::markTestIncomplete(sprintf('Class "%s" has no tests yet, please help and add it.', $className));
  68. }
  69. static::assertTrue(class_exists($testClassName), sprintf('Expected test class "%s" for "%s" not found.', $testClassName, $className));
  70. static::assertTrue(is_subclass_of($testClassName, TestCase::class), sprintf('Expected test class "%s" to be a subclass of "\PhpCsFixer\Tests\TestCase".', $testClassName));
  71. }
  72. /**
  73. * @param string $className
  74. *
  75. * @dataProvider provideSrcClassesNotAbuseInterfacesCases
  76. */
  77. public function testThatSrcClassesNotAbuseInterfaces($className)
  78. {
  79. $rc = new \ReflectionClass($className);
  80. $allowedMethods = array_map(
  81. function (\ReflectionClass $interface) {
  82. return $this->getPublicMethodNames($interface);
  83. },
  84. $rc->getInterfaces()
  85. );
  86. if (\count($allowedMethods)) {
  87. $allowedMethods = array_unique(array_merge(...array_values($allowedMethods)));
  88. }
  89. $allowedMethods[] = '__construct';
  90. $allowedMethods[] = '__destruct';
  91. $allowedMethods[] = '__wakeup';
  92. $exceptionMethods = [
  93. 'configure', // due to AbstractFixer::configure
  94. 'getConfigurationDefinition', // due to AbstractFixer::getConfigurationDefinition
  95. 'getDefaultConfiguration', // due to AbstractFixer::getDefaultConfiguration
  96. 'setWhitespacesConfig', // due to AbstractFixer::setWhitespacesConfig
  97. ];
  98. // @TODO: 3.0 should be removed
  99. $exceptionMethodsPerClass = [
  100. \PhpCsFixer\Config::class => ['create'],
  101. \PhpCsFixer\Event\Event::class => ['stopPropagation'],
  102. \PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer::class => ['fixSpace'],
  103. ];
  104. $definedMethods = $this->getPublicMethodNames($rc);
  105. $extraMethods = array_diff(
  106. $definedMethods,
  107. $allowedMethods,
  108. $exceptionMethods,
  109. isset($exceptionMethodsPerClass[$className]) ? $exceptionMethodsPerClass[$className] : []
  110. );
  111. sort($extraMethods);
  112. static::assertEmpty(
  113. $extraMethods,
  114. sprintf(
  115. "Class '%s' should not have public methods that are not part of implemented interfaces.\nViolations:\n%s",
  116. $className,
  117. implode("\n", array_map(static function ($item) {
  118. return " * {$item}";
  119. }, $extraMethods))
  120. )
  121. );
  122. }
  123. /**
  124. * @param string $className
  125. *
  126. * @dataProvider provideSrcClassCases
  127. */
  128. public function testThatSrcClassesNotExposeProperties($className)
  129. {
  130. $rc = new \ReflectionClass($className);
  131. if (\PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer::class === $className) {
  132. static::markTestIncomplete(sprintf(
  133. 'Public properties of fixer `%s` will be removed on 3.0.',
  134. \PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer::class
  135. ));
  136. }
  137. static::assertEmpty(
  138. $rc->getProperties(\ReflectionProperty::IS_PUBLIC),
  139. sprintf('Class \'%s\' should not have public properties.', $className)
  140. );
  141. if ($rc->isFinal()) {
  142. return;
  143. }
  144. $allowedProps = [];
  145. $definedProps = $rc->getProperties(\ReflectionProperty::IS_PROTECTED);
  146. if (false !== $rc->getParentClass()) {
  147. $allowedProps = $rc->getParentClass()->getProperties(\ReflectionProperty::IS_PROTECTED);
  148. }
  149. $allowedProps = array_map(static function (\ReflectionProperty $item) {
  150. return $item->getName();
  151. }, $allowedProps);
  152. $definedProps = array_map(static function (\ReflectionProperty $item) {
  153. return $item->getName();
  154. }, $definedProps);
  155. $exceptionPropsPerClass = [
  156. \PhpCsFixer\AbstractPhpdocTypesFixer::class => ['tags'],
  157. \PhpCsFixer\AbstractAlignFixerHelper::class => ['deepestLevel'],
  158. \PhpCsFixer\AbstractFixer::class => ['configuration', 'configurationDefinition', 'whitespacesConfig'],
  159. \PhpCsFixer\AbstractProxyFixer::class => ['proxyFixers'],
  160. \PhpCsFixer\Test\AbstractFixerTestCase::class => ['fixer', 'linter'],
  161. \PhpCsFixer\Test\AbstractIntegrationTestCase::class => ['linter'],
  162. ];
  163. $extraProps = array_diff(
  164. $definedProps,
  165. $allowedProps,
  166. isset($exceptionPropsPerClass[$className]) ? $exceptionPropsPerClass[$className] : []
  167. );
  168. sort($extraProps);
  169. static::assertEmpty(
  170. $extraProps,
  171. sprintf(
  172. "Class '%s' should not have protected properties.\nViolations:\n%s",
  173. $className,
  174. implode("\n", array_map(static function ($item) {
  175. return " * {$item}";
  176. }, $extraProps))
  177. )
  178. );
  179. }
  180. /**
  181. * @dataProvider provideTestClassCases
  182. *
  183. * @param string $testClassName
  184. */
  185. public function testThatTestClassesAreTraitOrAbstractOrFinal($testClassName)
  186. {
  187. $rc = new \ReflectionClass($testClassName);
  188. static::assertTrue(
  189. $rc->isTrait() || $rc->isAbstract() || $rc->isFinal(),
  190. sprintf('Test class %s should be trait, abstract or final.', $testClassName)
  191. );
  192. }
  193. /**
  194. * @dataProvider provideTestClassCases
  195. *
  196. * @param string $testClassName
  197. */
  198. public function testThatTestClassesAreInternal($testClassName)
  199. {
  200. $rc = new \ReflectionClass($testClassName);
  201. $doc = new DocBlock($rc->getDocComment());
  202. static::assertNotEmpty(
  203. $doc->getAnnotationsOfType('internal'),
  204. sprintf('Test class %s should have internal annotation.', $testClassName)
  205. );
  206. }
  207. /**
  208. * @dataProvider provideTestClassCases
  209. *
  210. * @param string $testClassName
  211. */
  212. public function testThatPublicMethodsAreCorrectlyNamed($testClassName)
  213. {
  214. $reflectionClass = new \ReflectionClass($testClassName);
  215. $publicMethods = array_filter(
  216. $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC),
  217. static function (\ReflectionMethod $reflectionMethod) use ($reflectionClass) {
  218. return $reflectionMethod->getDeclaringClass()->getName() === $reflectionClass->getName();
  219. }
  220. );
  221. if ($publicMethods === []) {
  222. $this->addToAssertionCount(1); // no methods to test, all good!
  223. }
  224. foreach ($publicMethods as $method) {
  225. static::assertRegExp(
  226. '/^(test|provide|setUpBeforeClass$|tearDownAfterClass$)/',
  227. $method->getName(),
  228. sprintf('Public method "%s::%s" is not properly named.', $reflectionClass->getName(), $method->getName())
  229. );
  230. }
  231. }
  232. /**
  233. * @dataProvider provideTestClassCases
  234. *
  235. * @param string $testClassName
  236. */
  237. public function testThatDataProvidersAreCorrectlyNamed($testClassName)
  238. {
  239. $usedDataProviderMethodNames = $this->getUsedDataProviderMethodNames($testClassName);
  240. if (empty($dataProviderMethodNames)) {
  241. $this->addToAssertionCount(1); // no data providers to test, all good!
  242. }
  243. foreach ($usedDataProviderMethodNames as $dataProviderMethodName) {
  244. static::assertRegExp('/^provide[A-Z]\S+Cases$/', $dataProviderMethodName, sprintf(
  245. 'Data provider in "%s" with name "%s" is not correctly named.',
  246. $testClassName,
  247. $dataProviderMethodName
  248. ));
  249. }
  250. }
  251. /**
  252. * @dataProvider provideTestClassCases
  253. *
  254. * @param string $testClassName
  255. */
  256. public function testThatDataProvidersAreUsed($testClassName)
  257. {
  258. $reflectionClass = new \ReflectionClass($testClassName);
  259. $definedDataProviders = array_filter(
  260. $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC),
  261. static function (\ReflectionMethod $reflectionMethod) use ($reflectionClass) {
  262. return $reflectionMethod->getDeclaringClass()->getName() === $reflectionClass->getName()
  263. && 'provide' === substr($reflectionMethod->getName(), 0, 7);
  264. }
  265. );
  266. if ($definedDataProviders === []) {
  267. $this->addToAssertionCount(1); // no methods to test, all good!
  268. }
  269. $usedDataProviderMethodNames = $this->getUsedDataProviderMethodNames($testClassName);
  270. foreach ($definedDataProviders as $definedDataProvider) {
  271. static::assertContains(
  272. $definedDataProvider->getName(),
  273. $usedDataProviderMethodNames,
  274. sprintf('Data provider in "%s" with name "%s" is not used.', $definedDataProvider->getDeclaringClass()->getName(), $definedDataProvider->getName())
  275. );
  276. }
  277. }
  278. /**
  279. * @dataProvider provideClassesWherePregFunctionsAreForbiddenCases
  280. *
  281. * @param string $className
  282. */
  283. public function testThereIsNoPregFunctionUsedDirectly($className)
  284. {
  285. $rc = new \ReflectionClass($className);
  286. $tokens = Tokens::fromCode(file_get_contents($rc->getFileName()));
  287. $stringTokens = array_filter(
  288. $tokens->toArray(),
  289. static function (Token $token) {
  290. return $token->isGivenKind(T_STRING);
  291. }
  292. );
  293. $strings = array_map(
  294. static function (Token $token) {
  295. return $token->getContent();
  296. },
  297. $stringTokens
  298. );
  299. $strings = array_unique($strings);
  300. $message = sprintf('Class %s must not use preg_*, it shall use Preg::* instead.', $className);
  301. static::assertNotContains('preg_filter', $strings, $message);
  302. static::assertNotContains('preg_grep', $strings, $message);
  303. static::assertNotContains('preg_match', $strings, $message);
  304. static::assertNotContains('preg_match_all', $strings, $message);
  305. static::assertNotContains('preg_replace', $strings, $message);
  306. static::assertNotContains('preg_replace_callback', $strings, $message);
  307. static::assertNotContains('preg_split', $strings, $message);
  308. }
  309. /**
  310. * @dataProvider provideTestClassCases
  311. *
  312. * @param string $testClassName
  313. */
  314. public function testExpectedInputOrder($testClassName)
  315. {
  316. $reflectionClass = new \ReflectionClass($testClassName);
  317. $publicMethods = array_filter(
  318. $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC),
  319. static function (\ReflectionMethod $reflectionMethod) use ($reflectionClass) {
  320. return $reflectionMethod->getDeclaringClass()->getName() === $reflectionClass->getName();
  321. }
  322. );
  323. if ($publicMethods === []) {
  324. $this->addToAssertionCount(1); // no methods to test, all good!
  325. return;
  326. }
  327. /** @var \ReflectionMethod $method */
  328. foreach ($publicMethods as $method) {
  329. $parameters = $method->getParameters();
  330. if (\count($parameters) < 2) {
  331. $this->addToAssertionCount(1); // not enough parameters to test, all good!
  332. continue;
  333. }
  334. $expected = [
  335. 'expected' => false,
  336. 'input' => false,
  337. ];
  338. for ($i = \count($parameters) - 1; $i >= 0; --$i) {
  339. $name = $parameters[$i]->getName();
  340. if (isset($expected[$name])) {
  341. $expected[$name] = $i;
  342. }
  343. }
  344. $expected = array_filter($expected);
  345. if (\count($expected) < 2) {
  346. $this->addToAssertionCount(1); // not enough parameters to test, all good!
  347. continue;
  348. }
  349. static::assertLessThan(
  350. $expected['input'],
  351. $expected['expected'],
  352. sprintf('Public method "%s::%s" has parameter \'input\' before \'expected\'.', $reflectionClass->getName(), $method->getName())
  353. );
  354. }
  355. }
  356. public function provideSrcClassCases()
  357. {
  358. return array_map(
  359. static function ($item) {
  360. return [$item];
  361. },
  362. $this->getSrcClasses()
  363. );
  364. }
  365. public function provideSrcClassesNotAbuseInterfacesCases()
  366. {
  367. return array_map(
  368. static function ($item) {
  369. return [$item];
  370. },
  371. array_filter($this->getSrcClasses(), static function ($className) {
  372. $rc = new \ReflectionClass($className);
  373. $doc = false !== $rc->getDocComment()
  374. ? new DocBlock($rc->getDocComment())
  375. : null;
  376. if (
  377. $rc->isInterface()
  378. || ($doc && \count($doc->getAnnotationsOfType('internal')))
  379. || 0 === \count($rc->getInterfaces())
  380. || \in_array($className, [
  381. \PhpCsFixer\Finder::class,
  382. \PhpCsFixer\Test\AbstractFixerTestCase::class,
  383. \PhpCsFixer\Test\AbstractIntegrationTestCase::class,
  384. \PhpCsFixer\Tests\Test\AbstractFixerTestCase::class,
  385. \PhpCsFixer\Tests\Test\AbstractIntegrationTestCase::class,
  386. \PhpCsFixer\Tokenizer\Tokens::class,
  387. ], true)
  388. ) {
  389. return false;
  390. }
  391. return true;
  392. })
  393. );
  394. }
  395. public function provideSrcConcreteClassCases()
  396. {
  397. return array_map(
  398. static function ($item) { return [$item]; },
  399. array_filter(
  400. $this->getSrcClasses(),
  401. static function ($className) {
  402. $rc = new \ReflectionClass($className);
  403. return !$rc->isAbstract() && !$rc->isInterface();
  404. }
  405. )
  406. );
  407. }
  408. public function provideTestClassCases()
  409. {
  410. return array_map(
  411. static function ($item) {
  412. return [$item];
  413. },
  414. $this->getTestClasses()
  415. );
  416. }
  417. public function provideClassesWherePregFunctionsAreForbiddenCases()
  418. {
  419. return array_map(
  420. static function ($item) {
  421. return [$item];
  422. },
  423. array_filter(
  424. $this->getSrcClasses(),
  425. static function ($className) {
  426. return Preg::class !== $className;
  427. }
  428. )
  429. );
  430. }
  431. private function getUsedDataProviderMethodNames($testClassName)
  432. {
  433. $dataProviderMethodNames = [];
  434. $tokens = Tokens::fromCode(file_get_contents(
  435. str_replace('\\', \DIRECTORY_SEPARATOR, preg_replace('#^PhpCsFixer\\\Tests#', 'tests', $testClassName)).'.php'
  436. ));
  437. foreach ($tokens as $token) {
  438. if ($token->isGivenKind(T_DOC_COMMENT)) {
  439. $docBlock = new DocBlock($token->getContent());
  440. $dataProviderAnnotations = $docBlock->getAnnotationsOfType('dataProvider');
  441. foreach ($dataProviderAnnotations as $dataProviderAnnotation) {
  442. if (1 === preg_match('/@dataProvider\s+(?P<methodName>\w+)/', $dataProviderAnnotation->getContent(), $matches)) {
  443. $dataProviderMethodNames[] = $matches['methodName'];
  444. }
  445. }
  446. }
  447. }
  448. return array_unique($dataProviderMethodNames);
  449. }
  450. private function getSrcClasses()
  451. {
  452. static $classes;
  453. if (null !== $classes) {
  454. return $classes;
  455. }
  456. $finder = Finder::create()
  457. ->files()
  458. ->name('*.php')
  459. ->in(__DIR__.'/../../src')
  460. ->exclude([
  461. 'Resources',
  462. ])
  463. ;
  464. $classes = array_map(
  465. static function (SplFileInfo $file) {
  466. return sprintf(
  467. '%s\\%s%s%s',
  468. 'PhpCsFixer',
  469. strtr($file->getRelativePath(), \DIRECTORY_SEPARATOR, '\\'),
  470. $file->getRelativePath() ? '\\' : '',
  471. $file->getBasename('.'.$file->getExtension())
  472. );
  473. },
  474. iterator_to_array($finder, false)
  475. );
  476. sort($classes);
  477. return $classes;
  478. }
  479. private function getTestClasses()
  480. {
  481. static $classes;
  482. if (null !== $classes) {
  483. return $classes;
  484. }
  485. $finder = Finder::create()
  486. ->files()
  487. ->name('*.php')
  488. ->in(__DIR__.'/..')
  489. ->exclude([
  490. 'Fixtures',
  491. ])
  492. ;
  493. $classes = array_map(
  494. static function (SplFileInfo $file) {
  495. return sprintf(
  496. 'PhpCsFixer\\Tests\\%s%s%s',
  497. strtr($file->getRelativePath(), \DIRECTORY_SEPARATOR, '\\'),
  498. $file->getRelativePath() ? '\\' : '',
  499. $file->getBasename('.'.$file->getExtension())
  500. );
  501. },
  502. iterator_to_array($finder, false)
  503. );
  504. $classes = array_filter($classes, static function ($class) {
  505. return is_subclass_of($class, TestCase::class);
  506. });
  507. sort($classes);
  508. return $classes;
  509. }
  510. /**
  511. * @return string[]
  512. */
  513. private function getPublicMethodNames(\ReflectionClass $rc)
  514. {
  515. return array_map(
  516. static function (\ReflectionMethod $rm) {
  517. return $rm->getName();
  518. },
  519. $rc->getMethods(\ReflectionMethod::IS_PUBLIC)
  520. );
  521. }
  522. }