ProjectCodeTest.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\AutoReview;
  13. use PhpCsFixer\AbstractFixer;
  14. use PhpCsFixer\AbstractPhpdocToTypeDeclarationFixer;
  15. use PhpCsFixer\AbstractPhpdocTypesFixer;
  16. use PhpCsFixer\AbstractProxyFixer;
  17. use PhpCsFixer\Console\Command\FixCommand;
  18. use PhpCsFixer\DocBlock\Annotation;
  19. use PhpCsFixer\DocBlock\DocBlock;
  20. use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
  21. use PhpCsFixer\Fixer\ConfigurableFixerTrait;
  22. use PhpCsFixer\Fixer\PhpUnit\PhpUnitNamespacedFixer;
  23. use PhpCsFixer\FixerConfiguration\AliasedFixerOptionBuilder;
  24. use PhpCsFixer\FixerFactory;
  25. use PhpCsFixer\Preg;
  26. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  27. use PhpCsFixer\Tests\Test\AbstractIntegrationTestCase;
  28. use PhpCsFixer\Tests\TestCase;
  29. use PhpCsFixer\Tokenizer\Token;
  30. use PhpCsFixer\Tokenizer\Tokens;
  31. use PhpCsFixer\Tokenizer\TokensAnalyzer;
  32. use Symfony\Component\Finder\Finder;
  33. use Symfony\Component\Finder\SplFileInfo;
  34. /**
  35. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  36. *
  37. * @internal
  38. *
  39. * @coversNothing
  40. *
  41. * @group auto-review
  42. * @group covers-nothing
  43. */
  44. final class ProjectCodeTest extends TestCase
  45. {
  46. /**
  47. * @var null|array<string, array{class-string<TestCase>}>
  48. */
  49. private static ?array $testClassCases = null;
  50. /**
  51. * @var null|array<string, array{class-string}>
  52. */
  53. private static ?array $srcClassCases = null;
  54. /**
  55. * @var null|array<string, array{class-string, string}>
  56. */
  57. private static ?array $dataProviderMethodCases = null;
  58. /**
  59. * @var array<class-string, Tokens>
  60. */
  61. private static array $tokensCache = [];
  62. public static function tearDownAfterClass(): void
  63. {
  64. self::$srcClassCases = null;
  65. self::$testClassCases = null;
  66. self::$tokensCache = [];
  67. }
  68. /**
  69. * @dataProvider provideThatSrcClassHaveTestClassCases
  70. */
  71. public function testThatSrcClassHaveTestClass(string $className): void
  72. {
  73. $testClassName = 'PhpCsFixer\Tests'.substr($className, 10).'Test';
  74. self::assertTrue(class_exists($testClassName), \sprintf('Expected test class "%s" for "%s" not found.', $testClassName, $className));
  75. }
  76. /**
  77. * This test requires 8.2+, so it can properly detect readonly parent class.
  78. *
  79. * @dataProvider provideSrcClassCases
  80. *
  81. * @param class-string $className
  82. *
  83. * @requires PHP 8.2
  84. */
  85. public function testThatSrcClassesAreReadonlyWhenPossible(string $className): void
  86. {
  87. $rc = new \ReflectionClass($className);
  88. $rcProperties = $rc->getProperties();
  89. if (0 === \count($rcProperties)) {
  90. $this->addToAssertionCount(1);
  91. return; // public properties present, no need for class to be readonly
  92. }
  93. $parentClass = $rc->getParentClass();
  94. if (\PHP_VERSION_ID >= 8_02_00 && false !== $parentClass && !$parentClass->isReadOnly()) {
  95. $this->addToAssertionCount(1);
  96. return; // Parent class is _not_ readonly, child class cannot be readonly in such case
  97. }
  98. $rc = new \ReflectionClass($className);
  99. $docComment = $rc->getDocComment();
  100. $doc = new DocBlock(false !== $docComment ? $docComment : '/** */');
  101. $readonly = \count($doc->getAnnotationsOfType('readonly')) > 0;
  102. $exceptions = [
  103. AliasedFixerOptionBuilder::class,
  104. ];
  105. // we allow exceptions to _not_ follow the rule,
  106. // but when they are ready to start following it - we shall remove them from exceptions list
  107. if (\in_array($className, $exceptions, true)) {
  108. self::assertFalse($readonly);
  109. return;
  110. }
  111. if ($readonly) {
  112. $this->addToAssertionCount(1);
  113. return; // already readonly
  114. }
  115. $tokens = $this->createTokensForClass($className);
  116. $constructorSequence = $tokens->findSequence([
  117. [T_FUNCTION],
  118. [T_STRING, '__construct'],
  119. '(',
  120. ]);
  121. if (null !== $constructorSequence) {
  122. $tokens = clone $tokens;
  123. $openIndex = $tokens->getNextTokenOfKind(array_key_last($constructorSequence), ['{']);
  124. $closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $openIndex);
  125. $tokens->overrideRange($openIndex + 1, $closeIndex - 1, []);
  126. }
  127. $tokensContent = $tokens->generateCode();
  128. $propertyNames = array_map(static fn (\ReflectionProperty $item) => $item->getName(), $rcProperties);
  129. $overrideFound = Preg::match(
  130. '/(?:self::\$|static::\$|\$this->)(?:'.implode('|', $propertyNames).')(?:\[[^=]*\])?\s*(?:=|(?:\?\?=))/',
  131. $tokensContent
  132. );
  133. if ($overrideFound) {
  134. $this->addToAssertionCount(1);
  135. return; // properties are mutable during lifecycle of instance, class is not readonly
  136. }
  137. self::fail(
  138. \sprintf('The class "%s" should have readonly annotation.', $className)
  139. );
  140. }
  141. /**
  142. * @dataProvider provideThatSrcClassesNotAbuseInterfacesCases
  143. *
  144. * @param class-string $className
  145. */
  146. public function testThatSrcClassesNotAbuseInterfaces(string $className): void
  147. {
  148. $rc = new \ReflectionClass($className);
  149. $allowedMethods = array_map(
  150. fn (\ReflectionClass $interface): array => $this->getPublicMethodNames($interface),
  151. $rc->getInterfaces()
  152. );
  153. if (\count($allowedMethods) > 0) {
  154. $allowedMethods = array_unique(array_merge(...array_values($allowedMethods)));
  155. }
  156. $allowedMethods[] = '__construct';
  157. $allowedMethods[] = '__destruct';
  158. $allowedMethods[] = '__wakeup';
  159. $exceptionMethods = [
  160. 'configure', // due to AbstractFixer::configure
  161. 'getConfigurationDefinition', // due to AbstractFixer::getConfigurationDefinition
  162. 'getDefaultConfiguration', // due to AbstractFixer::getDefaultConfiguration
  163. 'setWhitespacesConfig', // due to AbstractFixer::setWhitespacesConfig
  164. 'createConfigurationDefinition', // due to AbstractProxyFixer calling `createConfigurationDefinition` of proxied rule
  165. ];
  166. $definedMethods = $this->getPublicMethodNames($rc);
  167. $extraMethods = array_diff(
  168. $definedMethods,
  169. $allowedMethods,
  170. $exceptionMethods
  171. );
  172. sort($extraMethods);
  173. self::assertEmpty(
  174. $extraMethods,
  175. \sprintf(
  176. "Class '%s' should not have public methods that are not part of implemented interfaces.\nViolations:\n%s",
  177. $className,
  178. implode("\n", array_map(static fn (string $item): string => " * {$item}", $extraMethods))
  179. )
  180. );
  181. }
  182. /**
  183. * @dataProvider provideSrcClassCases
  184. *
  185. * @param class-string $className
  186. */
  187. public function testThatSrcClassesNotExposeProperties(string $className): void
  188. {
  189. $rc = new \ReflectionClass($className);
  190. self::assertEmpty(
  191. $rc->getProperties(\ReflectionProperty::IS_PUBLIC),
  192. \sprintf('Class \'%s\' should not have public properties.', $className)
  193. );
  194. if ($rc->isFinal()) {
  195. return;
  196. }
  197. $allowedProps = [];
  198. $definedProps = $rc->getProperties(\ReflectionProperty::IS_PROTECTED);
  199. if (false !== $rc->getParentClass()) {
  200. $allowedProps = $rc->getParentClass()->getProperties(\ReflectionProperty::IS_PROTECTED);
  201. }
  202. $allowedProps = array_map(static fn (\ReflectionProperty $item): string => $item->getName(), $allowedProps);
  203. $definedProps = array_map(static fn (\ReflectionProperty $item): string => $item->getName(), $definedProps);
  204. $exceptionPropsPerClass = [
  205. AbstractFixer::class => ['configuration', 'configurationDefinition', 'whitespacesConfig'],
  206. AbstractPhpdocToTypeDeclarationFixer::class => ['configuration'],
  207. AbstractPhpdocTypesFixer::class => ['tags'],
  208. AbstractProxyFixer::class => ['proxyFixers'],
  209. ConfigurableFixerTrait::class => ['configuration'],
  210. FixCommand::class => ['defaultDescription', 'defaultName'], // TODO: PHP 8.0+, remove properties and test when PHP 8+ is required
  211. ];
  212. $extraProps = array_diff(
  213. $definedProps,
  214. $allowedProps,
  215. $exceptionPropsPerClass[$className] ?? []
  216. );
  217. sort($extraProps);
  218. self::assertEmpty(
  219. $extraProps,
  220. \sprintf(
  221. "Class '%s' should not have protected properties.\nViolations:\n%s",
  222. $className,
  223. implode("\n", array_map(static fn (string $item): string => " * {$item}", $extraProps))
  224. )
  225. );
  226. }
  227. /**
  228. * @dataProvider provideTestClassCases
  229. */
  230. public function testThatTestClassExtendsPhpCsFixerTestCaseClass(string $className): void
  231. {
  232. self::assertTrue(is_subclass_of($className, TestCase::class), \sprintf('Expected test class "%s" to be a subclass of "%s".', $className, TestCase::class));
  233. }
  234. /**
  235. * @dataProvider provideTestClassCases
  236. *
  237. * @param class-string<TestCase> $testClassName
  238. */
  239. public function testThatTestClassesAreTraitOrAbstractOrFinal(string $testClassName): void
  240. {
  241. $rc = new \ReflectionClass($testClassName);
  242. self::assertTrue(
  243. $rc->isTrait() || $rc->isAbstract() || $rc->isFinal(),
  244. \sprintf('Test class %s should be trait, abstract or final.', $testClassName)
  245. );
  246. }
  247. /**
  248. * @dataProvider provideTestClassCases
  249. *
  250. * @param class-string<TestCase> $testClassName
  251. */
  252. public function testThatTestClassesAreInternal(string $testClassName): void
  253. {
  254. $rc = new \ReflectionClass($testClassName);
  255. $doc = new DocBlock($rc->getDocComment());
  256. self::assertNotEmpty(
  257. $doc->getAnnotationsOfType('internal'),
  258. \sprintf('Test class %s should have internal annotation.', $testClassName)
  259. );
  260. }
  261. /**
  262. * @dataProvider provideTestClassCases
  263. *
  264. * @param class-string<TestCase> $testClassName
  265. */
  266. public function testThatTestClassesPublicMethodsAreCorrectlyNamed(string $testClassName): void
  267. {
  268. $reflectionClass = new \ReflectionClass($testClassName);
  269. $publicMethods = array_filter(
  270. $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC),
  271. static fn (\ReflectionMethod $reflectionMethod): bool => $reflectionMethod->getDeclaringClass()->getName() === $reflectionClass->getName()
  272. );
  273. if ([] === $publicMethods) {
  274. $this->expectNotToPerformAssertions(); // no methods to test, all good!
  275. return;
  276. }
  277. foreach ($publicMethods as $method) {
  278. self::assertMatchesRegularExpression(
  279. '/^(test|expect|provide|setUpBeforeClass$|tearDownAfterClass$|__construct$)/',
  280. $method->getName(),
  281. \sprintf('Public method "%s::%s" is not properly named.', $reflectionClass->getName(), $method->getName())
  282. );
  283. }
  284. }
  285. /**
  286. * @dataProvider provideDataProviderMethodCases
  287. *
  288. * @param class-string<TestCase> $testClassName
  289. */
  290. public function testThatTestDataProvidersAreUsed(string $testClassName, string $dataProviderName): void
  291. {
  292. $usedDataProviderMethodNames = [];
  293. foreach ($this->getUsedDataProviderMethodNames($testClassName) as $providerName) {
  294. $usedDataProviderMethodNames[] = $providerName;
  295. }
  296. self::assertContains(
  297. $dataProviderName,
  298. $usedDataProviderMethodNames,
  299. \sprintf('Data provider "%s::%s" is not used.', $testClassName, $dataProviderName),
  300. );
  301. }
  302. /**
  303. * @dataProvider provideDataProviderMethodCases
  304. */
  305. public function testThatTestDataProvidersAreCorrectlyNamed(string $testClassName, string $dataProviderName): void
  306. {
  307. self::assertMatchesRegularExpression('/^provide[A-Z]\S+Cases$/', $dataProviderName, \sprintf(
  308. 'Data provider "%s::%s" is not correctly named.',
  309. $testClassName,
  310. $dataProviderName,
  311. ));
  312. }
  313. /**
  314. * @return iterable<string, array{string, string}>
  315. */
  316. public static function provideDataProviderMethodCases(): iterable
  317. {
  318. if (null === self::$dataProviderMethodCases) {
  319. self::$dataProviderMethodCases = [];
  320. foreach (self::provideTestClassCases() as $testClassName) {
  321. $testClassName = reset($testClassName);
  322. $reflectionClass = new \ReflectionClass($testClassName);
  323. $dataProviderNames = array_filter(
  324. $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC),
  325. static fn (\ReflectionMethod $reflectionMethod): bool => $reflectionMethod->getDeclaringClass()->getName() === $reflectionClass->getName() && str_starts_with($reflectionMethod->getName(), 'provide')
  326. );
  327. foreach ($dataProviderNames as $dataProviderName) {
  328. self::$dataProviderMethodCases[$testClassName.'::'.$dataProviderName->getName()] = [$testClassName, $dataProviderName->getName()];
  329. }
  330. }
  331. }
  332. yield from self::$dataProviderMethodCases;
  333. }
  334. /**
  335. * @dataProvider provideTestClassCases
  336. *
  337. * @param class-string<TestCase> $testClassName
  338. */
  339. public function testThatTestClassCoversAreCorrect(string $testClassName): void
  340. {
  341. $reflectionClass = new \ReflectionClass($testClassName);
  342. if ($reflectionClass->isAbstract() || $reflectionClass->isInterface()) {
  343. $this->expectNotToPerformAssertions();
  344. return;
  345. }
  346. $doc = $reflectionClass->getDocComment();
  347. self::assertNotFalse($doc);
  348. if (Preg::match('/@coversNothing/', $doc)) {
  349. return;
  350. }
  351. $covers = Preg::matchAll('/@covers (\S*)/', $doc, $matches);
  352. self::assertGreaterThanOrEqual(1, $covers, \sprintf('Missing @covers in PHPDoc of test class "%s".', $testClassName));
  353. array_shift($matches);
  354. /** @var class-string $class */
  355. $class = '\\'.str_replace('PhpCsFixer\Tests\\', 'PhpCsFixer\\', substr($testClassName, 0, -4));
  356. $parentClass = (new \ReflectionClass($class))->getParentClass();
  357. $parentClassName = false === $parentClass ? null : '\\'.$parentClass->getName();
  358. foreach ($matches as $match) {
  359. $classMatch = array_shift($match);
  360. self::assertTrue(
  361. $classMatch === $class || $parentClassName === $classMatch,
  362. \sprintf('Unexpected @covers "%s" for "%s".', $classMatch, $testClassName)
  363. );
  364. }
  365. }
  366. /**
  367. * @dataProvider provideSrcClassCases
  368. * @dataProvider provideTestClassCases
  369. *
  370. * @param class-string $className
  371. */
  372. public function testThereIsNoUsageOfExtract(string $className): void
  373. {
  374. $calledFunctions = $this->extractFunctionNamesCalledInClass($className);
  375. $message = \sprintf('Class %s must not use "extract()", explicitly extract only the keys that are needed - you never know what\'s else inside.', $className);
  376. self::assertNotContains('extract', $calledFunctions, $message);
  377. }
  378. /**
  379. * @dataProvider provideThereIsNoPregFunctionUsedDirectlyCases
  380. *
  381. * @param class-string $className
  382. */
  383. public function testThereIsNoPregFunctionUsedDirectly(string $className): void
  384. {
  385. $calledFunctions = $this->extractFunctionNamesCalledInClass($className);
  386. $message = \sprintf('Class %s must not use preg_*, it shall use Preg::* instead.', $className);
  387. self::assertNotContains('preg_filter', $calledFunctions, $message);
  388. self::assertNotContains('preg_grep', $calledFunctions, $message);
  389. self::assertNotContains('preg_match', $calledFunctions, $message);
  390. self::assertNotContains('preg_match_all', $calledFunctions, $message);
  391. self::assertNotContains('preg_replace', $calledFunctions, $message);
  392. self::assertNotContains('preg_replace_callback', $calledFunctions, $message);
  393. self::assertNotContains('preg_split', $calledFunctions, $message);
  394. }
  395. /**
  396. * @dataProvider provideTestClassCases
  397. *
  398. * @param class-string $className
  399. */
  400. public function testThereIsNoUsageOfSetAccessible(string $className): void
  401. {
  402. $calledFunctions = $this->extractFunctionNamesCalledInClass($className);
  403. $message = \sprintf('Class %s must not use "setAccessible()", use "Closure::bind()" instead.', $className);
  404. self::assertNotContains('setAccessible', $calledFunctions, $message);
  405. }
  406. /**
  407. * @dataProvider provideTestClassCases
  408. *
  409. * @param class-string<TestCase> $className
  410. */
  411. public function testNoPHPUnitMockUsed(string $className): void
  412. {
  413. $calledFunctions = $this->extractFunctionNamesCalledInClass($className);
  414. $message = \sprintf('Class %s must not use PHPUnit\'s mock, it shall use anonymous class instead.', $className);
  415. self::assertNotContains('getMockBuilder', $calledFunctions, $message);
  416. self::assertNotContains('createMock', $calledFunctions, $message);
  417. self::assertNotContains('createMockForIntersectionOfInterfaces', $calledFunctions, $message);
  418. self::assertNotContains('createPartialMock', $calledFunctions, $message);
  419. self::assertNotContains('createTestProxy', $calledFunctions, $message);
  420. self::assertNotContains('getMockForAbstractClass', $calledFunctions, $message);
  421. self::assertNotContains('getMockFromWsdl', $calledFunctions, $message);
  422. self::assertNotContains('getMockForTrait', $calledFunctions, $message);
  423. self::assertNotContains('getMockClass', $calledFunctions, $message);
  424. self::assertNotContains('createConfiguredMock', $calledFunctions, $message);
  425. self::assertNotContains('getObjectForTrait', $calledFunctions, $message);
  426. }
  427. /**
  428. * @dataProvider provideTestClassCases
  429. *
  430. * @param class-string<TestCase> $testClassName
  431. */
  432. public function testExpectedInputOrder(string $testClassName): void
  433. {
  434. $reflectionClass = new \ReflectionClass($testClassName);
  435. $publicMethods = array_filter(
  436. $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC),
  437. static fn (\ReflectionMethod $reflectionMethod): bool => $reflectionMethod->getDeclaringClass()->getName() === $reflectionClass->getName()
  438. );
  439. if ([] === $publicMethods) {
  440. $this->expectNotToPerformAssertions(); // no methods to test, all good!
  441. return;
  442. }
  443. /** @var \ReflectionMethod $method */
  444. foreach ($publicMethods as $method) {
  445. $parameters = $method->getParameters();
  446. if (\count($parameters) < 2) {
  447. $this->addToAssertionCount(1); // not enough parameters to test, all good!
  448. continue;
  449. }
  450. $expected = [
  451. 'expected' => false,
  452. 'input' => false,
  453. ];
  454. for ($i = \count($parameters) - 1; $i >= 0; --$i) {
  455. $name = $parameters[$i]->getName();
  456. if (isset($expected[$name])) {
  457. $expected[$name] = $i;
  458. }
  459. }
  460. $expectedFound = array_filter($expected, static fn ($item): bool => false !== $item);
  461. if (\count($expectedFound) < 2) {
  462. $this->addToAssertionCount(1); // not enough parameters to test, all good!
  463. continue;
  464. }
  465. self::assertLessThan(
  466. $expected['input'],
  467. $expected['expected'],
  468. \sprintf('Public method "%s::%s" has parameter \'input\' before \'expected\'.', $reflectionClass->getName(), $method->getName())
  469. );
  470. }
  471. }
  472. /**
  473. * @dataProvider provideDataProviderMethodCases
  474. *
  475. * @param class-string<TestCase> $testClassName
  476. * @param non-empty-string $dataProviderName
  477. */
  478. public function testDataProvidersAreNonPhpVersionConditional(string $testClassName, string $dataProviderName): void
  479. {
  480. $tokens = $this->createTokensForClass($testClassName);
  481. $tokensAnalyzer = new TokensAnalyzer($tokens);
  482. $dataProviderElements = array_filter($tokensAnalyzer->getClassyElements(), static function (array $v, int $k) use ($tokens, $dataProviderName) {
  483. $nextToken = $tokens[$tokens->getNextMeaningfulToken($k)];
  484. // element is data provider method
  485. return 'method' === $v['type'] && $nextToken->equals([T_STRING, $dataProviderName]);
  486. }, ARRAY_FILTER_USE_BOTH);
  487. if (1 !== \count($dataProviderElements)) {
  488. throw new \UnexpectedValueException(\sprintf('Data provider "%s::%s" should be found exactly once, got %d times.', $testClassName, $dataProviderName, \count($dataProviderElements)));
  489. }
  490. $methodIndex = array_key_first($dataProviderElements);
  491. $startIndex = $tokens->getNextTokenOfKind($methodIndex, ['{']);
  492. $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startIndex);
  493. $versionTokens = array_filter($tokens->findGivenKind(T_STRING, $startIndex, $endIndex), static fn (Token $v): bool => $v->equalsAny([
  494. [T_STRING, 'PHP_VERSION_ID'],
  495. [T_STRING, 'PHP_MAJOR_VERSION'],
  496. [T_STRING, 'PHP_MINOR_VERSION'],
  497. [T_STRING, 'PHP_RELEASE_VERSION'],
  498. [T_STRING, 'phpversion'],
  499. ], false));
  500. self::assertCount(
  501. 0,
  502. $versionTokens,
  503. \sprintf(
  504. 'Data provider "%s::%s" should not check PHP version and provide different cases depends on it. It leads to situation when DataProvider provides "sometimes 10, sometimes 11" test cases, depends on PHP version. That makes John Doe to see "you run 10/10" and thinking all tests are executed, instead of actually seeing "you run 10/11 and 1 skipped".',
  505. $testClassName,
  506. $dataProviderName,
  507. ),
  508. );
  509. }
  510. /**
  511. * @dataProvider provideDataProviderMethodCases
  512. */
  513. public function testDataProvidersDeclaredReturnType(string $testClassName, string $dataProviderName): void
  514. {
  515. $dataProvider = new \ReflectionMethod($testClassName, $dataProviderName);
  516. self::assertSame('iterable', $dataProvider->hasReturnType() && $dataProvider->getReturnType() instanceof \ReflectionNamedType ? $dataProvider->getReturnType()->getName() : null, \sprintf('Data provider "%s::%s" must provide `iterable` as return in method prototype.', $testClassName, $dataProviderName));
  517. $doc = new DocBlock(false !== $dataProvider->getDocComment() ? $dataProvider->getDocComment() : '/** */');
  518. $returnDocs = $doc->getAnnotationsOfType('return');
  519. if (\count($returnDocs) > 1) {
  520. throw new \UnexpectedValueException(\sprintf('Multiple "%s::%s@return" annotations.', $testClassName, $dataProviderName));
  521. }
  522. if (1 !== \count($returnDocs)) {
  523. $this->addToAssertionCount(1); // no @return annotation, all good!
  524. return;
  525. }
  526. $returnDoc = $returnDocs[0];
  527. $types = $returnDoc->getTypes();
  528. self::assertCount(1, $types, \sprintf('Data provider "%s::%s@return" must provide single type.', $testClassName, $dataProviderName));
  529. self::assertMatchesRegularExpression('/^iterable\</', $types[0], \sprintf('Data provider "%s::%s@return" must return iterable.', $testClassName, $dataProviderName));
  530. self::assertMatchesRegularExpression('/^iterable\<(?:(?:int\|)?string, )?array\{/', $types[0], \sprintf('Data provider "%s::%s@return" must return iterable of tuples (eg `iterable<string, array{string, string}>`).', $testClassName, $dataProviderName));
  531. }
  532. /**
  533. * @dataProvider provideSrcClassCases
  534. * @dataProvider provideTestClassCases
  535. *
  536. * @param class-string $className
  537. */
  538. public function testAllCodeContainSingleClassy(string $className): void
  539. {
  540. $headerTypes = [
  541. T_ABSTRACT,
  542. T_AS,
  543. T_COMMENT,
  544. T_DECLARE,
  545. T_DOC_COMMENT,
  546. T_FINAL,
  547. T_LNUMBER,
  548. T_NAMESPACE,
  549. T_NS_SEPARATOR,
  550. T_OPEN_TAG,
  551. T_STRING,
  552. T_USE,
  553. T_WHITESPACE,
  554. ];
  555. if (\defined('T_READONLY')) { // @TODO: drop condition when PHP 8.1+ is required
  556. $headerTypes[] = T_READONLY;
  557. }
  558. $tokens = $this->createTokensForClass($className);
  559. $classyIndex = null;
  560. self::assertTrue($tokens->isAnyTokenKindsFound(Token::getClassyTokenKinds()), \sprintf('File for "%s" should contains a classy.', $className));
  561. $count = \count($tokens);
  562. for ($index = 1; $index < $count; ++$index) {
  563. if ($tokens[$index]->isClassy()) {
  564. $classyIndex = $index;
  565. break;
  566. }
  567. if (\defined('T_ATTRIBUTE') && $tokens[$index]->isGivenKind(T_ATTRIBUTE)) {
  568. $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ATTRIBUTE, $index);
  569. continue;
  570. }
  571. if (!$tokens[$index]->isGivenKind($headerTypes) && !$tokens[$index]->equalsAny([';', '=', '(', ')'])) {
  572. self::fail(\sprintf('File for "%s" should only contains single classy, found "%s" @ %d.', $className, $tokens[$index]->toJson(), $index));
  573. }
  574. }
  575. self::assertNotNull($classyIndex, \sprintf('File for "%s" does not contain a classy.', $className));
  576. $nextTokenOfKind = $tokens->getNextTokenOfKind($classyIndex, ['{']);
  577. if (!\is_int($nextTokenOfKind)) {
  578. throw new \UnexpectedValueException('Classy without {} - braces.');
  579. }
  580. $classyEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $nextTokenOfKind);
  581. self::assertNull($tokens->getNextMeaningfulToken($classyEndIndex), \sprintf('File for "%s" should only contains a single classy.', $className));
  582. }
  583. /**
  584. * @dataProvider provideSrcClassCases
  585. *
  586. * @param class-string $className
  587. */
  588. public function testInheritdocIsNotAbused(string $className): void
  589. {
  590. $rc = new \ReflectionClass($className);
  591. $allowedMethods = array_map(
  592. fn (\ReflectionClass $interface): array => $this->getPublicMethodNames($interface),
  593. $rc->getInterfaces()
  594. );
  595. if (\count($allowedMethods) > 0) {
  596. $allowedMethods = array_merge(...array_values($allowedMethods));
  597. }
  598. $parentClass = $rc;
  599. while (false !== $parentClass = $parentClass->getParentClass()) {
  600. foreach ($parentClass->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $method) {
  601. $allowedMethods[] = $method->getName();
  602. }
  603. }
  604. $allowedMethods = array_unique($allowedMethods);
  605. $methodsWithInheritdoc = array_filter(
  606. $rc->getMethods(),
  607. static fn (\ReflectionMethod $rm): bool => false !== $rm->getDocComment() && stripos($rm->getDocComment(), '@inheritdoc')
  608. );
  609. $methodsWithInheritdoc = array_map(
  610. static fn (\ReflectionMethod $rm): string => $rm->getName(),
  611. $methodsWithInheritdoc
  612. );
  613. $extraMethods = array_diff($methodsWithInheritdoc, $allowedMethods);
  614. self::assertEmpty(
  615. $extraMethods,
  616. \sprintf(
  617. "Class '%s' should not have methods with '@inheritdoc' in PHPDoc that are not inheriting PHPDoc.\nViolations:\n%s",
  618. $className,
  619. implode("\n", array_map(static fn ($item): string => " * {$item}", $extraMethods))
  620. )
  621. );
  622. }
  623. /**
  624. * @return iterable<string, array{class-string}>
  625. */
  626. public static function provideSrcClassCases(): iterable
  627. {
  628. if (null === self::$srcClassCases) {
  629. $cases = self::getSrcClasses();
  630. self::$srcClassCases = array_combine(
  631. $cases,
  632. array_map(static fn (string $case): array => [$case], $cases),
  633. );
  634. }
  635. yield from self::$srcClassCases;
  636. }
  637. /**
  638. * @return iterable<array{string}>
  639. */
  640. public static function provideThatSrcClassesNotAbuseInterfacesCases(): iterable
  641. {
  642. return array_map(
  643. static fn (string $item): array => [$item],
  644. array_filter(self::getSrcClasses(), static function (string $className): bool {
  645. $rc = new \ReflectionClass($className);
  646. $doc = false !== $rc->getDocComment()
  647. ? new DocBlock($rc->getDocComment())
  648. : null;
  649. if (
  650. $rc->isInterface()
  651. || (null !== $doc && \count($doc->getAnnotationsOfType('internal')) > 0)
  652. || \in_array($className, [
  653. \PhpCsFixer\Finder::class,
  654. AbstractFixerTestCase::class,
  655. AbstractIntegrationTestCase::class,
  656. Tokens::class,
  657. ], true)
  658. ) {
  659. return false;
  660. }
  661. $interfaces = $rc->getInterfaces();
  662. $interfacesCount = \count($interfaces);
  663. if (0 === $interfacesCount) {
  664. return false;
  665. }
  666. if (1 === $interfacesCount) {
  667. $interface = reset($interfaces);
  668. if (\Stringable::class === $interface->getName()) {
  669. return false;
  670. }
  671. }
  672. return true;
  673. })
  674. );
  675. }
  676. /**
  677. * @return iterable<array{string}>
  678. */
  679. public static function provideThatSrcClassHaveTestClassCases(): iterable
  680. {
  681. return array_map(
  682. static fn (string $item): array => [$item],
  683. array_filter(
  684. self::getSrcClasses(),
  685. static function (string $className): bool {
  686. $rc = new \ReflectionClass($className);
  687. return !$rc->isTrait() && !$rc->isAbstract() && !$rc->isInterface() && \count($rc->getMethods(\ReflectionMethod::IS_PUBLIC)) > 0;
  688. }
  689. )
  690. );
  691. }
  692. public function testAllTestsForShortOpenTagAreHandled(): void
  693. {
  694. $testClassesWithShortOpenTag = array_filter(
  695. self::getTestClasses(),
  696. fn (string $className): bool => str_contains($this->getFileContentForClass($className), 'short_open_tag') && self::class !== $className
  697. );
  698. $testFilesWithShortOpenTag = array_map(
  699. fn (string $className): string => './'.$this->getFilePathForClass($className),
  700. $testClassesWithShortOpenTag
  701. );
  702. $phpunitXmlContent = file_get_contents(__DIR__.'/../../phpunit.xml.dist');
  703. $phpunitFiles = (array) simplexml_load_string($phpunitXmlContent)->xpath('testsuites/testsuite[@name="short-open-tag"]')[0]->file;
  704. sort($testFilesWithShortOpenTag);
  705. sort($phpunitFiles);
  706. self::assertSame($testFilesWithShortOpenTag, $phpunitFiles);
  707. }
  708. /**
  709. * @dataProvider provideTestClassCases
  710. *
  711. * @param class-string $className
  712. */
  713. public function testThatTestMethodsAreNotDuplicated(string $className): void
  714. {
  715. $class = new \ReflectionClass($className);
  716. $alreadyFoundMethods = [];
  717. $duplicates = [];
  718. foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
  719. if (!str_starts_with($method->getName(), 'test')) {
  720. continue;
  721. }
  722. $startLine = (int) $method->getStartLine();
  723. $length = (int) $method->getEndLine() - $startLine;
  724. if (3 === $length) { // open and closing brace are included - this checks for single line methods
  725. continue;
  726. }
  727. /** @var list<string> $source */
  728. $source = file((string) $method->getFileName());
  729. $candidateContent = implode('', \array_slice($source, $startLine, $length));
  730. if (str_contains($candidateContent, '$this->doTest(')) {
  731. continue;
  732. }
  733. $foundInDuplicates = false;
  734. foreach ($alreadyFoundMethods as $methodKey => $methodContent) {
  735. if ($candidateContent === $methodContent) {
  736. $duplicates[] = \sprintf('%s is duplicate of %s', $methodKey, $method->getName());
  737. $foundInDuplicates = true;
  738. }
  739. }
  740. if (!$foundInDuplicates) {
  741. $alreadyFoundMethods[$method->getName()] = $candidateContent;
  742. }
  743. }
  744. self::assertSame(
  745. [],
  746. $duplicates,
  747. \sprintf(
  748. "Duplicated methods found in %s:\n - %s",
  749. $className,
  750. implode("\n - ", $duplicates)
  751. )
  752. );
  753. }
  754. /**
  755. * @dataProvider provideDataProviderMethodCases
  756. *
  757. * @param class-string<TestCase> $testClassName
  758. */
  759. public function testThatDataFromDataProvidersIsNotDuplicated(string $testClassName, string $dataProviderName): void
  760. {
  761. $exceptions = [ // should only shrink
  762. 'PhpCsFixer\Tests\AutoReview\CommandTest::provideCommandHasNameConstCases',
  763. 'PhpCsFixer\Tests\AutoReview\DocumentationTest::provideFixerDocumentationFileIsUpToDateCases',
  764. 'PhpCsFixer\Tests\AutoReview\FixerFactoryTest::providePriorityIntegrationTestFilesAreListedInPriorityGraphCases',
  765. 'PhpCsFixer\Tests\Console\Command\DescribeCommandTest::provideExecuteOutputCases',
  766. 'PhpCsFixer\Tests\Console\Command\HelpCommandTest::provideGetDisplayableAllowedValuesCases',
  767. 'PhpCsFixer\Tests\Documentation\FixerDocumentGeneratorTest::provideGenerateRuleSetsDocumentationCases',
  768. 'PhpCsFixer\Tests\Fixer\Basic\EncodingFixerTest::provideFixCases',
  769. 'PhpCsFixer\Tests\UtilsTest::provideStableSortCases',
  770. ];
  771. if (\in_array($testClassName.'::'.$dataProviderName, $exceptions, true)) {
  772. $this->addToAssertionCount(1);
  773. return;
  774. }
  775. $dataProvider = new \ReflectionMethod($testClassName, $dataProviderName);
  776. $duplicates = [];
  777. $alreadyFoundCases = [];
  778. foreach ($dataProvider->invoke($dataProvider->getDeclaringClass()->newInstanceWithoutConstructor()) as $candidateKey => $candidateData) {
  779. $candidateData = serialize($candidateData);
  780. $foundInDuplicates = false;
  781. foreach ($alreadyFoundCases as $caseKey => $caseData) {
  782. if ($candidateData === $caseData) {
  783. $duplicates[] = \sprintf(
  784. 'Duplicate in %s::%s: %s and %s.'.PHP_EOL,
  785. $testClassName,
  786. $dataProviderName,
  787. \is_int($caseKey) ? '#'.$caseKey : '"'.$caseKey.'"',
  788. \is_int($candidateKey) ? '#'.$candidateKey : '"'.$candidateKey.'"',
  789. );
  790. $foundInDuplicates = true;
  791. }
  792. }
  793. if (!$foundInDuplicates) {
  794. $alreadyFoundCases[$candidateKey] = $candidateData;
  795. }
  796. }
  797. self::assertSame([], $duplicates);
  798. }
  799. /**
  800. * @return iterable<string, array{class-string<TestCase>}>
  801. */
  802. public static function provideTestClassCases(): iterable
  803. {
  804. if (null === self::$testClassCases) {
  805. $cases = self::getTestClasses();
  806. self::$testClassCases = array_combine(
  807. $cases,
  808. array_map(static fn (string $case): array => [$case], $cases),
  809. );
  810. }
  811. yield from self::$testClassCases;
  812. }
  813. /**
  814. * @return iterable<array{string}>
  815. */
  816. public static function provideThereIsNoPregFunctionUsedDirectlyCases(): iterable
  817. {
  818. return array_map(
  819. static fn (string $item): array => [$item],
  820. array_filter(
  821. self::getSrcClasses(),
  822. static fn (string $className): bool => Preg::class !== $className,
  823. ),
  824. );
  825. }
  826. /**
  827. * @dataProvider providePhpUnitFixerExtendsAbstractPhpUnitFixerCases
  828. *
  829. * @param class-string $className
  830. */
  831. public function testPhpUnitFixerExtendsAbstractPhpUnitFixer(string $className): void
  832. {
  833. $reflection = new \ReflectionClass($className);
  834. self::assertTrue($reflection->isSubclassOf(AbstractPhpUnitFixer::class));
  835. }
  836. /**
  837. * @return iterable<array{string}>
  838. */
  839. public static function providePhpUnitFixerExtendsAbstractPhpUnitFixerCases(): iterable
  840. {
  841. $factory = new FixerFactory();
  842. $factory->registerBuiltInFixers();
  843. foreach ($factory->getFixers() as $fixer) {
  844. if (!str_starts_with($fixer->getName(), 'php_unit_')) {
  845. continue;
  846. }
  847. // this one fixes usage of PHPUnit classes
  848. if ($fixer instanceof PhpUnitNamespacedFixer) {
  849. continue;
  850. }
  851. if ($fixer instanceof AbstractProxyFixer) {
  852. continue;
  853. }
  854. yield [\get_class($fixer)];
  855. }
  856. }
  857. /**
  858. * @dataProvider provideSrcClassCases
  859. * @dataProvider provideTestClassCases
  860. *
  861. * @param class-string $className
  862. */
  863. public function testConstantsAreInUpperCase(string $className): void
  864. {
  865. $rc = new \ReflectionClass($className);
  866. $reflectionClassConstants = $rc->getReflectionConstants();
  867. if (\count($reflectionClassConstants) < 1) {
  868. $this->expectNotToPerformAssertions();
  869. return;
  870. }
  871. foreach ($reflectionClassConstants as $constant) {
  872. $constantName = $constant->getName();
  873. self::assertSame(strtoupper($constantName), $constantName, $className);
  874. }
  875. }
  876. /**
  877. * @param class-string $className
  878. *
  879. * @return list<string>
  880. */
  881. private function extractFunctionNamesCalledInClass(string $className): array
  882. {
  883. $tokens = $this->createTokensForClass($className);
  884. $stringTokens = array_filter(
  885. $tokens->toArray(),
  886. static fn (Token $token): bool => $token->isGivenKind(T_STRING)
  887. );
  888. $strings = array_map(
  889. static fn (Token $token): string => $token->getContent(),
  890. $stringTokens
  891. );
  892. return array_unique($strings);
  893. }
  894. /**
  895. * @param class-string $className
  896. */
  897. private function getFilePathForClass(string $className): string
  898. {
  899. $file = $className;
  900. $file = preg_replace('#^PhpCsFixer\\\Tests\\\#', 'tests\\', $file);
  901. $file = preg_replace('#^PhpCsFixer\\\#', 'src\\', $file);
  902. return str_replace('\\', \DIRECTORY_SEPARATOR, $file).'.php';
  903. }
  904. /**
  905. * @param class-string $className
  906. */
  907. private function getFileContentForClass(string $className): string
  908. {
  909. return file_get_contents($this->getFilePathForClass($className));
  910. }
  911. /**
  912. * @param class-string $className
  913. */
  914. private function createTokensForClass(string $className): Tokens
  915. {
  916. if (!isset(self::$tokensCache[$className])) {
  917. self::$tokensCache[$className] = Tokens::fromCode(self::getFileContentForClass($className));
  918. }
  919. return self::$tokensCache[$className];
  920. }
  921. /**
  922. * @param class-string<TestCase> $testClassName
  923. *
  924. * @return iterable<string, string>
  925. */
  926. private function getUsedDataProviderMethodNames(string $testClassName): iterable
  927. {
  928. foreach ($this->getAnnotationsOfTestClass($testClassName, 'dataProvider') as $methodName => $dataProviderAnnotation) {
  929. if (1 === preg_match('/@dataProvider\s+(?P<methodName>\w+)/', $dataProviderAnnotation->getContent(), $matches)) {
  930. yield $methodName => $matches['methodName'];
  931. }
  932. }
  933. }
  934. /**
  935. * @param class-string<TestCase> $testClassName
  936. *
  937. * @return iterable<string, Annotation>
  938. */
  939. private function getAnnotationsOfTestClass(string $testClassName, string $annotation): iterable
  940. {
  941. $tokens = $this->createTokensForClass($testClassName);
  942. foreach ($tokens as $index => $token) {
  943. if (!$token->isGivenKind(T_DOC_COMMENT)) {
  944. continue;
  945. }
  946. $methodName = $tokens[$tokens->getNextTokenOfKind($index, [[T_STRING]])]->getContent();
  947. $docBlock = new DocBlock($token->getContent());
  948. $dataProviderAnnotations = $docBlock->getAnnotationsOfType($annotation);
  949. foreach ($dataProviderAnnotations as $dataProviderAnnotation) {
  950. yield $methodName => $dataProviderAnnotation;
  951. }
  952. }
  953. }
  954. /**
  955. * @return list<class-string>
  956. */
  957. private static function getSrcClasses(): array
  958. {
  959. static $classes;
  960. if (null !== $classes) {
  961. return $classes;
  962. }
  963. $finder = Finder::create()
  964. ->files()
  965. ->name('*.php')
  966. ->in(__DIR__.'/../../src')
  967. ->exclude([
  968. 'Resources',
  969. ])
  970. ;
  971. /** @var list<class-string> $classes */
  972. $classes = array_map(
  973. static fn (SplFileInfo $file): string => \sprintf(
  974. '%s\%s%s%s',
  975. 'PhpCsFixer',
  976. strtr($file->getRelativePath(), \DIRECTORY_SEPARATOR, '\\'),
  977. '' !== $file->getRelativePath() ? '\\' : '',
  978. $file->getBasename('.'.$file->getExtension())
  979. ),
  980. iterator_to_array($finder, false)
  981. );
  982. sort($classes);
  983. return $classes;
  984. }
  985. /**
  986. * @return list<class-string<TestCase>>
  987. */
  988. private static function getTestClasses(): array
  989. {
  990. static $classes;
  991. if (null !== $classes) {
  992. return $classes;
  993. }
  994. $finder = Finder::create()
  995. ->files()
  996. ->name('*Test.php')
  997. ->in(__DIR__.'/..')
  998. ->exclude([
  999. 'Fixtures',
  1000. ])
  1001. ;
  1002. /** @var list<class-string<TestCase>> $classes */
  1003. $classes = array_map(
  1004. static fn (SplFileInfo $file): string => \sprintf(
  1005. 'PhpCsFixer\Tests\%s%s%s',
  1006. strtr($file->getRelativePath(), \DIRECTORY_SEPARATOR, '\\'),
  1007. '' !== $file->getRelativePath() ? '\\' : '',
  1008. $file->getBasename('.'.$file->getExtension())
  1009. ),
  1010. iterator_to_array($finder, false)
  1011. );
  1012. sort($classes);
  1013. return $classes;
  1014. }
  1015. /**
  1016. * @param \ReflectionClass<object> $rc
  1017. *
  1018. * @return list<string>
  1019. */
  1020. private function getPublicMethodNames(\ReflectionClass $rc): array
  1021. {
  1022. return array_map(
  1023. static fn (\ReflectionMethod $rm): string => $rm->getName(),
  1024. $rc->getMethods(\ReflectionMethod::IS_PUBLIC)
  1025. );
  1026. }
  1027. }