1010use PrinsFrank \PdfParser \PdfParser ;
1111use PrinsFrank \PdfParser \Tests \Samples \Info \FileInfo ;
1212use PrinsFrank \PdfParser \Tests \Samples \Info \SampleProvider ;
13+ use RuntimeException ;
1314use TypeError ;
1415use ValueError ;
1516
1617#[CoversNothing]
1718class 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 }
0 commit comments