AbstractIntegrationCaseFactory.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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\Test;
  13. use PhpCsFixer\RuleSet\RuleSet;
  14. use Symfony\Component\Finder\SplFileInfo;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. */
  20. abstract class AbstractIntegrationCaseFactory implements IntegrationCaseFactoryInterface
  21. {
  22. public function create(SplFileInfo $file): IntegrationCase
  23. {
  24. try {
  25. if (!preg_match(
  26. '/^
  27. --TEST-- \r?\n(?<title> .*?)
  28. \s --RULESET-- \r?\n(?<ruleset> .*?)
  29. (?:\s --CONFIG-- \r?\n(?<config> .*?))?
  30. (?:\s --SETTINGS-- \r?\n(?<settings> .*?))?
  31. (?:\s --REQUIREMENTS-- \r?\n(?<requirements> .*?))?
  32. (?:\s --EXPECT-- \r?\n(?<expect> .*?\r?\n*))?
  33. (?:\s --INPUT-- \r?\n(?<input> .*))?
  34. $/sx',
  35. $file->getContents(),
  36. $match
  37. )) {
  38. throw new \InvalidArgumentException('File format is invalid.');
  39. }
  40. $match = array_merge(
  41. [
  42. 'config' => null,
  43. 'settings' => null,
  44. 'requirements' => null,
  45. 'expect' => null,
  46. 'input' => null,
  47. ],
  48. $match
  49. );
  50. return new IntegrationCase(
  51. $file->getRelativePathname(),
  52. $this->determineTitle($file, $match['title']),
  53. $this->determineSettings($file, $match['settings']),
  54. $this->determineRequirements($file, $match['requirements']),
  55. $this->determineConfig($file, $match['config']),
  56. $this->determineRuleset($file, $match['ruleset']),
  57. $this->determineExpectedCode($file, $match['expect']),
  58. $this->determineInputCode($file, $match['input'])
  59. );
  60. } catch (\InvalidArgumentException $e) {
  61. throw new \InvalidArgumentException(
  62. \sprintf('%s Test file: "%s".', $e->getMessage(), $file->getPathname()),
  63. $e->getCode(),
  64. $e
  65. );
  66. }
  67. }
  68. /**
  69. * Parses the '--CONFIG--' block of a '.test' file.
  70. *
  71. * @return array{indent: string, lineEnding: string}
  72. */
  73. protected function determineConfig(SplFileInfo $file, ?string $config): array
  74. {
  75. $parsed = $this->parseJson($config, [
  76. 'indent' => ' ',
  77. 'lineEnding' => "\n",
  78. ]);
  79. if (!\is_string($parsed['indent'])) {
  80. throw new \InvalidArgumentException(\sprintf(
  81. 'Expected string value for "indent", got "%s".',
  82. \is_object($parsed['indent']) ? \get_class($parsed['indent']) : \gettype($parsed['indent']).'#'.$parsed['indent']
  83. ));
  84. }
  85. if (!\is_string($parsed['lineEnding'])) {
  86. throw new \InvalidArgumentException(\sprintf(
  87. 'Expected string value for "lineEnding", got "%s".',
  88. \is_object($parsed['lineEnding']) ? \get_class($parsed['lineEnding']) : \gettype($parsed['lineEnding']).'#'.$parsed['lineEnding']
  89. ));
  90. }
  91. return $parsed;
  92. }
  93. /**
  94. * Parses the '--REQUIREMENTS--' block of a '.test' file and determines requirements.
  95. *
  96. * @return array{php: int, "php<": int, os: list<string>}
  97. */
  98. protected function determineRequirements(SplFileInfo $file, ?string $config): array
  99. {
  100. $parsed = $this->parseJson($config, [
  101. 'php' => \PHP_VERSION_ID,
  102. 'php<' => PHP_INT_MAX,
  103. 'os' => ['Linux', 'Darwin', 'Windows'],
  104. ]);
  105. if (!\is_int($parsed['php'])) {
  106. throw new \InvalidArgumentException(\sprintf(
  107. 'Expected int value like 50509 for "php", got "%s".',
  108. get_debug_type($parsed['php']).'#'.$parsed['php'],
  109. ));
  110. }
  111. if (!\is_int($parsed['php<'])) {
  112. throw new \InvalidArgumentException(\sprintf(
  113. 'Expected int value like 80301 for "php<", got "%s".',
  114. get_debug_type($parsed['php<']).'#'.$parsed['php<'],
  115. ));
  116. }
  117. if (!\is_array($parsed['os'])) {
  118. throw new \InvalidArgumentException(\sprintf(
  119. 'Expected array of OS names for "os", got "%s".',
  120. get_debug_type($parsed['os']).' ('.$parsed['os'].')',
  121. ));
  122. }
  123. return $parsed;
  124. }
  125. /**
  126. * Parses the '--RULESET--' block of a '.test' file and determines what fixers should be used.
  127. */
  128. protected function determineRuleset(SplFileInfo $file, string $config): RuleSet
  129. {
  130. return new RuleSet($this->parseJson($config));
  131. }
  132. /**
  133. * Parses the '--TEST--' block of a '.test' file and determines title.
  134. */
  135. protected function determineTitle(SplFileInfo $file, string $config): string
  136. {
  137. return $config;
  138. }
  139. /**
  140. * Parses the '--SETTINGS--' block of a '.test' file and determines settings.
  141. *
  142. * @return array{checkPriority: bool, deprecations: list<string>}
  143. */
  144. protected function determineSettings(SplFileInfo $file, ?string $config): array
  145. {
  146. $parsed = $this->parseJson($config, [
  147. 'checkPriority' => true,
  148. 'deprecations' => [],
  149. ]);
  150. if (!\is_bool($parsed['checkPriority'])) {
  151. throw new \InvalidArgumentException(\sprintf(
  152. 'Expected bool value for "checkPriority", got "%s".',
  153. \is_object($parsed['checkPriority']) ? \get_class($parsed['checkPriority']) : \gettype($parsed['checkPriority']).'#'.$parsed['checkPriority']
  154. ));
  155. }
  156. if (!\is_array($parsed['deprecations'])) {
  157. throw new \InvalidArgumentException(\sprintf(
  158. 'Expected array value for "deprecations", got "%s".',
  159. \is_object($parsed['deprecations']) ? \get_class($parsed['deprecations']) : \gettype($parsed['deprecations']).'#'.$parsed['deprecations']
  160. ));
  161. }
  162. foreach ($parsed['deprecations'] as $index => $deprecation) {
  163. if (!\is_string($deprecation)) {
  164. throw new \InvalidArgumentException(\sprintf(
  165. 'Expected only string value for "deprecations", got "%s" @ index %d.',
  166. \is_object($deprecation) ? \get_class($deprecation) : \gettype($deprecation).'#'.$deprecation,
  167. $index
  168. ));
  169. }
  170. }
  171. return $parsed;
  172. }
  173. protected function determineExpectedCode(SplFileInfo $file, ?string $code): string
  174. {
  175. $code = $this->determineCode($file, $code, '-out.php');
  176. if (null === $code) {
  177. throw new \InvalidArgumentException('Missing expected code.');
  178. }
  179. return $code;
  180. }
  181. protected function determineInputCode(SplFileInfo $file, ?string $code): ?string
  182. {
  183. return $this->determineCode($file, $code, '-in.php');
  184. }
  185. private function determineCode(SplFileInfo $file, ?string $code, string $suffix): ?string
  186. {
  187. if (null !== $code) {
  188. return $code;
  189. }
  190. $candidateFile = new SplFileInfo($file->getPathname().$suffix, '', '');
  191. if ($candidateFile->isFile()) {
  192. return $candidateFile->getContents();
  193. }
  194. return null;
  195. }
  196. /**
  197. * @param null|array<string, mixed> $template
  198. *
  199. * @return array<string, mixed>
  200. */
  201. private function parseJson(?string $encoded, ?array $template = null): array
  202. {
  203. // content is optional if template is provided
  204. if ((null === $encoded || '' === $encoded) && null !== $template) {
  205. $decoded = [];
  206. } else {
  207. $decoded = json_decode($encoded, true, 512, JSON_THROW_ON_ERROR);
  208. }
  209. if (null !== $template) {
  210. foreach ($template as $index => $value) {
  211. if (!\array_key_exists($index, $decoded)) {
  212. $decoded[$index] = $value;
  213. }
  214. }
  215. }
  216. return $decoded;
  217. }
  218. }