Skip to content

Commit ddd4285

Browse files
committed
test(snapshots) refresh scaffold snapshots for 4.7 output
init scaffold now emits the dev:rebuild command and the tests/_wordpress, tests/_data and Support/Data .gitignore files (4.7.0); regenerate the 3.5 scaffold snapshots to match.
1 parent b9e142d commit ddd4285

9 files changed

Lines changed: 127 additions & 344 deletions

config/rector-35.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use lucatume\Rector\SerializableThrowableCompatibilityRector;
1111
use Rector\Config\RectorConfig;
1212
use Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector;
13+
use Rector\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector;
1314
use Rector\DowngradePhp81\Rector\FuncCall\DowngradeHashAlgorithmXxHashRector;
1415
use Rector\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector;
1516
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
@@ -66,10 +67,15 @@
6667
// after every `new ReflectionProperty`. The master source already guards each call with
6768
// `PHP_VERSION_ID < 80100 && $prop->setAccessible(true)`, so the injected copies are redundant
6869
// and, in one spot, land before the variable is assigned (fatal). Skip it; master's guards stand.
70+
//
71+
// DowngradeMatchToSwitchRector drops the left side of `$x = $a ?? match(...)`; skip it for
72+
// MachineInformation (its only matches use that pattern) so DowngradeCoalesceMatchAssignRector
73+
// can downgrade them without losing the constructor-argument fallback.
6974
$rectorConfig->skip([
7075
DowngradeParameterTypeWideningRector::class,
7176
DowngradeHashAlgorithmXxHashRector::class,
7277
DowngradeSetAccessibleReflectionPropertyRector::class,
78+
DowngradeMatchToSwitchRector::class => [dirname(__DIR__) . '/src/Utils/MachineInformation.php'],
7379
]);
7480

7581
// Downgrade PHP_OS_FAMILY (PHP 7.2+) to PHP_OS for PHP 7.1 compatibility

config/rector/src/DowngradeCoalesceMatchAssignRector.php

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,38 @@
88
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
99
use PhpParser\Node\Expr\ConstFetch;
1010
use PhpParser\Node\Expr\Match_;
11+
use PhpParser\Node\MatchArm;
1112
use PhpParser\Node\Name;
13+
use PhpParser\Node\Stmt\Break_;
14+
use PhpParser\Node\Stmt\Case_;
1215
use PhpParser\Node\Stmt\Else_;
1316
use PhpParser\Node\Stmt\Expression;
1417
use PhpParser\Node\Stmt\If_;
18+
use PhpParser\Node\Stmt\Switch_;
1519
use Rector\Rector\AbstractRector;
1620
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1721
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1822

