AbstractIntegrationCaseFactory.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. 'os' => ['Linux', 'Darwin', 'Windows'],
  103. ]);
  104. if (!\is_int($parsed['php'])) {
  105. throw new \InvalidArgumentException(sprintf(
  106. 'Expected int value like 50509 for "php", got "%s".',
  107. get_debug_type($parsed['php']).'#'.$parsed['php'],
  108. ));
  109. }
  110. if (!\is_array($parsed['os'])) {
  111. throw new \InvalidArgumentException(sprintf(
  112. 'Expected array of OS names for "os", got "%s".',
  113. get_debug_type($parsed['os']).' ('.$parsed['os'].')',
  114. ));
  115. }
  116. return $parsed;
  117. }
  118. /**
  119. * Parses the '--RULESET--' block of a '.test' file and determines what fixers should be used.
  120. */
  121. protected function determineRuleset(SplFileInfo $file, string $config): RuleSet
  122. {
  123. return new RuleSet($this->parseJson($config));
  124. }
  125. /**
  126. * Parses the '--TEST--' block of a '.test' file and determines title.
  127. */
  128. protected function determineTitle(SplFileInfo $file, string $config): string
  129. {
  130. return $config;
  131. }
  132. /**
  133. * Parses the '--SETTINGS--' block of a '.test' file and determines settings.
  134. *
  135. * @return array{checkPriority: bool, deprecations: list<string>}
  136. */
  137. protected function determineSettings(SplFileInfo $file, ?string $config): array
  138. {
  139. $parsed = $this->parseJson($config, [
  140. 'checkPriority' => true,
  141. 'deprecations' => [],
  142. ]);
  143. if (!\is_bool($parsed['checkPriority'])) {
  144. throw new \InvalidArgumentException(sprintf(
  145. 'Expected bool value for "checkPriority", got "%s".',
  146. \is_object($parsed['checkPriority']) ? \get_class($parsed['checkPriority']) : \gettype($parsed['checkPriority']).'#'.$parsed['checkPriority']
  147. ));
  148. }
  149. if (!\is_array($parsed['deprecations'])) {
  150. throw new \InvalidArgumentException(sprintf(
  151. 'Expected array value for "deprecations", got "%s".',
  152. \is_object($parsed['deprecations']) ? \get_class($parsed['deprecations']) : \gettype($parsed['deprecations']).'#'.$parsed['deprecations']
  153. ));
  154. }
  155. foreach ($parsed['deprecations'] as $index => $deprecation) {
  156. if (!\is_string($deprecation)) {
  157. throw new \InvalidArgumentException(sprintf(
  158. 'Expected only string value for "deprecations", got "%s" @ index %d.',
  159. \is_object($deprecation) ? \get_class($deprecation) : \gettype($deprecation).'#'.$deprecation,
  160. $index
  161. ));
  162. }
  163. }
  164. return $parsed;
  165. }
  166. protected function determineExpectedCode(SplFileInfo $file, ?string $code): string
  167. {
  168. $code = $this->determineCode($file, $code, '-out.php');
  169. if (null === $code) {
  170. throw new \InvalidArgumentException('Missing expected code.');
  171. }
  172. return $code;
  173. }
  174. protected function determineInputCode(SplFileInfo $file, ?string $code): ?string
  175. {
  176. return $this->determineCode($file, $code, '-in.php');
  177. }
  178. private function determineCode(SplFileInfo $file, ?string $code, string $suffix): ?string
  179. {
  180. if (null !== $code) {
  181. return $code;
  182. }
  183. $candidateFile = new SplFileInfo($file->getPathname().$suffix, '', '');
  184. if ($candidateFile->isFile()) {
  185. return $candidateFile->getContents();
  186. }
  187. return null;
  188. }
  189. /**
  190. * @param null|array<mixed> $template
  191. *
  192. * @return array<mixed>
  193. */
  194. private function parseJson(?string $encoded, array $template = null): array
  195. {
  196. // content is optional if template is provided
  197. if ((null === $encoded || '' === $encoded) && null !== $template) {
  198. $decoded = [];
  199. } else {
  200. $decoded = json_decode($encoded, true, 512, JSON_THROW_ON_ERROR);
  201. }
  202. if (null !== $template) {
  203. foreach ($template as $index => $value) {
  204. if (!\array_key_exists($index, $decoded)) {
  205. $decoded[$index] = $value;
  206. }
  207. }
  208. }
  209. return $decoded;
  210. }
  211. }