Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ The `PdfTestTrait` provides methods to compare a `TCPDF` object against an image
- `self::assertSamePdf(string|SplFileInfo|resource|TCPDF, TCPDF)`
- `self::assertNotSamePdf(string|SplFileInfo|resource|TCPDF, TCPDF)`.

**For both ImageTestTrait and PdfTestTrait:**

The environment variable `PHPUNIT_EXTENSIONS_IMAGE_DIFF_OUTPUT_PATH` can be set to a directory where a `diff.html` will be generated which will
show the differences.
show the differences. Optionally, the environment variable `PHPUNIT_EXTENSIONS_IMAGE_DIFF_OUTPUT_URL` can be set to inform the user where
to find the outputted `diff.html` file. This is useful when running tests in a CI environment.

## About us

Expand Down
12 changes: 9 additions & 3 deletions src/Renderer/ImageDiffRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@

/**
* Renders the results IsSameImageConstraint difference to local html file. Can be enabled by setting the `PHPUNIT_EXTENSIONS_IMAGE_DIFF_OUTPUT_PATH`
* Optionally an output url can be set which will be used to notify the user where to find the rendered diff.
*/
class ImageDiffRenderer implements ImageDiffRendererInterface
{
private readonly ?string $outputPath;
private readonly ?string $outputUrl;

public function __construct(?string $outputPath = null)
public function __construct(?string $outputPath = null, ?string $outputUrl = null)
{
$outputPath ??= $_SERVER['PHPUNIT_EXTENSIONS_IMAGE_DIFF_OUTPUT_PATH'] ?? null;
assert(is_string($outputPath) || $outputPath === null, 'PHPUNIT_EXTENSIONS_IMAGE_DIFF_OUTPUT_PATH must be a string or null');
$this->outputPath = $outputPath;
$this->outputPath = $outputPath !== null ? rtrim($outputPath, '/\\') : null;

$outputUrl ??= $_SERVER['PHPUNIT_EXTENSIONS_IMAGE_DIFF_OUTPUT_URL'] ?? null;
assert(is_string($outputUrl) || $outputUrl === null, 'PHPUNIT_EXTENSIONS_IMAGE_DIFF_OUTPUT_URL must be a string or null');
$this->outputUrl = $outputUrl !== null ? rtrim($outputUrl, '/') : null;
}

/**
Expand Down Expand Up @@ -48,7 +54,7 @@ public function render(Imagick $diff, Imagick $expected, Imagick $actual): ?stri
$html = str_replace(array_keys($replaces), array_values($replaces), $this->createHtml());
file_put_contents($this->outputPath . DIRECTORY_SEPARATOR . 'diff.html', $html);

return 'View the differences at ' . $this->outputPath . '/diff.html';
return 'View the differences at ' . ($this->outputUrl === null ? $this->outputPath : $this->outputUrl) . '/diff.html';
}

private function createHtml(): string
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/Renderer/ImageDiffRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,22 @@ public function testRenderShouldSkipOnAbsentOutputPath(): void
static::assertNull($result);
static::assertFileDoesNotExist($this->path . '/pdf/diff.html');
}

public function testRenderShouldShowOutputUrl(): void
{
$diff = $this->createMock(Imagick::class);
$diff->expects(static::once())->method('setImageFormat')->with('png');
$diff->expects(static::once())->method('getImageBlob')->willReturn('diff');

$expected = $this->createMock(Imagick::class);
$expected->expects(static::once())->method('setImageFormat')->with('png');
$expected->expects(static::once())->method('getImageBlob')->willReturn('expected');

$actual = $this->createMock(Imagick::class);
$actual->expects(static::once())->method('setImageFormat')->with('png');
$actual->expects(static::once())->method('getImageBlob')->willReturn('actual');

$result = (new ImageDiffRenderer($this->path . '/pdf', 'https://example.com/'))->render($diff, $expected, $actual);
static::assertSame('View the differences at https://example.com/diff.html', $result);
}
}