-
Notifications
You must be signed in to change notification settings - Fork 41
Add PHPUnit-like contextual diff visualization for API test failures #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jakubtobiasz
wants to merge
4
commits into
lchrusciel:master
Choose a base branch
from
jakubtobiasz:claude/improve-diff-visualization-DoZwf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8a74fc0
Add PHPUnit-like contextual diff visualization for API test failures
claude 680b12d
Address PR review feedback for diff visualization feature
claude 0b7c04c
Address PR #209 review comments: improve diff output formatting
claude 7f1cf15
Make diff renderer pattern-aware to hide matching phpmatcher patterns
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of the ApiTestCase package. | ||
| * | ||
| * (c) Łukasz Chruściel | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace ApiTestCase\Renderer; | ||
|
|
||
| /** | ||
| * PHPUnit-like contextual diff renderer. | ||
| * Shows only differences with configurable context lines above and below each change. | ||
| * Marks missing expected values in red and unexpected actual values in green. | ||
| */ | ||
| class ContextualDiffRenderer extends \Diff_Renderer_Abstract | ||
| { | ||
| /** | ||
| * @var \Diff | ||
| */ | ||
| public $diff; | ||
|
|
||
| /** | ||
| * @var string|null Path to the expected response file for display | ||
| */ | ||
| private ?string $expectedFilePath; | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $options Optional configuration: | ||
| * - 'expectedFilePath' (string): Path to expected response file | ||
| */ | ||
| public function __construct(array $options = []) | ||
| { | ||
| parent::__construct($options); | ||
| $this->expectedFilePath = $options['expectedFilePath'] ?? null; | ||
| } | ||
|
|
||
| /** | ||
| * Render and return a contextual diff with color highlighting. | ||
| * | ||
| * @return string The contextual diff output | ||
| */ | ||
| public function render(): string | ||
| { | ||
| $output = ''; | ||
|
|
||
| // Add expected file path if provided | ||
| if ($this->expectedFilePath !== null) { | ||
| $output .= sprintf("--- Expected: %s\n", $this->expectedFilePath); | ||
| $output .= "+++ Actual\n"; | ||
| } | ||
|
|
||
| $opCodes = $this->diff->getGroupedOpcodes(); | ||
|
|
||
| foreach ($opCodes as $group) { | ||
| $lastItem = count($group) - 1; | ||
| $i1 = $group[0][1]; | ||
| $i2 = $group[$lastItem][2]; | ||
| $j1 = $group[0][3]; | ||
| $j2 = $group[$lastItem][4]; | ||
|
|
||
| if ($i1 == 0 && $i2 == 0) { | ||
| $i1 = -1; | ||
| $i2 = -1; | ||
| } | ||
|
|
||
| $output .= sprintf("@@ -%d,%d +%d,%d @@\n", $i1 + 1, $i2 - $i1, $j1 + 1, $j2 - $j1); | ||
|
|
||
| foreach ($group as $code) { | ||
| [$tag, $i1, $i2, $j1, $j2] = $code; | ||
|
|
||
| if ($tag === 'equal') { | ||
| // Context lines (unchanged) | ||
| $lines = $this->diff->GetA($i1, $i2); | ||
| foreach ($lines as $line) { | ||
| $output .= ' ' . $line . "\n"; | ||
| } | ||
| } else { | ||
| // Handle deletions (expected but not found in actual) | ||
| if ($tag === 'replace' || $tag === 'delete') { | ||
| $lines = $this->diff->GetA($i1, $i2); | ||
| foreach ($lines as $line) { | ||
| $output .= $this->colorRed('-' . $line) . "\n"; | ||
| } | ||
| } | ||
|
|
||
| // Handle insertions (unexpected in actual) | ||
| if ($tag === 'replace' || $tag === 'insert') { | ||
| $lines = $this->diff->GetB($j1, $j2); | ||
| foreach ($lines as $line) { | ||
| $output .= $this->colorGreen('+' . $line) . "\n"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $output; | ||
| } | ||
|
|
||
| /** | ||
| * Apply red ANSI color code to a string (for expected but missing values). | ||
| * | ||
| * @param string $text Text to colorize | ||
| * @return string Colorized text | ||
| */ | ||
| private function colorRed(string $text): string | ||
| { | ||
| if ($this->isColorSupported()) { | ||
| return "\033[31m" . $text . "\033[0m"; | ||
| } | ||
|
|
||
| return $text; | ||
| } | ||
|
|
||
| /** | ||
| * Apply green ANSI color code to a string (for unexpected actual values). | ||
| * | ||
| * @param string $text Text to colorize | ||
| * @return string Colorized text | ||
| */ | ||
| private function colorGreen(string $text): string | ||
| { | ||
| if ($this->isColorSupported()) { | ||
| return "\033[32m" . $text . "\033[0m"; | ||
| } | ||
|
|
||
| return $text; | ||
| } | ||
|
|
||
| /** | ||
| * Check if ANSI color codes are supported in the current environment. | ||
| * | ||
| * @return bool True if colors are supported | ||
| */ | ||
| private function isColorSupported(): bool | ||
| { | ||
| // Check if explicitly disabled | ||
| if (isset($_SERVER['NO_COLOR']) || getenv('NO_COLOR') !== false) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if output is to a terminal | ||
| if (function_exists('posix_isatty') && defined('STDOUT')) { | ||
| return @posix_isatty(STDOUT); | ||
| } | ||
|
|
||
| // For Windows, check for ANSICON or ConEmu or Windows 10+ | ||
| if (DIRECTORY_SEPARATOR === '\\') { | ||
| return (getenv('ANSICON') !== false || getenv('ConEmuANSI') === 'ON' || | ||
| (function_exists('sapi_windows_vt100_support') && @sapi_windows_vt100_support(STDOUT))); | ||
| } | ||
|
|
||
| // Default to true for Unix-like systems | ||
| return true; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.