AbstractIntegrationCaseFactory.php 7.9 KB

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