1923
/**
20-
* Split `$x = $a ?? match (...) { ... };` into an if/else before the match is downgraded.
24+
* Downgrade `$x = $a ?? match (...) { ... };` to an if/else for PHP < 8.0.
2125
*
22-
* Rector's DowngradeMatchToSwitchRector turns the nested match into a switch that assigns $x
23-
* directly but drops the `$a ??` fallback, so the provided value is ignored (e.g.
26+
* Rector's built-in DowngradeMatchToSwitchRector turns the nested match into a switch that
27+
* assigns $x directly but drops the `$a ??` fallback, so the provided value is ignored (e.g.
2428
* MachineInformation ignoring its constructor arguments and always auto-detecting the host).
25-
* Rewriting the coalesce into an explicit if/else keeps the match in a plain `$x = match(...)`
26-
* position the downgrade handles correctly, and preserves the `$a ??` semantics.
29+
* The built-in is skipped for the affected file in rector-35.php and this rule does the whole
30+
* downgrade: `if ($a !== null) { $x = $a; } else { switch (subject) { ... } }`, preserving the
31+
* `$a ??` semantics.
2732
*/
2833
class DowngradeCoalesceMatchAssignRector extends AbstractRector
2934
{
3035
public function getRuleDefinition(): RuleDefinition
3136
{
3237
return new RuleDefinition(
33-
'Split an assignment of "value ?? match(...)" into if/else so the coalesce survives the match downgrade',
38+
'Downgrade an assignment of "value ?? match(...)" to if/else, preserving the coalesce',
3439
[
3540
new CodeSample(
3641
'$this->os = $os ?? match ($d) { "l" => "linux", default => "?" };',
37-
"if (\$os !== null) {\n \$this->os = \$os;\n} else {\n \$this->os = match (\$d) { \"l\" => \"linux\", default => \"?\" };\n}"
42+
"if (\$os !== null) {\n \$this->os = \$os;\n} else {\n switch (\$d) {\n case \"l\":\n \$this->os = \"linux\";\n break;\n default:\n \$this->os = \"?\";\n break;\n }\n}"
3843
),
3944
]
4045
);
@@ -61,14 +66,43 @@ public function refactor(Node $node): ?Node
6166
}
6267

6368
$target = $assign->var;
64-
$fallbackValue = $coalesce->left;
6569

6670
return new If_(
67-
new NotIdentical($fallbackValue, new ConstFetch(new Name('null'))),
71+
new NotIdentical($coalesce->left, new ConstFetch(new Name('null'))),
6872
[
69-
'stmts' => [new Expression(new Assign($target, $fallbackValue))],
70-
'else' => new Else_([new Expression(new Assign($target, $coalesce->right))]),
73+
'stmts' => [new Expression(new Assign($target, $coalesce->left))],
74+
'else' => new Else_([$this->matchToSwitch($coalesce->right, $target)]),
7175
]
7276
);
7377
}
78+
79+
private function matchToSwitch(Match_ $match, Node $target): Switch_
80+
{
81+
$cases = [];
82+
foreach ($match->arms as $arm) {
83+
$cases = array_merge($cases, $this->armToCases($arm, $target));
84+
}
85+
return new Switch_($match->cond, $cases);
86+
}
87+
88+
/**
89+
* @return Case_[]
90+
*/
91+
private function armToCases(MatchArm $arm, Node $target): array
92+
{
93+
$body = [new Expression(new Assign($target, $arm->body)), new Break_()];
94+
95+
// Default arm: conds is null.
96+
if ($arm->conds === null) {
97+
return [new Case_(null, $body)];
98+
}
99+
100+
// One arm can list several conditions (`'a', 'b' => ...`): fall through to the last.
101+
$cases = [];
102+
$last = count($arm->conds) - 1;
103+
foreach ($arm->conds as $i => $cond) {
104+
$cases[] = new Case_($cond, $i === $last ? $body : []);
105+
}
106+
return $cases;
107+
}
74108
}

tests/unit/Codeception/Template/__snapshots__/WpbrowserTest__3.5__should_scaffold_for_child_theme_correctly__0.snapshot

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ add_action('after_setup_theme', 'theme_23_some_function');
1212
<<< /index.php <<<
1313

1414
>>> /tests/EndToEnd.suite.yml >>>
15-
# Integration suite configuration
15+
# EndToEnd suite configuration
1616
#
17-
# Run integration and "WordPress unit" tests.
17+
# Run end-to-end tests.
1818

1919
actor: EndToEndTester
2020
bootstrap: _bootstrap.php
@@ -290,8 +290,8 @@ TEST_TABLE_PREFIX=test_
290290
WORDPRESS_TABLE_PREFIX=wp_
291291

292292
# The URL and domain of the WordPress site used in end-to-end tests.
293-
WORDPRESS_URL=http://localhost:33117
294-
WORDPRESS_DOMAIN=localhost:33117
293+
WORDPRESS_URL=http://localhost:45628
294+
WORDPRESS_DOMAIN=localhost:45628
295295
WORDPRESS_ADMIN_PATH=/wp-admin
296296

297297
# The username and password of the administrator user of the WordPress site used in end-to-end tests.
@@ -300,20 +300,24 @@ WORDPRESS_ADMIN_PASSWORD=password
300300

301301
# The host and port of the ChromeDriver server that will be used in end-to-end tests.
302302
CHROMEDRIVER_HOST=localhost
303-
CHROMEDRIVER_PORT=45469
303+
CHROMEDRIVER_PORT=13862
304304

