Skip to content

Commit ef1760b

Browse files
authored
Add sample from #235 (#271)
1 parent 0166162 commit ef1760b

54 files changed

Lines changed: 2590 additions & 3 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/Samples/SamplesTest.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
use PrinsFrank\PdfParser\PdfParser;
1111
use PrinsFrank\PdfParser\Tests\Samples\Info\FileInfo;
1212
use PrinsFrank\PdfParser\Tests\Samples\Info\SampleProvider;
13+
use RuntimeException;
1314
use TypeError;
1415
use ValueError;
1516

1617
#[CoversNothing]
1718
class SamplesTest extends TestCase {
18-
/** @throws TypeError|ValueError */
19+
/** @throws TypeError|ValueError|RuntimeException */
1920
#[DataProviderExternal(SampleProvider::class, 'samples')]
2021
public function testExternalSourcePDFs(FileInfo $fileInfo): void {
2122
$document = (new PdfParser())->parseFile($fileInfo->pdfPath);
@@ -31,9 +32,48 @@ public function testExternalSourcePDFs(FileInfo $fileInfo): void {
3132
static::assertNotNull($page = $document->getPage($index + 1));
3233
static::assertSame(trim($expectedPage->content), trim($page->getText()));
3334
foreach ($expectedPage->imagePaths as $imageIndex => $imagePath) {
34-
static::assertSame(
35-
file_get_contents($imagePath),
35+
self::assertImage(
36+
$imagePath,
3637
$page->getImages()[$imageIndex]->getStream()->toString(),
38+
sprintf('Page %d, image %d', $index, $imageIndex),
39+
);
40+
}
41+
}
42+
}
43+
44+
/** @throws RuntimeException */
45+
private static function assertImage(string $expectedImagePath, string $actualImageContent, string $imageName): void {
46+
$expectedImageContent = file_get_contents($expectedImagePath);
47+
if ($expectedImageContent === false) {
48+
throw new RuntimeException(sprintf('Unable to load expected image "%s"', $expectedImagePath));
49+
}
50+
51+
if (str_ends_with($expectedImagePath, '.tiff')) { // gd doesn't have support for tiff so we have to compare the raw content
52+
static::assertSame(
53+
$expectedImageContent,
54+
$actualImageContent,
55+
$imageName,
56+
);
57+
58+
return;
59+
}
60+
61+
if (($expectedImage = imagecreatefromstring($expectedImageContent)) === false) {
62+
throw new RuntimeException(sprintf('Unable to load expected image "%s"', $imageName));
63+
}
64+
65+
if (($actualImage = imagecreatefromstring($actualImageContent)) === false) {
66+
throw new RuntimeException(sprintf('Unable to load actual image "%s"', $imageName));
67+
}
68+
69+
static::assertSame(imagesx($expectedImage), imagesx($actualImage), $imageName);
70+
static::assertSame(imagesy($expectedImage), imagesy($actualImage), $imageName);
71+
for ($x = 0; $x < imagesx($expectedImage); $x++) {
72+
for ($y = 0; $y < imagesy($expectedImage); $y++) {
73+
static::assertSame(
74+
imagecolorat($expectedImage, $x, $y),
75+
imagecolorat($actualImage, $x, $y),
76+
$imageName,
3777
);
3878
}
3979
}

tests/Samples/files/issue-235/contents.yml

Lines changed: 2547 additions & 0 deletions
Large diffs are not rendered by default.
1.51 MB
Binary file not shown.
9.11 KB
95.8 KB
14.3 KB
24.2 KB
11.6 KB
15.6 KB
11.9 KB

0 commit comments

Comments
 (0)