|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +// Tests that CacheMetaExtensionInterface invalidates cache when the hash changes. |
| 5 | +// |
| 6 | +// Step 1: Run Rector with enabled.txt=false and --clear-cache → no changes, cache populated |
| 7 | +// Step 2: Change enabled.txt to true → cache invalidated → rule triggers → changes reported |
| 8 | + |
| 9 | +use Rector\Console\Formatter\ColorConsoleDiffFormatter; |
| 10 | +use Rector\Console\Style\SymfonyStyleFactory; |
| 11 | +use Rector\Differ\DefaultDiffer; |
| 12 | +use Rector\Util\Reflection\PrivatesAccessor; |
| 13 | +use Symfony\Component\Console\Command\Command; |
| 14 | + |
| 15 | +$projectRoot = __DIR__ .'/..'; |
| 16 | +$rectorBin = $projectRoot . '/../bin/rector'; |
| 17 | +$autoloadFile = $projectRoot . '/../vendor/autoload.php'; |
| 18 | + |
| 19 | +require_once __DIR__ . '/../../vendor/autoload.php'; |
| 20 | + |
| 21 | +$symfonyStyleFactory = new SymfonyStyleFactory(new PrivatesAccessor()); |
| 22 | +$symfonyStyle = $symfonyStyleFactory->create(); |
| 23 | + |
| 24 | +$e2eCommand = 'php '. $rectorBin .' process --dry-run --no-ansi -a '. $autoloadFile; |
| 25 | + |
| 26 | +// Step 1: enabled=false, clear cache → no changes |
| 27 | +file_put_contents(__DIR__ . '/enabled.txt', "false\n"); |
| 28 | + |
| 29 | +$output = []; |
| 30 | +exec($e2eCommand . ' --clear-cache', $output, $exitCode); |
| 31 | +$outputString = trim(implode("\n", $output)); |
| 32 | + |
| 33 | +if (! str_contains($outputString, '[OK] Rector is done!')) { |
| 34 | + $symfonyStyle->error('Step 1 failed: Expected no changes with enabled=false'); |
| 35 | + $symfonyStyle->writeln($outputString); |
| 36 | + exit(Command::FAILURE); |
| 37 | +} |
| 38 | + |
| 39 | +$symfonyStyle->success('Step 1 passed: No changes with enabled=false'); |
| 40 | + |
| 41 | +// Step 2: enabled=true, no --clear-cache → cache meta invalidated → rule triggers |
| 42 | +file_put_contents(__DIR__ . '/enabled.txt', "true\n"); |
| 43 | + |
| 44 | +$output = []; |
| 45 | +exec($e2eCommand, $output, $exitCode); |
| 46 | +$outputString = trim(implode("\n", $output)); |
| 47 | +$outputString = str_replace(__DIR__, '.', $outputString); |
| 48 | + |
| 49 | +$expectedOutput = trim((string) file_get_contents(__DIR__ . '/expected-output.diff')); |
| 50 | + |
| 51 | +// Restore enabled.txt |
| 52 | +file_put_contents(__DIR__ . '/enabled.txt', "false\n"); |
| 53 | + |
| 54 | +if ($outputString === $expectedOutput) { |
| 55 | + $symfonyStyle->success('Step 2 passed: Cache invalidated, rule triggered'); |
| 56 | + exit(Command::SUCCESS); |
| 57 | +} |
| 58 | + |
| 59 | +$symfonyStyle->error('Step 2 failed: Expected cache invalidation to trigger the rule'); |
| 60 | + |
| 61 | +$defaultDiffer = new DefaultDiffer(); |
| 62 | +$colorConsoleDiffFormatter = new ColorConsoleDiffFormatter(); |
| 63 | +$diff = $colorConsoleDiffFormatter->format($defaultDiffer->diff($outputString, $expectedOutput)); |
| 64 | +$symfonyStyle->writeln($diff); |
| 65 | + |
| 66 | +exit(Command::FAILURE); |
0 commit comments