123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Test;
- use PhpCsFixer\RuleSet;
- use Symfony\Component\Finder\SplFileInfo;
- /**
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * @internal
- */
- final class IntegrationCaseFactory
- {
- /**
- * @param SplFileInfo $file
- *
- * @return IntegrationCase
- */
- public function create(SplFileInfo $file)
- {
- try {
- if (!preg_match(
- '/^
- --TEST-- \r?\n(?<title> .*?)
- \s --RULESET-- \r?\n(?<ruleset> .*?)
- (?:\s --CONFIG-- \r?\n(?<config> .*?))?
- (?:\s --SETTINGS-- \r?\n(?<settings> .*?))?
- (?:\s --REQUIREMENTS-- \r?\n(?<requirements> .*?))?
- (?:\s --EXPECT-- \r?\n(?<expect> .*?\r?\n*))?
- (?:\s --INPUT-- \r?\n(?<input> .*))?
- $/sx',
- $file->getContents(),
- $match
- )) {
- throw new \InvalidArgumentException('File format is invalid.');
- }
- $match = array_merge(
- array(
- 'config' => null,
- 'settings' => null,
- 'requirements' => null,
- 'expect' => null,
- 'input' => null,
- ),
- $match
- );
- return new IntegrationCase(
- $file->getRelativePathname(),
- $match['title'],
- $this->determineSettings($match['settings']),
- $this->determineRequirements($match['requirements']),
- $this->determineConfig($match['config']),
- $this->determineRuleset($match['ruleset']),
- $this->determineExpectedCode($match['expect'], $file),
- $this->determineInputCode($match['input'], $file)
- );
- } catch (\InvalidArgumentException $e) {
- throw new \InvalidArgumentException(
- sprintf('%s Test file: "%s".', $e->getMessage(), $file->getRelativePathname()),
- $e->getCode(),
- $e
- );
- }
- }
- /**
- * Parses the '--CONFIG--' block of a '.test' file.
- *
- * @param string $config
- *
- * @return array
- */
- private function determineConfig($config)
- {
- $parsed = $this->parseJson($config, array(
- 'indent' => ' ',
- 'lineEnding' => "\n",
- ));
- if (!is_string($parsed['indent'])) {
- throw new \InvalidArgumentException(sprintf(
- 'Expected string value for "indent", got "%s".',
- is_object($parsed['indent']) ? get_class($parsed['indent']) : gettype($parsed['indent']).'#'.$parsed['indent']
- ));
- }
- if (!is_string($parsed['lineEnding'])) {
- throw new \InvalidArgumentException(sprintf(
- 'Expected string value for "lineEnding", got "%s".',
- is_object($parsed['lineEnding']) ? get_class($parsed['lineEnding']) : gettype($parsed['lineEnding']).'#'.$parsed['lineEnding']
- ));
- }
- return $parsed;
- }
- /**
- * Parses the '--REQUIREMENTS--' block of a '.test' file and determines requirements.
- *
- * @param string $config
- *
- * @return array
- */
- private function determineRequirements($config)
- {
- $parsed = $this->parseJson($config, array(
- 'hhvm' => true,
- 'php' => PHP_VERSION_ID,
- ));
- if (!is_int($parsed['php']) || $parsed['php'] < 50306) {
- throw new \InvalidArgumentException(sprintf(
- 'Expected int >= 50306 value for "php", got "%s".',
- is_object($parsed['php']) ? get_class($parsed['php']) : gettype($parsed['php']).'#'.$parsed['php']
- ));
- }
- if (!is_bool($parsed['hhvm'])) {
- throw new \InvalidArgumentException(sprintf(
- 'Expected bool value for "hhvm", got "%s".',
- is_object($parsed['hhvm']) ? get_class($parsed['hhvm']) : gettype($parsed['hhvm']).'#'.$parsed['hhvm']
- ));
- }
- return $parsed;
- }
- /**
- * Parses the '--RULESET--' block of a '.test' file and determines what fixers should be used.
- *
- * @param string $config
- *
- * @return RuleSet
- */
- private function determineRuleset($config)
- {
- return new RuleSet($this->parseJson($config));
- }
- /**
- * Parses the '--SETTINGS--' block of a '.test' file and determines settings.
- *
- * @param string $config
- *
- * @return array
- */
- private function determineSettings($config)
- {
- $parsed = $this->parseJson($config, array(
- 'checkPriority' => true,
- ));
- if (!is_bool($parsed['checkPriority'])) {
- throw new \InvalidArgumentException(sprintf(
- 'Expected bool value for "checkPriority", got "%s".',
- is_object($parsed['checkPriority']) ? get_class($parsed['checkPriority']) : gettype($parsed['checkPriority']).'#'.$parsed['checkPriority']
- ));
- }
- return $parsed;
- }
- /**
- * @param null|string $code
- * @param SplFileInfo $file
- *
- * @return string
- */
- private function determineExpectedCode($code, SplFileInfo $file)
- {
- $code = $this->determineCode($code, $file, '-out.php');
- if (null === $code) {
- throw new \InvalidArgumentException('Missing expected code.');
- }
- return $code;
- }
- /**
- * @param null|string $code
- * @param SplFileInfo $file
- *
- * @return null|string
- */
- private function determineInputCode($code, SplFileInfo $file)
- {
- return $this->determineCode($code, $file, '-in.php');
- }
- /**
- * @param null|string $code
- * @param SplFileInfo $file
- * @param string $suffix
- *
- * @return null|string
- */
- private function determineCode($code, SplFileInfo $file, $suffix)
- {
- if (null !== $code) {
- return $code;
- }
- $candidateFile = new SplFileInfo($file->getPathname().$suffix, '', '');
- if ($candidateFile->isFile()) {
- return $candidateFile->getContents();
- }
- }
- /**
- * @param null|string $encoded
- * @param null|array $template
- *
- * @return array
- */
- private function parseJson($encoded, array $template = null)
- {
- // content is optional if template is provided
- if (!$encoded && null !== $template) {
- $decoded = array();
- } else {
- $decoded = json_decode($encoded, true);
- if (JSON_ERROR_NONE !== json_last_error()) {
- throw new \InvalidArgumentException(sprintf('Malformed JSON: "%s", error: "%s".', $encoded, json_last_error_msg()));
- }
- }
- if (null !== $template) {
- $decoded = array_merge(
- $template,
- array_intersect_key(
- $decoded,
- array_flip(array_keys($template))
- )
- );
- }
- return $decoded;
- }
- }
|