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