From 8b8a157050655c7380ae7a4ba3765a1dc9f768d0 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Wed, 30 Dec 2020 17:59:42 +0100 Subject: [PATCH] Fixes #367 (Call to a member function init() on boolean in Header.php on line 66) (#380) * deployed fix for #367 by @lukgru, added two tests to demonstrate expected behavior * fixed coding style --- src/Smalot/PdfParser/Header.php | 6 ++++-- tests/Integration/HeaderTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Smalot/PdfParser/Header.php b/src/Smalot/PdfParser/Header.php index c4f7c9f2..faf79af4 100644 --- a/src/Smalot/PdfParser/Header.php +++ b/src/Smalot/PdfParser/Header.php @@ -62,8 +62,10 @@ public function __construct($elements = [], Document $document = null) public function init() { - foreach ($this->elements as $name => $element) { - $element->init(); + foreach ($this->elements as $element) { + if ($element instanceof Element) { + $element->init(); + } } } diff --git a/tests/Integration/HeaderTest.php b/tests/Integration/HeaderTest.php index 4a8e3c9a..92eaa9f9 100644 --- a/tests/Integration/HeaderTest.php +++ b/tests/Integration/HeaderTest.php @@ -32,6 +32,7 @@ namespace Tests\Smalot\PdfParser\Integration; +use Smalot\PdfParser\Element; use Smalot\PdfParser\Element\ElementMissing; use Smalot\PdfParser\Element\ElementName; use Smalot\PdfParser\Header; @@ -44,6 +45,35 @@ */ class HeaderTest extends TestCase { + /** + * Checks that init function is called for each element. + */ + public function testInitHappyPath() + { + $element = $this->createMock(Element::class); + $element->expects($this->exactly(1))->method('init'); + + $fixture = new Header([$element]); + $fixture->init(); + } + + /** + * Checks buggy behavior if an element was given which is not of type Element. + * + * Problem was, it always called $element::init(), even if its not an object at all. + * + * @see https://github.com/smalot/pdfparser/issues/367 + * + * @doesNotPerformAssertions + */ + public function testInitInvalidElement() + { + $element = false; + + $fixture = new Header([$element]); + $fixture->init(); + } + public function testParse() { $document = $this->getDocumentInstance();