305305
# The port on which the PHP built-in server will serve the WordPress installation.
306-
BUILTIN_SERVER_PORT=33117
306+
BUILTIN_SERVER_PORT=45628
307+
# The WordPress version installed by `init`, reused by the `dev:rebuild` command.
308+
WORDPRESS_VERSION=6.8.1
309+
307310

308311
<<< /tests/.env <<<
309312

310313
>>> /tests/_output/.gitkeep >>>
311314

312315
<<< /tests/_output/.gitkeep <<<
313316

314-
>>> /tests/_data/.gitkeep >>>
317+
>>> /tests/_data/.gitignore >>>
318+
!dump.sql
315319

316-
<<< /tests/_data/.gitkeep <<<
320+
<<< /tests/_data/.gitignore <<<
317321

318322
>>> /codeception.yml >>>
319323
support_namespace: Support
@@ -360,6 +364,7 @@ extensions:
360364
- lucatume\WPBrowser\Command\DevStop
361365
- lucatume\WPBrowser\Command\DevInfo
362366
- lucatume\WPBrowser\Command\DevRestart
367+
- lucatume\WPBrowser\Command\DevRebuild
363368
- lucatume\WPBrowser\Command\ChromedriverUpdate
364369

365370
<<< /codeception.yml <<<

tests/unit/Codeception/Template/__snapshots__/WpbrowserTest__3.5__should_scaffold_for_plugin_with_non_plugin_php_file__0.snapshot

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
>>> /tests/EndToEnd.suite.yml >>>
2-
# Integration suite configuration
2+
# EndToEnd suite configuration
33
#
4-
# Run integration and "WordPress unit" tests.
4+
# Run end-to-end tests.
55

66
actor: EndToEndTester
77
bootstrap: _bootstrap.php
@@ -285,8 +285,8 @@ TEST_TABLE_PREFIX=test_
285285
WORDPRESS_TABLE_PREFIX=wp_
286286

287287
# The URL and domain of the WordPress site used in end-to-end tests.
288-
WORDPRESS_URL=http://localhost:46881
289-
WORDPRESS_DOMAIN=localhost:46881
288+
WORDPRESS_URL=http://localhost:9261
289+
WORDPRESS_DOMAIN=localhost:9261
290290
WORDPRESS_ADMIN_PATH=/wp-admin
291291

292292
# The username and password of the administrator user of the WordPress site used in end-to-end tests.
@@ -295,20 +295,24 @@ WORDPRESS_ADMIN_PASSWORD=password
295295

296296
# The host and port of the ChromeDriver server that will be used in end-to-end tests.
297297
CHROMEDRIVER_HOST=localhost
298-
CHROMEDRIVER_PORT=7413
298+
CHROMEDRIVER_PORT=64577
299299

300300
# The port on which the PHP built-in server will serve the WordPress installation.
301-
BUILTIN_SERVER_PORT=46881
301+
BUILTIN_SERVER_PORT=9261
302+
# The WordPress version installed by `init`, reused by the `dev:rebuild` command.
303+
WORDPRESS_VERSION=6.8.1
304+
302305

303306
<<< /tests/.env <<<
304307

305308
>>> /tests/_output/.gitkeep >>>
306309

307310
<<< /tests/_output/.gitkeep <<<
308311

309-
>>> /tests/_data/.gitkeep >>>
312+
>>> /tests/_data/.gitignore >>>
313+
!dump.sql
310314

311-
<<< /tests/_data/.gitkeep <<<
315+
<<< /tests/_data/.gitignore <<<
312316

313317
>>> /codeception.yml >>>
314318
support_namespace: Support
@@ -355,6 +359,7 @@ extensions:
355359
- lucatume\WPBrowser\Command\DevStop
356360
- lucatume\WPBrowser\Command\DevInfo
357361
- lucatume\WPBrowser\Command\DevRestart
362+
- lucatume\WPBrowser\Command\DevRebuild
358363
- lucatume\WPBrowser\Command\ChromedriverUpdate
359364

360365
<<< /codeception.yml <<<

0 commit comments

Comments
 (0)