This is a test
And another one
';
$footnotesXml = '
footnote text
';
$endnotesXml = '
This is an endnote
';
$phpWord = $this->getDocumentFromString(['document' => $documentXml, 'footnotes' => $footnotesXml, 'endnotes' => $endnotesXml]);
$elements = $phpWord->getSection(0)->getElements();
self::assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[0]);
/** @var \PhpOffice\PhpWord\Element\TextRun $textRun */
$textRun = $elements[0];
//test the text in the first paragraph
/** @var \PhpOffice\PhpWord\Element\Text $text */
$text = $elements[0]->getElement(0);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text);
self::assertEquals('This is a test', $text->getText());
//test the presence of the footnote in the document.xml
/** @var \PhpOffice\PhpWord\Element\Footnote $footnote */
$documentFootnote = $textRun->getElement(1);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Footnote', $documentFootnote);
self::assertEquals(1, $documentFootnote->getRelationId());
//test the presence of the footnote in the footnote.xml
/** @var \PhpOffice\PhpWord\Element\Footnote $footnote */
$footnote = $phpWord->getFootnotes()->getItem(1);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Footnote', $footnote);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $footnote->getElement(0));
self::assertEquals('footnote text', $footnote->getElement(0)->getText());
self::assertEquals(1, $footnote->getRelationId());
//test the text in the second paragraph
/** @var \PhpOffice\PhpWord\Element\Text $text */
$text = $elements[1]->getElement(0);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text);
self::assertEquals('And another one', $text->getText());
//test the presence of the endnote in the document.xml
/** @var \PhpOffice\PhpWord\Element\Endnote $endnote */
$documentEndnote = $elements[1]->getElement(1);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Endnote', $documentEndnote);
self::assertEquals(2, $documentEndnote->getRelationId());
//test the presence of the endnote in the endnote.xml
/** @var \PhpOffice\PhpWord\Element\Endnote $endnote */
$endnote = $phpWord->getEndnotes()->getItem(1);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Endnote', $endnote);
self::assertEquals(2, $endnote->getRelationId());
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $endnote->getElement(0));
self::assertEquals('This is an endnote', $endnote->getElement(0)->getText());
}
public function testReadHeadingWithOverriddenStyle(): void
{
$documentXml = '
This is a bold
heading
but with parts not in bold
';
$stylesXml = '
';
$phpWord = $this->getDocumentFromString(['document' => $documentXml, 'styles' => $stylesXml]);
$elements = $phpWord->getSection(0)->getElements();
self::assertInstanceOf('PhpOffice\PhpWord\Element\Title', $elements[0]);
/** @var \PhpOffice\PhpWord\Element\Title $title */
$title = $elements[0];
self::assertEquals('Heading1', $title->getStyle());
/** @var \PhpOffice\PhpWord\Element\Text $text */
$text = $title->getText()->getElement(0);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text);
self::assertEquals('This is a bold ', $text->getText());
/** @var \PhpOffice\PhpWord\Element\Text $text */
$text = $title->getText()->getElement(1);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text);
self::assertEquals('heading', $text->getText());
self::assertFalse($text->getFontStyle()->isBold());
/** @var \PhpOffice\PhpWord\Element\Text $text */
$text = $title->getText()->getElement(2);
self::assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text);
self::assertEquals(' but with parts not in bold', $text->getText());
}
}