Skip to content

Commit

Permalink
Fixes #367 (Call to a member function init() on boolean in Header.php…
Browse files Browse the repository at this point in the history
… on line 66) (#380)

* deployed fix for #367 by @lukgru, added two tests to demonstrate expected behavior
* fixed coding style
  • Loading branch information
k00ni authored Dec 30, 2020
1 parent a4bb6d2 commit 8b8a157
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Smalot/PdfParser/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions tests/Integration/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit 8b8a157

Please sign in to comment.