From 1895c17417aefa4508e35836c46da61988d61f26 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Tue, 13 Apr 2021 10:27:56 +0200 Subject: [PATCH] Replaces #403 (fix Call to a member function getFontSpaceLimit() on null) (#406) * try to trigger error which is mentioned in #403 * allow CI to run on all push events * reverted changes to provoke error message, error: Call to a member function getFontSpaceLimit() on null * deployed fixes by @xfolder; added test case to reproduce error * refined test function * PHPUnit::expectNotToPerformAssertions not available when using PHP 5.6 and 7.0; Use doesNotPerformAssertions as PHP doc comment. Co-authored-by: Mirko --- src/Smalot/PdfParser/PDFObject.php | 4 ++-- tests/Integration/FontTest.php | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Smalot/PdfParser/PDFObject.php b/src/Smalot/PdfParser/PDFObject.php index d37612fa..61278e1e 100644 --- a/src/Smalot/PdfParser/PDFObject.php +++ b/src/Smalot/PdfParser/PDFObject.php @@ -259,7 +259,7 @@ private function getDefaultFont(Page $page = null) return reset($fonts); } - return new Font($this->document); + return new Font($this->document, null, null, $this->config); } /** @@ -488,7 +488,7 @@ public function getTextArray(Page $page = null) { $text = []; $sections = $this->getSectionsText($this->content); - $current_font = new Font($this->document); + $current_font = new Font($this->document, null, null, $this->config); foreach ($sections as $section) { $commands = $this->getCommandsText($section); diff --git a/tests/Integration/FontTest.php b/tests/Integration/FontTest.php index 1f7d75d7..719de7f7 100644 --- a/tests/Integration/FontTest.php +++ b/tests/Integration/FontTest.php @@ -32,6 +32,7 @@ namespace Tests\Smalot\PdfParser\Integration; +use Smalot\PdfParser\Config; use Smalot\PdfParser\Document; use Smalot\PdfParser\Element; use Smalot\PdfParser\Encoding; @@ -348,6 +349,25 @@ public function testDecodeText() $this->assertEquals('æöü', $font->decodeText($commands)); } + /** + * Tests buggy behavior which lead to: + * + * Call to a member function getFontSpaceLimit() on null + * + * @see https://github.com/smalot/pdfparser/pull/403 + * + * @doesNotPerformAssertions + */ + public function testTriggerGetFontSpaceLimitOnNull() + { + // error is triggered, if we set the fourth parameter to null + $font = new Font(new Document(), null, null, new Config()); + + // both functions can trigger the error + $font->decodeText([]); + $font->getTextArray(); + } + public function testXmlContent() { $filename = $this->rootDir.'/samples/bugs/Issue18.pdf';