forked from PrinsFrank/pdfparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSamplesTest.php
More file actions
122 lines (103 loc) · 4.66 KB
/
Copy pathSamplesTest.php
File metadata and controls
122 lines (103 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace PrinsFrank\PdfParser\Tests\Samples;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\TestCase;
use PrinsFrank\PdfParser\Document\Version\Version;
use PrinsFrank\PdfParser\PdfParser;
use PrinsFrank\PdfParser\Tests\Samples\Info\FileInfo;
use PrinsFrank\PdfParser\Tests\Samples\Info\SampleProvider;
use RuntimeException;
use TypeError;
use ValueError;
#[CoversNothing]
class SamplesTest extends TestCase {
/** @throws TypeError|ValueError|RuntimeException */
#[DataProviderExternal(SampleProvider::class, 'samples')]
public function testExternalSourcePDFs(FileInfo $fileInfo): void {
$document = (new PdfParser())->parseFile($fileInfo->pdfPath);
// PDF version
static::assertSame(Version::from(number_format($fileInfo->version / 10, 1)), $document->version);
// title
if (null !== $fileInfo->title) {
static::assertSame($fileInfo->title, $document->getInformationDictionary()?->getTitle());
}
// producer
if (null !== $fileInfo->producer) {
static::assertSame($fileInfo->producer, $document->getInformationDictionary()?->getProducer());
}
// author
if (null !== $fileInfo->author) {
static::assertSame($fileInfo->author, $document->getInformationDictionary()?->getAuthor());
}
// creator
if (null !== $fileInfo->creator) {
static::assertSame($fileInfo->creator, $document->getInformationDictionary()?->getCreator());
}
// creation date
if (null !== $fileInfo->creationDate) {
static::assertEquals($fileInfo->creationDate, $document->getInformationDictionary()?->getCreationDate());
}
// modification date
if (null !== $fileInfo->modificationDate) {
static::assertEquals($fileInfo->modificationDate, $document->getInformationDictionary()?->getModificationDate());
}
/*
* Check content related expectations
*/
if (is_array($fileInfo->textPartsExpectedSomewhereInTheExtractedText)) {
foreach ($fileInfo->textPartsExpectedSomewhereInTheExtractedText as $textPart) {
static::assertStringContainsString($textPart, $document->getText());
}
}
/*
* Check content of pages (fulltext)
*/
$pages = $fileInfo->pages ?? [];
static::assertSame(count($pages), $document->getNumberOfPages());
foreach ($pages as $index => $expectedPage) {
static::assertNotNull($page = $document->getPage($index + 1));
static::assertSame(trim($expectedPage->content), trim($page->getText()));
foreach ($expectedPage->imagePaths as $imageIndex => $imagePath) {
self::assertImage(
$imagePath,
$page->getImages()[$imageIndex]->getStream()->toString(),
sprintf('Page %d, image %d', $index, $imageIndex),
);
}
}
}
/** @throws RuntimeException */
private static function assertImage(string $expectedImagePath, string $actualImageContent, string $imageName): void {
$expectedImageContent = file_get_contents($expectedImagePath);
if ($expectedImageContent === false) {
throw new RuntimeException(sprintf('Unable to load expected image "%s"', $expectedImagePath));
}
if (str_ends_with($expectedImagePath, '.tiff')) { // gd doesn't have support for tiff so we have to compare the raw content
static::assertSame(
$expectedImageContent,
$actualImageContent,
$imageName,
);
return;
}
if (($expectedImage = imagecreatefromstring($expectedImageContent)) === false) {
throw new RuntimeException(sprintf('Unable to load expected image "%s"', $imageName));
}
if (($actualImage = imagecreatefromstring($actualImageContent)) === false) {
throw new RuntimeException(sprintf('Unable to load actual image "%s"', $imageName));
}
static::assertSame(imagesx($expectedImage), imagesx($actualImage), $imageName);
static::assertSame(imagesy($expectedImage), imagesy($actualImage), $imageName);
for ($x = 0; $x < imagesx($expectedImage); $x++) {
for ($y = 0; $y < imagesy($expectedImage); $y++) {
static::assertSame(
imagecolorat($expectedImage, $x, $y),
imagecolorat($actualImage, $x, $y),
$imageName,
);
}
}
}
}